Skip to content

Commit d475316

Browse files
Tornado selector thread copy main thread context
1 parent 0b7e700 commit d475316

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

tornado/platform/asyncio.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import asyncio
2626
import atexit
2727
import concurrent.futures
28+
import contextvars
2829
import errno
2930
import functools
3031
import select
@@ -472,6 +473,8 @@ class SelectorThread:
472473
_closed = False
473474

474475
def __init__(self, real_loop: asyncio.AbstractEventLoop) -> None:
476+
self._main_thread_ctx = contextvars.copy_context()
477+
475478
self._real_loop = real_loop
476479

477480
self._select_cond = threading.Condition()
@@ -491,7 +494,7 @@ async def thread_manager_anext() -> None:
491494
# clean up if we get to this point but the event loop is closed without
492495
# starting.
493496
self._real_loop.call_soon(
494-
lambda: self._real_loop.create_task(thread_manager_anext())
497+
lambda: self._real_loop.create_task(thread_manager_anext()), context=self._main_thread_ctx
495498
)
496499

497500
self._readers: Dict[_FileDescriptorLike, Callable] = {}
@@ -618,7 +621,7 @@ def _run_select(self) -> None:
618621
raise
619622

620623
try:
621-
self._real_loop.call_soon_threadsafe(self._handle_select, rs, ws)
624+
self._real_loop.call_soon_threadsafe(self._handle_select, rs, ws, context=self._main_thread_ctx)
622625
except RuntimeError:
623626
# "Event loop is closed". Swallow the exception for
624627
# consistency with PollIOLoop (and logical consistency

0 commit comments

Comments
 (0)