Skip to content

fix: avoid loader-lock work in DllMain detach#57

Open
H-Chris233 wants to merge 4 commits into
OpenSteam001:mainfrom
H-Chris233:fix-dllmain-detach-loaderlock
Open

fix: avoid loader-lock work in DllMain detach#57
H-Chris233 wants to merge 4 commits into
OpenSteam001:mainfrom
H-Chris233:fix-dllmain-detach-loaderlock

Conversation

@H-Chris233
Copy link
Copy Markdown
Contributor

@H-Chris233 H-Chris233 commented May 26, 2026

Problem

DllMain's DLL_PROCESS_DETACH path called SteamUI::CoreUnhook() and SteamClient::CoreUnhook(), which may perform LoadLibrary/FreeLibrary under the loader lock. This is unsafe during process termination and can cause deadlocks or undefined behavior.

Additionally, nothing prevented an external FreeLibrary from unloading the DLL while hooks and worker threads still reference its code.

Changes

  1. Pin the module on DLL_PROCESS_ATTACH via GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_PIN) so the DLL cannot be unmapped while still in use.

  2. Guard DETACH work: during process termination (pvReserved != nullptr), only call FileWatcher::Stop() to avoid std::thread's joinable-guard std::terminate. Skip CoreUnhook entirely since it may acquire the loader lock.

Testing

  • Verified DLL attaches/detaches cleanly in Steam processes.
  • FileWatcher thread joins properly during exit (no more std::terminate crashes).

@H-Chris233 H-Chris233 force-pushed the fix-dllmain-detach-loaderlock branch 2 times, most recently from 06f74a1 to 7c43c3f Compare May 27, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant