From 1d2d4f326acc0825690c151c38ac92d146b78146 Mon Sep 17 00:00:00 2001 From: Mirek Klimos Date: Fri, 11 Nov 2016 11:15:55 -0800 Subject: [PATCH] Add warnings when overriding RequestContext Summary: We currently store the previous value on creation of RequestContextScopeGuard and reset it to the previous one when it goes out of scope - this means that if it's not unset properly somewhere, we'll prolong its life by saving it and restoring later. We need to do this because of some edge cases but a RequestContext should generally never be overridden by a different RequestContext - let's add a warning if this happens Reviewed By: djwatson, palmtenor Differential Revision: D3971904 fbshipit-source-id: e72b5f01102d18977d8aa5f8dca5a7802545098a --- folly/io/async/Request.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/folly/io/async/Request.h b/folly/io/async/Request.h index 67f220d4..a87ec733 100644 --- a/folly/io/async/Request.h +++ b/folly/io/async/Request.h @@ -107,16 +107,26 @@ class RequestContextScopeGuard { std::shared_ptr prev_; public: + RequestContextScopeGuard(const RequestContextScopeGuard&) = delete; + RequestContextScopeGuard& operator=(const RequestContextScopeGuard&) = delete; + RequestContextScopeGuard(RequestContextScopeGuard&&) = delete; + RequestContextScopeGuard& operator=(RequestContextScopeGuard&&) = delete; + // Create a new RequestContext and reset to the original value when // this goes out of scope. RequestContextScopeGuard() : prev_(RequestContext::saveContext()) { + DLOG_IF(INFO, prev_ != nullptr) + << "Overriding folly::RequestContext - did you mean to unset it first?"; RequestContext::create(); } // Set a RequestContext that was previously captured by saveContext(). It will // be automatically reset to the original value when this goes out of scope. explicit RequestContextScopeGuard(std::shared_ptr ctx) - : prev_(RequestContext::setContext(std::move(ctx))) {} + : prev_(RequestContext::setContext(std::move(ctx))) { + DLOG_IF(INFO, prev_ != nullptr && prev_.get() != RequestContext::get()) + << "Overriding folly::RequestContext - did you mean to unset it first?"; + } ~RequestContextScopeGuard() { RequestContext::setContext(std::move(prev_)); -- 2.34.1