Commit dfe13560 authored by Misha Shneerson's avatar Misha Shneerson Committed by Facebook GitHub Bot

fix deadlock folly::RequestContext::try_get()

Summary:
`folly::RequestContext::try_get()` might deadlock if accessor to all threads is
held alive.

Reviewed By: amlannayak

Differential Revision: D31496646

fbshipit-source-id: 6edfd0a54f8bfaf72218d78ba1a6c09dc92a8e63
parent c3bb66ae
......@@ -608,13 +608,18 @@ void RequestContext::clearContextData(const RequestToken& val) {
return prevCtx;
}
namespace {
thread_local bool getStaticContextCalled = false;
}
/* static */ RequestContext::StaticContext& RequestContext::getStaticContext() {
getStaticContextCalled = true;
return StaticContextThreadLocal::get();
}
/* static */ RequestContext::StaticContext*
RequestContext::tryGetStaticContext() {
return StaticContextThreadLocal::try_get();
return getStaticContextCalled ? &StaticContextThreadLocal::get() : nullptr;
}
/* static */ RequestContext::StaticContextAccessor
......
......@@ -584,4 +584,19 @@ TEST(RequestContextTryGetTest, TryGetTest) {
EXPECT_TRUE(rc->hasContextData("test"));
auto* dataPtr = dynamic_cast<TestData*>(rc->getContextData("test"));
EXPECT_EQ(dataPtr->data_, 10);
auto thread = std::thread([&] {
auto accessor = RequestContext::accessAllThreads();
// test there is no deadlock with try_get()
RequestContext::try_get();
});
thread.join();
thread = std::thread([&] {
RequestContext::get();
auto accessor = RequestContext::accessAllThreads();
// test there is no deadlock with get()
RequestContext::get();
});
thread.join();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment