diff options
author | Daniel Vitor Morilha <daniel.morilha@mongodb.com> | 2021-12-21 14:50:43 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-12-21 15:40:38 +0000 |
commit | 36cb5167bbe5053bb806f5c4c060a80b2cc791c0 (patch) | |
tree | 9e0db499827e10d61763191278905b7868b1427f | |
parent | 19080405d9c7d08b54e70228a92d237223f2885c (diff) | |
download | mongo-36cb5167bbe5053bb806f5c4c060a80b2cc791c0.tar.gz |
SERVER-42971 Restore original thread name upon ThreadClient destruction
-rw-r--r-- | src/mongo/db/client.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/client.h | 3 | ||||
-rw-r--r-- | src/mongo/db/thread_client_test.cpp | 20 |
3 files changed, 25 insertions, 0 deletions
diff --git a/src/mongo/db/client.cpp b/src/mongo/db/client.cpp index 6982ad9e9db..7da46be32df 100644 --- a/src/mongo/db/client.cpp +++ b/src/mongo/db/client.cpp @@ -188,12 +188,14 @@ ThreadClient::ThreadClient(StringData desc, ServiceContext* serviceContext, transport::SessionHandle session) { invariantNoCurrentClient(); + _originalThreadName = ThreadName::get(ThreadContext::get()); Client::initThread(desc, serviceContext, std::move(session)); } ThreadClient::~ThreadClient() { invariant(currentClient); currentClient.reset(nullptr); + ThreadName::set(ThreadContext::get(), _originalThreadName); } Client* ThreadClient::get() const { diff --git a/src/mongo/db/client.h b/src/mongo/db/client.h index 67b8ed959d5..e64c6b24d34 100644 --- a/src/mongo/db/client.h +++ b/src/mongo/db/client.h @@ -335,6 +335,9 @@ public: Client& operator*() const { return *get(); } + +private: + boost::intrusive_ptr<ThreadName> _originalThreadName; }; /** diff --git a/src/mongo/db/thread_client_test.cpp b/src/mongo/db/thread_client_test.cpp index 6c700a3a741..08f448c61eb 100644 --- a/src/mongo/db/thread_client_test.cpp +++ b/src/mongo/db/thread_client_test.cpp @@ -87,5 +87,25 @@ TEST_F(ThreadClientTest, TestAlternativeClientRegion) { ASSERT_TRUE(haveClient()); } + +/** + * This test asserts that original thread names are restored after a ThreadClient object + * goes out of scope. + */ +TEST_F(ThreadClientTest, TestThreadName) { + ASSERT_FALSE(haveClient()); + const auto originalThreadName = getThreadName(); + + { + ThreadClient threadClient("MyThreadClient", getGlobalServiceContext()); + ASSERT_TRUE(haveClient()); + // The instatiation of ThreadClient should have changed this thread name + ASSERT_NE(originalThreadName, getThreadName()); + } + + ASSERT_FALSE(haveClient()); + // The original name for this thread should have been restored. + ASSERT_EQ(originalThreadName, getThreadName()); +} } // namespace } // namespace mongo |