summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmirsaman Memaripour <amirsaman.memaripour@mongodb.com>2020-05-26 14:24:08 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-06-02 21:48:06 +0000
commitfe30303411c922fe45d7466dd951b4244d79c5b2 (patch)
treef3c696627e31f1c965f48c3671da90dea2b35c69
parent9d3b31c1617eff81be08dd24d46870e0bcb2eb99 (diff)
downloadmongo-fe30303411c922fe45d7466dd951b4244d79c5b2.tar.gz
SERVER-48346 Fix lifetime issues for barriers captured by reference
Extends the lifetime of barriers that are referenced by background threads and their reference could outlive the barrier's owner. (cherry picked from commit 3b7bc03a8f8b964769ff708674163b67ddd1a450)
-rw-r--r--src/mongo/executor/thread_pool_task_executor_test.cpp6
-rw-r--r--src/mongo/util/concurrency/thread_pool_test.cpp22
2 files changed, 15 insertions, 13 deletions
diff --git a/src/mongo/executor/thread_pool_task_executor_test.cpp b/src/mongo/executor/thread_pool_task_executor_test.cpp
index b487cc87a41..40bf1c1249f 100644
--- a/src/mongo/executor/thread_pool_task_executor_test.cpp
+++ b/src/mongo/executor/thread_pool_task_executor_test.cpp
@@ -86,6 +86,9 @@ TEST_F(ThreadPoolExecutorTest, Schedule) {
});
barrier.countDownAndWait();
ASSERT_OK(status1);
+ // Wait for the executor to stop to ensure the scheduled job does not outlive current scope.
+ executor.shutdown();
+ executor.join();
}
TEST_F(ThreadPoolExecutorTest, ScheduleAfterShutdown) {
@@ -112,6 +115,9 @@ TEST_F(ThreadPoolExecutorTest, OnEvent) {
executor.signalEvent(event);
barrier.countDownAndWait();
ASSERT_OK(status1);
+ // Wait for the executor to stop to ensure the scheduled job does not outlive current scope.
+ executor.shutdown();
+ executor.join();
}
TEST_F(ThreadPoolExecutorTest, OnEventAfterShutdown) {
diff --git a/src/mongo/util/concurrency/thread_pool_test.cpp b/src/mongo/util/concurrency/thread_pool_test.cpp
index b57554add21..ff97ec92d50 100644
--- a/src/mongo/util/concurrency/thread_pool_test.cpp
+++ b/src/mongo/util/concurrency/thread_pool_test.cpp
@@ -32,8 +32,10 @@
#include "mongo/platform/basic.h"
#include <boost/optional.hpp>
+#include <fmt/format.h>
#include "mongo/base/init.h"
+#include "mongo/platform/atomic_word.h"
#include "mongo/platform/mutex.h"
#include "mongo/stdx/condition_variable.h"
#include "mongo/stdx/thread.h"
@@ -49,6 +51,7 @@
namespace {
using namespace mongo;
+using namespace fmt::literals;
MONGO_INITIALIZER(ThreadPoolCommonTests)(InitializerContext*) {
addTestsForThreadPool("ThreadPoolCommon",
@@ -254,29 +257,22 @@ DEATH_TEST(ThreadPoolTest,
TEST_F(ThreadPoolTest, ThreadPoolRunsOnCreateThreadFunctionBeforeConsumingTasks) {
unittest::Barrier barrier(2U);
-
- bool onCreateThreadCalled = false;
- std::string taskThreadName;
+ std::string journal;
ThreadPool::Options options;
options.threadNamePrefix = "mythread";
options.maxThreads = 1U;
- options.onCreateThread = [&onCreateThreadCalled,
- &taskThreadName](const std::string& threadName) {
- onCreateThreadCalled = true;
- taskThreadName = threadName;
+ options.onCreateThread = [&](const std::string& threadName) {
+ journal.append("[onCreate({})]"_format(threadName));
};
ThreadPool pool(options);
pool.startup();
-
- pool.schedule([&barrier](auto status) {
- ASSERT_OK(status);
+ pool.schedule([&](auto status) {
+ journal.append("[Call({})]"_format(status.toString()));
barrier.countDownAndWait();
});
barrier.countDownAndWait();
-
- ASSERT_TRUE(onCreateThreadCalled);
- ASSERT_EQUALS(options.threadNamePrefix + "0", taskThreadName);
+ ASSERT_EQUALS(journal, "[onCreate(mythread0)][Call(OK)]");
}
} // namespace