Repository navigation
Crash due to ConVar overlap and release. #421 #430
Description
This is an issue derived from #421 and introduced by 02e3490. There is a long discussion going on in #421, and I've created an issue for the record.
The main issue is due to Source.Python releasing the ConVar object created by ConVar(str) when the plugin is unloaded. This will cause other Source.Python/SourceMod plugins to crash if they hold the same ConVar object.
Code:
Plugin1 (test1)
from cvars import ConVar
test_convar = ConVar("test_convar", "1", "Test Convar(test1).")Plugin2 (test2) Or a SourceMod plugin with equivalent functionality.
from commands.server import ServerCommand
from cvars import ConVar
test_convar = ConVar("test_convar", "2", "Test Convar(test2).")
@ServerCommand("set_test")
def test_command(*args):
test_convar.set_string("3")Output;
$ sp plugin load test1
[SP] Loading plugin 'test1'...
[SP] Successfully loaded plugin 'test1'.
$ test_convar
"test_convar" = "1" - Test Convar(test1).
$ sp plugin load test2
[SP] Loading plugin 'test2'...
[SP] Successfully loaded plugin 'test2'.
$ sp plugin unload test1
[SP] Unloading plugin 'test1'...
[SP] Successfully unloaded plugin 'test1'.
$ set_test
Thread 1 "srcds_linux" received signal SIGSEGV, Segmentation fault.
0xf076d5e5 in boost::python::objects::polymorphic_id_generator<ConVar>::execute(void*) ()
The same problem occurs with the wrapping plugin for ConVars.
The root of the problem lies in the fact that ConVar(str) returns an existing registered ConVar.
Source.Python/src/core/modules/cvars/cvars.h
Lines 95 to 104 in 02e3490
| ConVar *pConVar = ICVarSharedExt::FindVar(g_pCVar, name); | |
| if (!pConVar) | |
| { | |
| ConVar* pConVar = new ConVar(strdup(name), strdup(value), flags, | |
| strdup(description), !min_value.is_none(), fMin, !max_value.is_none(), fMax); | |
| return boost::shared_ptr<ConVar>(pConVar, &Deleter); | |
| } | |
| return boost::shared_ptr<ConVar>(pConVar, &NeverDeleteDeleter<ConVar *>); |
In the Valve Server Plugin, the ConVar used by the plugin is created each time it is loaded, and released when it is unloaded. Also, when accessing a ConVar that already exists, the plugin retrieves the ConVar from ICvar.FindVar(g_pCVar->FindVar) and checks if it is valid before accessing it. However, SourcePython's ConVar(str) and SourceMod's CreateConVar return an existing ConVar, so if there are overlapping ConVars, intentionally or unintentionally, the plugin will own a single ConVar, resulting in a crash.
Unfortunately, ConVar(str) is used by many plugins to access an existing ConVar instead of cvar.find_var which should be used, and it is not possible to not return an existing ConVar without causing a major compatibility break.
But either way, when ConVar overlaps, the parent is set and the children access the parent, so when the parent is released, this also causes a crash. (This has been disabled in 02e3490, but should not be disabled for warnings.)
Source.Python/src/core/modules/commands/commands_server.cpp
Lines 54 to 62 in 02e3490
| virtual bool RegisterConCommandBase(ConCommandBase* pCommand) | |
| { | |
| if (!g_pCVar->FindCommandBase(pCommand->GetName())) { | |
| g_pCVar->RegisterConCommand(pCommand); | |
| return true; | |
| } | |
| return false; | |
| } |
We should stop releasing ConVar when unloading plugins, and if we do release it, at least it should be done when unloading Source.Python VSP, so that plugins can work safely. However, if we want to be compatible with SourceMod, I think we should prohibit release of ConVar itself.
Activity
Fixed #429.
Ayuto commentedon Oct 31, 2021
Sorry, for joning the discussion so late, but here is my opinion on this topic.
Deleting ConVars:
The issue we have with deleting ConVars is that we don't have a reference counter. With a reference counter we could easily keep track whether or not a ConVar can be deleted, but without one we have a little problem. We could implement our own reference counter for ConVars we created on our own, but that would only work in a world where we are the only VSP. This isn't the case, obviously. So, we should think about a different solution as I don't want to have any avoidable incompatibilities with SM. And the only solution I can think of (currently) is not deleting any ConVars. This isn't the best solution, but it's not like we have a constant memory leak that grows with every function call. Once the ConVar is created the memory is reserved and it's not going to grow further. This will never cause a server to reserve all available RAM, which then results in a crash due to unsufficient memory. I wouldn't even delete the ConVar if SP is being unloaded, because we don't know if there is another VSP or SM plugin that relies on the same ConVar (e. g. because of an unfortune coincidence or because the foreign plugin extended an SP plugin).
ConCommands overriding ConVars
I'm not really sure about this one. I would probably raise an exception if someone tries to create a ConCommand with the same name like a ConVar or vice-versa. This has two simple reasons:
jordanbriere commentedon Oct 31, 2021
Actually, we already do have a reference counter (
shared_ptr), we just don't use it and create a new unmanaged one every time. In fact, this is very easy to implement with a simple mapping fromConVar *→weak_ptr. Here is a quick example:../cvars/cvars.h
../test1/test1.py
../test2/test2.py
../collect/collect.py
Despite the assumptions that have been presented as facts countless times; they are not affected whether we release our
ConVaror not because, a stated into the other thread, they are unlinking their references as soon as we remove them from the global list. The incompatibilities are only one way; us wrapping their memory as if we own it which remain the case whether we release or purposely leak ourConVar.That said, as also stated into the other thread, our commands act the same exact way. For example:
../test1/test1.py
../test2/test2.py
Then running
sp plugin load test1;sp plugin load test2;sp plugin unload test1;test2will crash, etc. Leaking one, but not the other, seem inconsistent to me considering they are pretty much the same internally and should be treated as such. Though, commands cannot really be leaked since we also own their callbacks.Anyways, I just wanted to clarify these points. You guys go ahead and take the final decision and make the appropriate changes.
Re-introduced memory leaks. 😖 (Fixes Source-Python-Dev-Team#430)