xash3d-fwgs icon indicating copy to clipboard operation
xash3d-fwgs copied to clipboard

Eliminate FreeLibrary/dlclose

Open a1batross opened this issue 3 years ago • 8 comments

dlclose and FreeLibrary functions do not guarantee that the library was really unloaded.

It may lead to crashes, especially with global variables, when these don't get reinitialized to default value. Simplest example is pm_shared_initialized global in HLSDK code. If it somehow was marked as NODELETE, it will instantly crash the engine.

glibc and windows tries to unload the library but for example musl do not implement that at all. Also in Android NDK 25 release notes there is an bug mentioned where using dlclose can lead to crashes on some Android versions. NODELETE flag or eliminating dlclose is the only fix.

Some mods also are not intended to be reloaded all the time, like Metamod. I also get easy crashes with halflife-unified-sdk.

We already switched to execv() call for changing game because of dlclose/FreeLibrary being unreliable.

Here is the list of some troublesome parts that needs to be fixed as soon as possible:

  • [ ] RefAPIs GetHumanReadable call. I guess we cannot collect renderers descriptions this way, loading the library, calling the export and unloading.
  • [ ] pfnCheckGameDll menu call. It's used for menu to detect if there is a usable server library, or we can only connect to multiplayer games.
  • [ ] settings.scr cvars collect. Engine uses that to generate listenserver.cfg
  • [ ] Engine still can unload server library. I believe we don't need this in general, goldsrc never unloads it.

a1batross avatar Aug 02 '22 20:08 a1batross

cc @nekonomicon @SNMetamorph @mittorn

a1batross avatar Aug 02 '22 20:08 a1batross

Yea I agree that this should be done.

SNMetamorph avatar Aug 03 '22 16:08 SNMetamorph

IDK why need to unload libraries, It's just waste of time.

nekonomicon avatar Aug 05 '22 00:08 nekonomicon

@nekonomicon glad to see you again! :)

I like loading libraries dynamicly, it makes sense for plugin architecture.

But unloading is unnecessary. We can only restart the engine.

Though guys from RTX branch @zgdump @0x4E69676874466F78 would not agree, since they implemented hot renderer swap.

a1batross avatar Aug 05 '22 00:08 a1batross

Eh, I really wanted to switch renders... But I still haven't figured out why after a couple of ref switches everything explodes in random places of the engine

vklachkov avatar Aug 08 '22 04:08 vklachkov

@zgdump well, I doubt we can do anything with runtime that doesn't cleanup or even do anything at all after dlclose() and FreeLibrary().

Crashes and bugs just unavoidable with them.

a1batross avatar Aug 08 '22 13:08 a1batross

@a1batross I understand, cut the ends! If i'm really want to do some bad things, I will make a ref_proxy with... [ton bad jokes about booms]

vklachkov avatar Aug 09 '22 19:08 vklachkov

@zgdump well, in theory, with carefully crafted code that doesn't have unneeded exports (because they can get shared, that's why we compile with -fvisibility=hidden by default) we can run multiple renderers simultaneously with some kind of RefAPI proxy.

a1batross avatar Aug 10 '22 14:08 a1batross