From: Min RK Date: Thu, 16 Jun 2022 07:55:47 +0000 (+0200) Subject: Prevent multiple IOLoops for one asyncio loop X-Git-Tag: v6.2.0b2~3^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=44e28cf32ca120189026b55395587415e731f905;p=thirdparty%2Ftornado.git Prevent multiple IOLoops for one asyncio loop --- diff --git a/tornado/platform/asyncio.py b/tornado/platform/asyncio.py index 3b3678155..c35955ccf 100644 --- a/tornado/platform/asyncio.py +++ b/tornado/platform/asyncio.py @@ -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 diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index 73ca32547..557ebc375 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -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()