]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Prevent multiple IOLoops for one asyncio loop
authorMin RK <benjaminrk@gmail.com>
Thu, 16 Jun 2022 07:55:47 +0000 (09:55 +0200)
committerMin RK <benjaminrk@gmail.com>
Thu, 16 Jun 2022 08:20:39 +0000 (10:20 +0200)
tornado/platform/asyncio.py
tornado/test/ioloop_test.py

index 3b3678155bec935ff970b400a1b65a2f4a7714be..c35955ccfd449669ea4227570ed5e7bca7177cf8 100644 (file)
@@ -129,6 +129,14 @@ class BaseAsyncIOLoop(IOLoop):
                     del IOLoop._ioloop_for_asyncio[loop]
                 except KeyError:
                     pass
+
+        # Make sure we don't already have an IOLoop for this asyncio loop
+        if asyncio_loop in IOLoop._ioloop_for_asyncio:
+            existing_loop = IOLoop._ioloop_for_asyncio[asyncio_loop]
+            raise RuntimeError(
+                f"IOLoop {existing_loop} already associated with asyncio loop {asyncio_loop}"
+            )
+
         IOLoop._ioloop_for_asyncio[asyncio_loop] = self
 
         self._thread_identity = 0
index 73ca3254712ff9d685d1b2684baa4408395900a9..557ebc3757ec3f4dcf65cff7c957ae38f3aeddf1 100644 (file)
@@ -432,6 +432,9 @@ class TestIOLoop(AsyncTestCase):
         asyncio_loop = asyncio.new_event_loop()
         loop = IOLoop(asyncio_loop=asyncio_loop, make_current=False)
         assert loop.asyncio_loop is asyncio_loop  # type: ignore
+        with self.assertRaises(RuntimeError):
+            # Can't register two IOLoops with the same asyncio_loop
+            IOLoop(asyncio_loop=asyncio_loop, make_current=False)
         loop.close()