summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmirsaman Memaripour <amirsaman.memaripour@mongodb.com>2022-01-06 18:40:22 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-01-06 19:19:21 +0000
commita9bec8a996b1d8fd6d3e28b200d8483f9a944bcb (patch)
tree9378fc3b50fe09c4f2c2ea98b02ce787129edc4b
parent583a5b3d6e2aeb565fef7d2a3cfe6b4a16063be2 (diff)
downloadmongo-a9bec8a996b1d8fd6d3e28b200d8483f9a944bcb.tar.gz
SERVER-58310 Always hold `_mutex` before modifying `CallbackState::callback`
-rw-r--r--src/mongo/executor/thread_pool_task_executor.cpp11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/mongo/executor/thread_pool_task_executor.cpp b/src/mongo/executor/thread_pool_task_executor.cpp
index 2dfd75cddce..d642252c7f5 100644
--- a/src/mongo/executor/thread_pool_task_executor.cpp
+++ b/src/mongo/executor/thread_pool_task_executor.cpp
@@ -652,7 +652,10 @@ void ThreadPoolTaskExecutor::runCallback(std::shared_ptr<CallbackState> cbStateA
// Swap 'cbStateArg->callback' with temporary copy before running callback for exception
// safety.
TaskExecutor::CallbackFn callback;
- std::swap(cbStateArg->callback, callback);
+ {
+ auto lk = stdx::lock_guard(_mutex);
+ std::swap(cbStateArg->callback, callback);
+ }
callback(std::move(args));
}
cbStateArg->isFinished.store(true);
@@ -822,12 +825,16 @@ void ThreadPoolTaskExecutor::runCallbackExhaust(std::shared_ptr<CallbackState> c
if (!cbState->isFinished.load()) {
TaskExecutor::CallbackFn callback = [](const CallbackArgs&) {};
- std::swap(cbState->callback, callback);
+ {
+ auto lk = stdx::lock_guard(_mutex);
+ std::swap(cbState->callback, callback);
+ }
callback(std::move(args));
// Leave the empty callback function if the request has been marked canceled or finished
// while running the callback to avoid leaking resources.
if (!cbState->canceled.load() && !cbState->isFinished.load()) {
+ auto lk = stdx::lock_guard(_mutex);
std::swap(callback, cbState->callback);
}
}