From df8a88f5415e2385e7c77bdcc0579de86f779c4a Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 3 Sep 2021 13:40:25 +0200 Subject: [PATCH] swallow "Event loop is closed" from selector thread same catch as in `add_callback` for the same reasons - the selector thread may race with an asyncio_loop.close in the main thread seen running tests with pytest-asyncio --- tornado/platform/asyncio.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tornado/platform/asyncio.py b/tornado/platform/asyncio.py index 5e9c776d0..d312c3d90 100644 --- a/tornado/platform/asyncio.py +++ b/tornado/platform/asyncio.py @@ -576,7 +576,21 @@ class AddThreadSelectorEventLoop(asyncio.AbstractEventLoop): raise else: raise - self._real_loop.call_soon_threadsafe(self._handle_select, rs, ws) + + try: + self._real_loop.call_soon_threadsafe(self._handle_select, rs, ws) + except RuntimeError: + # "Event loop is closed". Swallow the exception for + # consistency with PollIOLoop (and logical consistency + # with the fact that we can't guarantee that an + # add_callback that completes without error will + # eventually execute). + pass + except AttributeError: + # ProactorEventLoop may raise this instead of RuntimeError + # if call_soon_threadsafe races with a call to close(). + # Swallow it too for consistency. + pass def _handle_select( self, rs: List["_FileDescriptorLike"], ws: List["_FileDescriptorLike"] -- 2.47.2