Repository navigation
HLE: load a host DLL outside the module cache's lock (loader-lock deadlock) - #357
Merged
Marvisak merged 1 commit intoNov 25, 2026
Merged
Conversation
host::module held the module cache's lock while it called LoadLibraryExA. Loading a DLL that is not loaded yet takes the OS loader lock, and title code runs under that lock too (a title DLL's thread attach, TLS callbacks run as threads start), where it can need the cache: any first use of a host! function, or a late-bound title DLL. One thread holding the cache and waiting for the loader lock, another holding the loader lock and waiting for the cache: the process hangs, and cannot even exit, as ExitProcess needs the loader lock too. The cache is now looked up, then unlocked for the load, then locked again to keep the handle; a thread that loaded the same DLL meanwhile got the same module, which stays loaded.
muhammed-aksoy
pushed a commit
to muhammed-aksoy/VIctory
that referenced
this pull request
Nov 24, 2026
edsuuu
referenced
this pull request
in edsuuu/VI-BR-PORT
Nov 24, 2026
…loader-lock deadlock) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
Tested PR #357 at head
Behavior covered: Checks loading a host DLL no longer blocks thread attach while module-cache operations are active. |
Closed
mercuryVM
approved these changes
Nov 24, 2026
mercuryVM
left a comment
There was a problem hiding this comment.
Tested c6959ac on Windows 11, RTX 5070, MSVC: cargo test -p victory-hle 89 passed, including loading_a_host_dll_does_not_block_thread_attach, which runs in a child process with a 30 s kill switch. Clippy's 1 warning is the existing field_reassign_with_default at xmem.rs:747.
- Dropping
MODULESbeforeLoadLibraryExAremoves the lock-order inversion with the OS loader lock that aDLL_THREAD_ATTACHor TLS callback looking up a function could hit. Re-taking the lock afterwards withentry().or_insertmakes a racing double load harmless: both handles are to the same module, which stays loaded. - The test's
.CRT$XLYTLS callback iscfg(test)and gated byARMED, so other tests in the binary only pay an atomic load per thread start.
AI-assisted (Claude Code).
mercuryVM
added a commit
to mercuryVM/VIctory
that referenced
this pull request
Nov 24, 2026
…le cache's lock (loader-lock deadlock) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 24, 2026
github-actions Bot
added a commit
to KevinPratap/VIctory
that referenced
this pull request
Nov 24, 2026
…ock (loader-lock deadlock)
github-actions Bot
added a commit
to KevinPratap/VIctory
that referenced
this pull request
Nov 24, 2026
…ock (loader-lock deadlock)
github-actions Bot
added a commit
to KevinPratap/VIctory
that referenced
this pull request
Nov 24, 2026
…ock (loader-lock deadlock)
Steenax6
added a commit
to Steenax6/VIctory-NVID
that referenced
this pull request
Nov 26, 2026
host::module held the module cache's lock while it called LoadLibraryExA. Loading a DLL that is not loaded yet takes the OS loader lock, and title code runs under that lock too (a title DLL's thread attach, TLS callbacks as threads start), where it can need the cache: one thread holding the cache and waiting for the loader lock, another holding the loader lock and waiting for the cache hangs the process, which then cannot even exit. The cache is looked up, unlocked for the load, and locked again to keep the handle; a thread that loaded the same DLL meanwhile got the same module, which stays loaded. The commit's test (a child process with a TLS callback that looks a function up under the loader lock) comes with it. Source: Marvisak/VIctory c6959ac (Duff), merged as PR Marvisak#357. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's wrong
host::module(invictory-hle/src/host.rs) caches the handles of the host DLLs we pass calls through to. On a miss it callsLoadLibraryExAwhile still holding the cache's lock (MODULES).Loading a DLL that isn't loaded yet takes the OS loader lock. Title code also runs under the loader lock: a title DLL's
DllMain(DLL_THREAD_ATTACH/DETACH), and TLS callbacks, which our loader runs as every thread starts and ends. That code can needMODULES. Any first use of ahost!function does (files.rs,net.rs,xsystem.rsandmedia.rsresolve theirs lazily), and so does binding a title DLL that's loaded late. That's two locks taken in opposite orders:Neither thread can go on. The process hangs, and can't even exit, since
ExitProcessneeds the loader lock too. Microsoft's DLL best practices name this exact pattern: holding a lock while callingLoadLibrary, when code under the loader lock can take that lock, is a lock-order deadlock.What the fix does
modulenow:LoadLibraryExAwithout holding the lock;entry(..).or_insert(..)).If two threads load the same DLL at once, both get the same module handle. Whichever stores first wins, and nothing is ever freed, so the race is harmless.
How it behaves after the fix
Loading a host DLL never blocks a thread that only needs the cache. Title thread starts and ends no longer deadlock against a late DLL load. Lookups that hit the cache don't change.
Tests
loading_a_host_dll_does_not_block_thread_attach(inhost.rs) builds the exact race:.CRT$XLY), armed for one thread start, holds the loader lock like a title's thread attach. It sleeps 500 ms, then looks upkernel32.dll!GetTickCount, which is already cached, so it only needsMODULES.cabinet.dll!FDICreate, a DLL nothing has loaded yet, which needs the loader lock.I ran
cargo test --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml:victory-hlehas 87 passed, the other crates have 0 failed.main: the child deadlocks. The parent kills it after 30 s:deadlock: a host DLL loads under the module cache's lock.--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (Tests: victory-shader tests crash (0xC0000005) when several tests use WARP devices in parallel #181, Shaders: share and serialize WARP host operations across threads #244).Not tested: the title itself starting threads during a late DLL load.