From a619885cc5a6e5d08cf6552bbaa99216210e5fb7 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 21 Jun 2022 08:41:38 +0100 Subject: [PATCH] make existing_loop check atomic --- tornado/platform/asyncio.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tornado/platform/asyncio.py b/tornado/platform/asyncio.py index ad1bde093b..909cd78776 100644 --- a/tornado/platform/asyncio.py +++ b/tornado/platform/asyncio.py @@ -131,14 +131,12 @@ def initialize( # type: ignore 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] + existing_loop = IOLoop._ioloop_for_asyncio.setdefault(asyncio_loop, self) + if existing_loop is not self: raise RuntimeError( f"IOLoop {existing_loop} already associated with asyncio loop {asyncio_loop}" ) - IOLoop._ioloop_for_asyncio[asyncio_loop] = self - self._thread_identity = 0 super().initialize(**kwargs)