folly/futures: fix early release of non-embedded callbacks
[folly.git] / folly / futures / detail / Core.h
index 05cde93721acc868e50ae9d546e280148523c91e..7516885264c2c68deff2e6dd17fb74f6cb9315c1 100644 (file)
@@ -335,12 +335,14 @@ class Core {
           x->add([this]() mutable {
             SCOPE_EXIT { detachOne(); };
             RequestContext::setContext(context_);
+            SCOPE_EXIT { callback_ = {}; };
             callback_(std::move(*result_));
           });
         } else {
           x->addWithPriority([this]() mutable {
             SCOPE_EXIT { detachOne(); };
             RequestContext::setContext(context_);
+            SCOPE_EXIT { callback_ = {}; };
             callback_(std::move(*result_));
           }, priority);
         }
@@ -348,10 +350,12 @@ class Core {
         --attached_; // Account for extra ++attached_ before try
         RequestContext::setContext(context_);
         result_ = Try<T>(exception_wrapper(std::current_exception()));
+        SCOPE_EXIT { callback_ = {}; };
         callback_(std::move(*result_));
       }
     } else {
       RequestContext::setContext(context_);
+      SCOPE_EXIT { callback_ = {}; };
       callback_(std::move(*result_));
     }
   }