[WIN] Enhance threadsafety of DbgHelp - #754
Merged
Merged
Conversation
Member
Author
tlopex
approved these changes
Sep 6, 2026
DbgHelp is single-threaded and the caller must serialize it, but TVMFFIBacktrace called it unguarded. An exception raised while another thread was formatting a backtrace crashed the process inside the reporter, so the original error was never printed. Guard every DbgHelp call with one mutex. Key the session on a duplicated process handle rather than GetCurrentProcess(), whose value is shared by every component, so another DbgHelp user cannot close our symbols. Initialize once and keep the session instead of doing it per call, which also removes module enumeration from the locked region. A failed initialization now yields a backtrace carrying only the caller's own frame instead of walking against a session that was never established.
tqchen
force-pushed
the
fix-win-dbghelp-session
branch
from
September 6, 2026 14:28
b3930f1 to
9e25fce
Compare
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.
src/ffi/backtrace_win.cccalled DbgHelp unguarded, though DbgHelp issingle-threaded and requires the caller to serialize it. The process died while
formatting a backtrace for another thread's exception, so the original error was
never printed.
GetCurrentProcess()hasthe same value in every component, so another DbgHelp user in the process could
close our symbols. A fresh key also makes
SymInitialize's failureunambiguous, so its result can be checked.
SymInitialize/SymCleanupand with it module enumeration inside the lock.There is deliberately no destructor:
SymCleanupat static-destruction timewould reintroduce teardown at an unpredictable point relative to DLL unload.
A failed initialization now returns a backtrace with only the caller's own frame.
StackWalk64resolves through the session, so without one there is no unwindingat all.