remove_event_loop: Allow ignoring specific errors

Replaces a bare except with Exception, and updates the
test case to specify only a single exception that is
allowed to occur.

Related to https://github.com/capnproto/pycapnp/issues/254
This commit is contained in:
John Vandenberg
2021-06-01 16:27:34 +08:00
parent bd91c52d57
commit 4797654323
2 changed files with 28 additions and 7 deletions

View File

@@ -1843,16 +1843,26 @@ cpdef remove_event_loop(ignore_errors=False):
if C_DEFAULT_EVENT_LOOP: if C_DEFAULT_EVENT_LOOP:
try: try:
C_DEFAULT_EVENT_LOOP._remove() C_DEFAULT_EVENT_LOOP._remove()
except: except Exception as e:
if not ignore_errors: if isinstance(ignore_errors, Exception):
if isinstance(e, ignore_errors):
ignore_errors = True
if ignore_errors is True:
pass
else:
raise raise
C_DEFAULT_EVENT_LOOP = None C_DEFAULT_EVENT_LOOP = None
if len(_THREAD_LOCAL_EVENT_LOOPS) > 0: if len(_THREAD_LOCAL_EVENT_LOOPS) > 0:
for loop in _THREAD_LOCAL_EVENT_LOOPS: for loop in _THREAD_LOCAL_EVENT_LOOPS:
try: try:
loop._remove() loop._remove()
except: except Exception as e:
if not ignore_errors: if isinstance(ignore_errors, Exception):
if isinstance(e, ignore_errors):
ignore_errors = True
if ignore_errors is True:
pass
else:
raise raise
_THREAD_LOCAL_EVENT_LOOPS = [] _THREAD_LOCAL_EVENT_LOOPS = []
_C_DEFAULT_EVENT_LOOP_LOCAL = None _C_DEFAULT_EVENT_LOOP_LOCAL = None

View File

@@ -9,6 +9,9 @@ import threading
import pytest import pytest
import capnp import capnp
from capnp.lib.capnp import KjException
import test_capability_capnp import test_capability_capnp
@@ -35,11 +38,19 @@ def test_making_threaded_event_loop():
''' '''
Threaded event loop test Threaded event loop test
''' '''
capnp.remove_event_loop(True) # The following raises a KjException, and if not caught causes an SIGABRT:
capnp.create_event_loop(True) # kj/async.c++:973: failed: expected head == nullptr; EventLoop destroyed with events still in the queue.
# Memory leak?; head->trace() = kj::_::ForkHub<kj::_::Void>
# kj::_::AdapterPromiseNode<kj::_::Void, kj::_::PromiseAndFulfillerAdapter<void> >
# stack: ...
# python(..) malloc: *** error for object 0x...: pointer being freed was not allocated
# python(..) malloc: *** set a breakpoint in malloc_error_break to debug
# Fatal Python error: Aborted
capnp.remove_event_loop(KjException)
capnp.create_event_loop(KjException)
capnp.remove_event_loop() capnp.remove_event_loop()
capnp.create_event_loop(True) capnp.create_event_loop(KjException)
class Server(test_capability_capnp.TestInterface.Server): class Server(test_capability_capnp.TestInterface.Server):