[SERVER-5738] Race condition in ONCE/MONGO_ONCE macros Created: 01/May/12  Updated: 05/Apr/17  Resolved: 04/Jan/17

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

Type: Bug Priority: Minor - P4
Reporter: Andy Schwerin Assignee: Mark Benvenuto
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Platforms 2017-01-23
Participants:

 Description   

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

#define MONGO_ONCE for( static bool undone = true; undone; undone = false )

Plain-and-simple not thread safe.



 Comments   
Comment by Githook User [ 04/Jan/17 ]

Author:

{u'username': u'markbenvenuto', u'name': u'Mark Benvenuto', u'email': u'mark.benvenuto@mongodb.com'}

Message: SERVER-5738 Remove MONGO_ONCE in favor of std::call_once
Branch: master
https://github.com/mongodb/mongo/commit/b7c7462c564298da54333775f7734e2623dfb08b

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

I'm in favor of std::call_once.

Comment by Mark Benvenuto [ 21/Dec/16 ]

acm adam.martin

There are two ways to fix this

  1. Transition to std::atomic or the like for the bool
  2. Remove the macro and use std::call_once

I prefer we just remove and use std::call_once instead.

There are two uses to prevent log spam (authz_session_external_state_server_common.cpp, and btree_logic.cpp), and there is one case in breakpoint(). Other then a test case, it is only used in three places.

Comment by Mark Benvenuto [ 19/Dec/16 ]

A better replacement for this in C++11 is std::call_once. http://en.cppreference.com/w/cpp/thread/call_once.

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