From: Mirek Klimos <miro@fb.com>
Date: Fri, 11 Nov 2016 19:15:55 +0000 (-0800)
Subject: Add warnings when overriding RequestContext
X-Git-Tag: v2016.11.14.00~7
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=1d2d4f326acc0825690c151c38ac92d146b78146;p=folly.git

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
---

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<RequestContext> 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<RequestContext> 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_));