]> git.ipfire.org Git - thirdparty/tornado.git/commit
asyncio: Manage the selector thread with an async generator 3272/head
authorBen Darnell <ben@bendarnell.com>
Wed, 17 May 2023 00:57:50 +0000 (20:57 -0400)
committerBen Darnell <ben@bendarnell.com>
Mon, 19 Jun 2023 17:39:59 +0000 (13:39 -0400)
commit3cca32d593263922030060b7f4781bcb1367ec2a
tree7dbfa235de133945e9b8321b1ad4af0b5dff1ea2
parentf28b245306b204e5ff312973b994fba82a3af01c
asyncio: Manage the selector thread with an async generator

Async generators have a special shutdown protocol which allows
us to detect the end of the event loop and stop our thread.
This lets us clean up the thread reliably when the event loop
is started/stopped via the tornado IOLoop interfaces (which
explicitly know about the selector thread), or when the
latest asyncio interfaces are used (asyncio.run or manually
calling shutdown_asyncgens).

The thread is still leaked when older versions of the asyncio
interfaces are used (loop.close *without* shutdown_asyncgens), but
I've been unable to find a solution that does not print leak warnings
even in the event of a clean shutdown. Use of shutdown_asyncgens is
now effectively required for apps combining asyncio and tornado.
This is unfortunate since leaking a thread is relatively expensive
compared to the usual consequences of failing to call
shutdown_asyncgens, but it seems to be the best we can do.

Fixes #3173
tornado/platform/asyncio.py
tornado/test/asyncio_test.py