Make outstanding LoopKeepAlive hold EventBase destructor
[folly.git] / folly / io / async / EventBase.cpp
index 5c0b78129e37a39e9f4da5ecbba45604b9363710..0987f1fac282cce125b41c7bea482e58666b9f37 100644 (file)
@@ -195,6 +195,14 @@ EventBase::EventBase(event_base* evb, bool enableTimeMeasurement)
 }
 
 EventBase::~EventBase() {
+  // Keep looping until all keep-alive handles are released. Each keep-alive
+  // handle signals that some external code will still schedule some work on
+  // this EventBase (so it's not safe to destroy it).
+  while (!loopKeepAlive_.unique()) {
+    applyLoopKeepAlive();
+    loopOnce();
+  }
+
   // Call all destruction callbacks, before we start cleaning up our state.
   while (!onDestructionCallbacks_.empty()) {
     LoopCallback* callback = &onDestructionCallbacks_.front();