Skip to content

Fixed the bug that the handler cannot get the main thread context on the Windows platform. #3479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tornado/platform/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import asyncio
import atexit
import concurrent.futures
import contextvars
import errno
import functools
import select
Expand Down Expand Up @@ -472,6 +473,8 @@ class SelectorThread:
_closed = False

def __init__(self, real_loop: asyncio.AbstractEventLoop) -> None:
self._main_thread_ctx = contextvars.copy_context()

self._real_loop = real_loop

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

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

try:
self._real_loop.call_soon_threadsafe(self._handle_select, rs, ws)
self._real_loop.call_soon_threadsafe(self._handle_select, rs, ws, context=self._main_thread_ctx)
except RuntimeError:
# "Event loop is closed". Swallow the exception for
# consistency with PollIOLoop (and logical consistency
Expand Down
Loading