diff --git a/trio_asyncio/_async.py b/trio_asyncio/_async.py index c5e6f0e..827582f 100644 --- a/trio_asyncio/_async.py +++ b/trio_asyncio/_async.py @@ -10,7 +10,8 @@ class TrioEventLoop(BaseTrioEventLoop): """ def _queue_handle(self, handle): - self._check_closed() + if self.is_closed(): + raise RuntimeError("Event loop is closed") self._q_send.send_nowait(handle) return handle diff --git a/trio_asyncio/_base.py b/trio_asyncio/_base.py index 77b0009..2a098ae 100644 --- a/trio_asyncio/_base.py +++ b/trio_asyncio/_base.py @@ -283,7 +283,8 @@ async def run_aio_coroutine(self, coro): """ try: - self._check_closed() + if self.is_closed(): + raise RuntimeError("Event loop is closed") fut = asyncio.ensure_future(coro, loop=self) except BaseException: coro.close() # avoid unawaited coroutine error @@ -393,7 +394,8 @@ def call_soon_threadsafe(self, callback, *args, **context): Note that the callback is a sync function. """ self._check_callback(callback, "call_soon_threadsafe") - self._check_closed() + if self.is_closed(): + raise RuntimeError("Event loop is closed") h = asyncio.Handle(callback, args, self, **context) self._token.run_sync_soon(self._q_send.send_nowait, h) @@ -485,7 +487,8 @@ def run_in_executor(self, executor, func, *args): Returns an asyncio.Future. """ self._check_callback(func, "run_in_executor") - self._check_closed() + if self.is_closed(): + raise RuntimeError("Event loop is closed") if executor is None: # pragma: no branch executor = self._default_executor assert isinstance(executor, TrioExecutor) @@ -515,7 +518,8 @@ def _handle_sig(self, sig, _): def add_signal_handler(self, sig, callback, *args): """asyncio's method to add a signal handler.""" - self._check_closed() + if self.is_closed(): + raise RuntimeError("Event loop is closed") self._check_signal(sig) if sig == signal.SIGKILL: raise RuntimeError("SIGKILL cannot be caught") @@ -559,7 +563,8 @@ def add_reader(self, fd, callback, *args): # Local helper to factor out common logic between _add_reader/_add_writer def _add_io_handler(self, set_handle, wait_ready, fd, callback, args): - self._check_closed() + if self.is_closed(): + raise RuntimeError("Event loop is closed") handle = ScopedHandle(callback, args, self) old_handle = set_handle(fd, handle) diff --git a/trio_asyncio/_sync.py b/trio_asyncio/_sync.py index bdec005..44079f8 100644 --- a/trio_asyncio/_sync.py +++ b/trio_asyncio/_sync.py @@ -78,7 +78,8 @@ def do_stop(): self._queue_handle(asyncio.Handle(do_stop, (), self)) def _queue_handle(self, handle): - self._check_closed() + if self.is_closed(): + raise RuntimeError("Event loop is closed") if self._glet is not greenlet.getcurrent() and self._token is not None: self.__run_in_greenlet(_sync, self._q_send.send_nowait, handle) else: @@ -168,7 +169,8 @@ def is_done(_): return result.unwrap() def __run_in_greenlet(self, async_fn, *args): - self._check_closed() + if self.is_closed(): + raise RuntimeError("Event loop is closed") if self._loop_running: raise RuntimeError( "You can't nest calls to run_until_complete()/run_forever()."