Hold m_loggersMutex in LoggerSystem::Shutdown#146
Open
JustinMDotNet wants to merge 1 commit into
Open
Conversation
Log<T> already takes m_loggersMutex around the lookup/emplace into m_loggers, but Shutdown iterated and cleared the same map with no lock. Any plugin thread or in-flight hook callback that called Log during shutdown could race against the iteration or use-after-clear. The race window is small but not theoretical: in the reverse-order shutdown, LoggerSystem::Shutdown runs last while RED4ext's own detours are still attached (those are detached in App::Destruct, see issue wopss#141). Take the scoped_lock at the top of Shutdown so the flush + clear pair is atomic with respect to Log. Fixes wopss#139 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.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.
Closes #139.
What this changes
LoggerSystem::Shutdownnow takesm_loggersMutexbefore iteratingm_loggersand callingclear(). One line added at the top of the function.Why
Log<T>inLoggerSystem.hpp:51-73already locks the mutex for the lookup/emplace.Shutdownwas the only path that mutated the same map without the lock, so any plugin thread or in-flight hook callback that calledLogduring shutdown could:Shutdownwas mid-iteration (iterator invalidation), orclear()had run.The window is narrow but real: the reverse-order shutdown in
App::ShutdownrunsLoggerSystem::Shutdownlast, by which point plugins have been "unloaded" but RED4ext's own game-side detours are still patched (those are not removed untilApp::Destruct, see #141). Anything those detours route into that calls back through the logger races against the clear.What I tested
Local rebuild. Functional behavior is unchanged on the single-threaded path; the only effect is that concurrent calls to
Logduring shutdown now serialize correctly instead of racing on the underlyingunordered_map.Note
This is companion to #141. Even after this PR, the safest fix for the shutdown story is to detach RED4ext's own hooks before any system is shut down, which is what #141 proposes separately. This PR fixes the lock-discipline bug in isolation; the two PRs are independent.