Commit 36dbf002 authored by Misha Shneerson's avatar Misha Shneerson Committed by Facebook GitHub Bot

Fix ASAN violations when shutting down FiberManager from python

Reviewed By: andriigrynenko

Differential Revision: D26497717

fbshipit-source-id: 35307fce7f9e85882f8f9e5a6bdfe651122727a3
parent 1d97d5b6
......@@ -102,7 +102,7 @@ FiberManager::~FiberManager() {
fibersPool_.pop_front_and_dispose([](Fiber* fiber) { delete fiber; });
}
assert(readyFibers_.empty());
assert(fibersActive_ == 0);
assert(!hasTasks());
}
LoopController& FiberManager::loopController() {
......
......@@ -13,6 +13,7 @@
# limitations under the License.
from libcpp.memory cimport unique_ptr
from libcpp cimport bool
from folly.executor cimport cAsyncioExecutor
cdef extern from "folly/fibers/LoopController.h" namespace "folly::fibers":
......@@ -24,7 +25,7 @@ cdef extern from "folly/fibers/FiberManagerInternal.h" namespace "folly::fibers"
pass
cdef cppclass cFiberManager "folly::fibers::FiberManager":
cFiberManager(unique_ptr[cLoopController], const cFiberManagerOptions&)
void loopUntilNoReady()
bool hasTasks() const
cdef extern from "folly/fibers/ExecutorLoopController.h" namespace "folly::fibers":
cdef cppclass cAsyncioLoopController "folly::fibers::ExecutorLoopController"(cLoopController):
......
......@@ -16,6 +16,10 @@ import asyncio
from cython.operator cimport dereference as deref
from libcpp.memory cimport unique_ptr
from folly.executor cimport get_executor
from libcpp.cast cimport (
dynamic_cast,
)
from folly.executor cimport cAsyncioExecutor
from folly.fiber_manager cimport (
cFiberManager,
cLoopController,
......@@ -29,17 +33,22 @@ loop_to_controller = WeakKeyDictionary()
cdef class FiberManager:
cdef unique_ptr[cFiberManager] cManager
cdef cAsyncioExecutor* cExecutor
# Lazy constructor, as __cinit__ doesn't support C types
cdef init(self, const cFiberManagerOptions& opts):
self.cExecutor = get_executor()
self.cManager.reset(new cFiberManager(
unique_ptr[cLoopController](new cAsyncioLoopController(
get_executor())),
self.cExecutor)),
opts));
def __dealloc__(FiberManager self):
while deref(self.cManager).hasTasks():
self.cExecutor.drive()
# drive one last time
deref(self.cManager).loopUntilNoReady()
self.cExecutor.drive()
# Explicitly reset here, otherwise it is possible
# that self.cManager dstor runs after python finalizes
# Cython deletes these after __dealloc__ returns.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment