[SERVER-5739] Race condition in SOMETIMES/MONGO_SOMETIMES macros Created: 01/May/12  Updated: 15/Dec/18  Resolved: 16/Nov/18

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: 2.1.1
Fix Version/s: 4.1.6

Type: Bug Priority: Minor - P4
Reporter: Andy Schwerin Assignee: Billy Donahue
Resolution: Done Votes: 0
Labels: neweng, platforms-interns-2017
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-37247 Rarely macro should log first use of ... Closed
is related to SERVER-14536 Helgrind suppressions for race-prone ... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Dev Tools 2018-11-19
Participants:

 Description   

The definition of MONGO_SOMETIMES contains a race condition. It's current implementation is as follows:

#define MONGO_SOMETIMES( occasion, howOften ) for( static unsigned occasion = 0; ++occasion % howOften == 0; )

Plain-and-simple not thread safe.



 Comments   
Comment by Githook User [ 16/Nov/18 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-5739 Fix races in RARELY/OCCASIONALLY.

Switch to using C++ instead of macros.
Fix SERVER-37247: these should fire on first hit.
Branch: master
https://github.com/mongodb/mongo/commit/63e43f1bb47f7bddf3dc37ad03a2bbee6d2a9423

Comment by Andrew Morrow (Inactive) [ 22/Dec/16 ]

Note that when this ticket was filed, there were in fact two races in the implemenation:

  • The initialization of the static occasion variable was not thread-safe pre-C++11
  • The increment of the occasion variable was non-atomic.

While the first of these issues was addressed by our move to C++11 and modernization of the toolchain, the latter has not been. The best solution is to probably declare the occasion variable as a static std::atomic.

We should consider, when fixing this, whether these should be 32-bit or 64-bit counters, and use the appropriate fixed-width underlying type for the atomics.

Generated at Thu Feb 08 03:09:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.