]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
swallow "Event loop is closed" from selector thread 3059/head
authorMin RK <benjaminrk@gmail.com>
Fri, 3 Sep 2021 11:40:25 +0000 (13:40 +0200)
committerMin RK <benjaminrk@gmail.com>
Fri, 3 Sep 2021 11:40:25 +0000 (13:40 +0200)
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

index 5e9c776d02ef00b8e9de8d901438e38a00a89716..d312c3d9067372afb4b931d07da73b01c65c2816 100644 (file)
@@ -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"]