Commit 4d499e08 authored by Mirek Klimos's avatar Mirek Klimos Committed by Facebook Github Bot 6

Replace RequestContext::setContext with RequestContextScopeGuard in folly

Summary: To make sure RequestContext is properly unset when we stop processing request on a thread. This changes the API in Fibers, NotificationQueue, HHWheelTimer and AsyncTimeout, and fixes RequestContext handling in Futures (reset RC after the callback is done)

Reviewed By: andriigrynenko

Differential Revision: D3279644

fbshipit-source-id: a6a1c2840cdce179334aa1a3b28fa514cd5358c1
parent 6da3b849
...@@ -334,28 +334,28 @@ class Core { ...@@ -334,28 +334,28 @@ class Core {
if (LIKELY(x->getNumPriorities() == 1)) { if (LIKELY(x->getNumPriorities() == 1)) {
x->add([this]() mutable { x->add([this]() mutable {
SCOPE_EXIT { detachOne(); }; SCOPE_EXIT { detachOne(); };
RequestContext::setContext(context_); RequestContextScopeGuard rctx(context_);
SCOPE_EXIT { callback_ = {}; }; SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_)); callback_(std::move(*result_));
}); });
} else { } else {
x->addWithPriority([this]() mutable { x->addWithPriority([this]() mutable {
SCOPE_EXIT { detachOne(); }; SCOPE_EXIT { detachOne(); };
RequestContext::setContext(context_); RequestContextScopeGuard rctx(context_);
SCOPE_EXIT { callback_ = {}; }; SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_)); callback_(std::move(*result_));
}, priority); }, priority);
} }
} catch (...) { } catch (...) {
--attached_; // Account for extra ++attached_ before try --attached_; // Account for extra ++attached_ before try
RequestContext::setContext(context_); RequestContextScopeGuard rctx(context_);
result_ = Try<T>(exception_wrapper(std::current_exception())); result_ = Try<T>(exception_wrapper(std::current_exception()));
SCOPE_EXIT { callback_ = {}; }; SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_)); callback_(std::move(*result_));
} }
} else { } else {
SCOPE_EXIT { detachOne(); }; SCOPE_EXIT { detachOne(); };
RequestContext::setContext(context_); RequestContextScopeGuard rctx(context_);
SCOPE_EXIT { callback_ = {}; }; SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_)); callback_(std::move(*result_));
} }
......
...@@ -157,12 +157,9 @@ void AsyncTimeout::libeventCallback(libevent_fd_t fd, short events, void* arg) { ...@@ -157,12 +157,9 @@ void AsyncTimeout::libeventCallback(libevent_fd_t fd, short events, void* arg) {
// this can't possibly fire if timeout->eventBase_ is nullptr // this can't possibly fire if timeout->eventBase_ is nullptr
timeout->timeoutManager_->bumpHandlingTime(); timeout->timeoutManager_->bumpHandlingTime();
auto old_ctx = RequestContextScopeGuard rctx(timeout->context_);
RequestContext::setContext(timeout->context_);
timeout->timeoutExpired(); timeout->timeoutExpired();
RequestContext::setContext(old_ctx);
} }
} // folly } // folly
...@@ -214,10 +214,8 @@ void HHWheelTimer::timeoutExpired() noexcept { ...@@ -214,10 +214,8 @@ void HHWheelTimer::timeoutExpired() noexcept {
count_--; count_--;
cb->wheel_ = nullptr; cb->wheel_ = nullptr;
cb->expiration_ = milliseconds(0); cb->expiration_ = milliseconds(0);
auto old_ctx = RequestContextScopeGuard rctx(cb->context_);
RequestContext::setContext(cb->context_);
cb->timeoutExpired(); cb->timeoutExpired();
RequestContext::setContext(old_ctx);
} }
} }
if (count_ > 0) { if (count_ > 0) {
......
...@@ -705,8 +705,7 @@ void NotificationQueue<MessageT>::Consumer::consumeMessages( ...@@ -705,8 +705,7 @@ void NotificationQueue<MessageT>::Consumer::consumeMessages(
auto& data = queue_->queue_.front(); auto& data = queue_->queue_.front();
MessageT msg(std::move(data.first)); MessageT msg(std::move(data.first));
auto old_ctx = RequestContextScopeGuard rctx(std::move(data.second));
RequestContext::setContext(data.second);
queue_->queue_.pop_front(); queue_->queue_.pop_front();
// Check to see if the queue is empty now. // Check to see if the queue is empty now.
...@@ -728,8 +727,6 @@ void NotificationQueue<MessageT>::Consumer::consumeMessages( ...@@ -728,8 +727,6 @@ void NotificationQueue<MessageT>::Consumer::consumeMessages(
messageAvailable(std::move(msg)); messageAvailable(std::move(msg));
destroyedFlagPtr_ = nullptr; destroyedFlagPtr_ = nullptr;
RequestContext::setContext(old_ctx);
// If the callback was destroyed before it returned, we are done // If the callback was destroyed before it returned, we are done
if (callbackDestroyed) { if (callbackDestroyed) {
return; return;
......
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