]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
avoid thread unsafe iteration of _ioloop_for_asyncio (#3125)
authorThomas Grainger <tagrain@gmail.com>
Fri, 18 Mar 2022 20:18:22 +0000 (20:18 +0000)
committerGitHub <noreply@github.com>
Fri, 18 Mar 2022 20:18:22 +0000 (16:18 -0400)
* avoid thread unsafe iteration of _ioloop_for_asyncio

See https://twitter.com/raymondh/status/1252759650224619521

* catch KeyError from potential concurrent _ioloop_for_asyncio deletes

tornado/platform/asyncio.py

index cc11adc5aaf929b561c84703e07ef03be30aa31c..f80c42dc26e4c74bfda0175541c081ba73175f81 100644 (file)
@@ -123,9 +123,12 @@ class BaseAsyncIOLoop(IOLoop):
         # TODO(bdarnell): consider making self.asyncio_loop a weakref
         # for AsyncIOMainLoop and make _ioloop_for_asyncio a
         # WeakKeyDictionary.
-        for loop in list(IOLoop._ioloop_for_asyncio):
+        for loop in IOLoop._ioloop_for_asyncio.copy():
             if loop.is_closed():
-                del IOLoop._ioloop_for_asyncio[loop]
+                try:
+                    del IOLoop._ioloop_for_asyncio[loop]
+                except KeyError:
+                    pass
         IOLoop._ioloop_for_asyncio[asyncio_loop] = self
 
         self._thread_identity = 0