Use std::lock_guard for exception safety in ThreadSafeMap and ThreadSafeQueue#157
Merged
davideberly merged 1 commit intodavideberly:masterfrom Feb 28, 2026
Conversation
…afeQueue All methods used manual mMutex.lock()/mMutex.unlock() instead of RAII. If any operation between lock and unlock throws (e.g., copy constructor, map insertion), the mutex would never be unlocked, causing deadlock. Replaced all manual lock/unlock pairs with std::lock_guard<std::mutex>. Co-Authored-By: Claude Opus 4.6 <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.
Summary
ThreadSafeMap.handThreadSafeQueue.hused manualmMutex.lock()/mMutex.unlock()instead of RAIIvalue = iter->second, map insertion inmMap[key] = value), the mutex would never be unlocked, causing a deadlockstd::lock_guard<std::mutex>, which guarantees unlock on scope exit regardless of exceptionsTest plan
This fix is structural (exception-safety improvement). The bug only manifests when a copy/move constructor throws while the mutex is held, which requires a specially crafted throwing type. The behavioral change is that mutex unlock is now guaranteed in all code paths.
Verified that the files compile correctly and the API is unchanged.
🤖 Generated with Claude Code