Add warnings when overriding RequestContext
authorMirek Klimos <miro@fb.com>
Fri, 11 Nov 2016 19:15:55 +0000 (11:15 -0800)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Fri, 11 Nov 2016 19:23:43 +0000 (11:23 -0800)
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

index 67f220d44f23d1c3c294baa974a9a2ea312c6338..a87ec7338b40a655f204ed5ea3ec7d5ecdc91a52 100644 (file)
@@ -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_));