Skip to content

Commit 8495449

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Fix data race in leak detection
TSAN identified a data race between updates to the states_ map (ex. in Mock::UnregisterLocked) and the iteration done in this destructor. Writes to the map use g_gmock_mutex, but the destructor does not acquire it. Acquiring the lock here fixes the data race. It should only be possible to trigger this TSAN finding in cases where a mock object is deleted by a thread other than the main thread. PiperOrigin-RevId: 591935393 Change-Id: I9dd1faa40058d78e165a91333346514b4b73365c
1 parent 530d5c8 commit 8495449

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

googlemock/src/gmock-spec-builders.cc

+1
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ class MockObjectRegistry {
490490
// failure, unless the user explicitly asked us to ignore it.
491491
~MockObjectRegistry() {
492492
if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return;
493+
internal::MutexLock l(&internal::g_gmock_mutex);
493494

494495
int leaked_count = 0;
495496
for (StateMap::const_iterator it = states_.begin(); it != states_.end();

0 commit comments

Comments
 (0)