[SERVER-31491] fix race in ThreadPoolTest::ThreadPoolRunsOnCreateThreadFunctionBeforeConsumingTasks Created: 10/Oct/17  Updated: 30/Oct/23  Resolved: 13/Oct/17

Status: Closed
Project: Core Server
Component/s: Concurrency
Affects Version/s: None
Fix Version/s: 3.6.0-rc1

Type: Bug Priority: Major - P3
Reporter: Benety Goh Assignee: Benety Goh
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Repl 2017-10-23
Participants:
Linked BF Score: 0

 Description   

In this test function, since the thread function provided to the ThreadPool relies on the Barrier, it is incorrect for the Barrier to have a shorter lifespan than the ThreadPool instance.

https://github.com/mongodb/mongo/blob/edc3dfce51b00ba3007f35cf6383efb72bf8b74e/src/mongo/util/concurrency/thread_pool_test.cpp#L256

thread_pool_test.cpp

242
TEST_F(ThreadPoolTest, ThreadPoolRunsOnCreateThreadFunctionBeforeConsumingTasks) {
243
    bool onCreateThreadCalled = false;
244
    std::string taskThreadName;
245
    ThreadPool::Options options;
246
    options.threadNamePrefix = "mythread";
247
    options.maxThreads = 1U;
248
    options.onCreateThread = [&onCreateThreadCalled,
249
                              &taskThreadName](const std::string& threadName) {
250
        onCreateThreadCalled = true;
251
        taskThreadName = threadName;
252
    };
253
 
254
    auto& pool = makePool(options);
255
    pool.startup();
256
    unittest::Barrier barrier(2U);
257
    ASSERT_OK(pool.schedule([&barrier] { barrier.countDownAndWait(); }));
258
    barrier.countDownAndWait();
259
 
260
    ASSERT_TRUE(onCreateThreadCalled);
261
    ASSERT_EQUALS(options.threadNamePrefix + "0", taskThreadName);
262
}



 Comments   
Comment by Githook User [ 13/Oct/17 ]

Author:

{'email': 'benety@mongodb.com', 'name': 'Benety Goh', 'username': 'benety'}

Message: SERVER-31491 fix race in ThreadPoolTest::ThreadPoolRunsOnCreateThreadFunctionBeforeConsumingTasks

This patch fixes the relative scopes of the Barrier and ThreadPool instances in the test function.
Since the thread function provided to the ThreadPool refer to the Barrier, the Barrier must outlive
the ThreadPool.
Branch: master
https://github.com/mongodb/mongo/commit/24e23ac82f32e24fb9ea2235ec9093c6688552b1

Generated at Thu Feb 08 04:27:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.