Let keep-alive tokens from VirtualEventBase be destroyed from any thread
authorYedidya Feldblum <yfeldblum@fb.com>
Wed, 4 Oct 2017 06:50:27 +0000 (23:50 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 4 Oct 2017 07:07:18 +0000 (00:07 -0700)
Summary:
[Folly] Let keep-alive tokens from `VirtualEventBase` be destroyed from any thread.

This case was missed in {D5951397} (facebook/folly@6f3b7616f3402355441c62ca60a36159435aa818).

Reviewed By: andriigrynenko

Differential Revision: D5970670

fbshipit-source-id: 19e2b00134516e68113234acd6111e21c3e23e8d

folly/io/async/VirtualEventBase.h

index 4e62fb121ba649cef20ca990a06c2909d0c7746b..068e649fe666a1f9645e67a6af9de378f64a225e 100644 (file)
@@ -118,7 +118,6 @@ class VirtualEventBase : public folly::Executor, public folly::TimeoutManager {
 
   /**
    * Returns you a handle which prevents VirtualEventBase from being destroyed.
-   * KeepAlive handle can be released from EventBase loop only.
    */
   KeepAlive getKeepAliveToken() override {
     DCHECK(loopKeepAliveCount_ + loopKeepAliveCountAtomic_.load() > 0);
@@ -137,7 +136,9 @@ class VirtualEventBase : public folly::Executor, public folly::TimeoutManager {
 
  protected:
   void keepAliveRelease() override {
-    getEventBase().dcheckIsInEventBaseThread();
+    if (!getEventBase().inRunningEventBaseThread()) {
+      return getEventBase().add([=] { keepAliveRelease(); });
+    }
     if (loopKeepAliveCountAtomic_.load()) {
       loopKeepAliveCount_ += loopKeepAliveCountAtomic_.exchange(0);
     }