Repository navigation
fix: avoid importing asyncio on Windows subprocess platform (#3551) #3561
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
Closed
Tikbaan
wants to merge
2
commits into
python-trio:main
from
Tikbaan:fix/avoid-asyncio-import-on-windows-subprocess-3551
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Avoid importing ``asyncio`` on Windows by implementing ``windows_pipe`` directly with ``_winapi``, reducing startup latency. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,91 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import _winapi | ||
| import itertools | ||
| import os | ||
| import tempfile | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from .._wait_for_object import WaitForSingleObject | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .. import _subprocess | ||
|
|
||
| _pipe_counter = itertools.count() | ||
|
|
||
|
|
||
| def windows_pipe( | ||
| *, | ||
| duplex: bool = False, | ||
| overlapped: tuple[bool, bool] = (True, True), | ||
| bufsize: int = 8192, | ||
| ) -> tuple[int, int]: | ||
| """Like os.pipe() but with overlapped support and using handles not fds. | ||
|
|
||
| Adapted from CPython's asyncio.windows_utils.pipe(). | ||
| Copyright (c) 2001-2026 Python Software Foundation; All Rights Reserved. | ||
| Licensed under the Python Software Foundation License Version 2. | ||
|
|
||
| Changes from CPython: | ||
| - Renamed pipe prefix from r'\\.\\pipe\asyncio-pipe-*' to r'\\.\\pipe\trio-pipe-*'. | ||
| - Modernized type hints for Trio's static typing and mypy checks. | ||
| - Explicit exception guard ensures early handle is closed if connection fails. | ||
| """ | ||
| address = tempfile.mktemp( | ||
| prefix=rf"\\.\pipe\trio-pipe-{os.getpid():d}-{next(_pipe_counter):d}-" | ||
| ) | ||
|
|
||
| if duplex: | ||
| openmode = _winapi.PIPE_ACCESS_DUPLEX | ||
| access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE | ||
| obsize, ibsize = bufsize, bufsize | ||
| else: | ||
| openmode = _winapi.PIPE_ACCESS_INBOUND | ||
| access = _winapi.GENERIC_WRITE | ||
| obsize, ibsize = 0, bufsize | ||
|
|
||
| openmode |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE | ||
|
|
||
| if overlapped[0]: | ||
| openmode |= _winapi.FILE_FLAG_OVERLAPPED | ||
|
|
||
| flags_and_attribs = _winapi.FILE_FLAG_OVERLAPPED if overlapped[1] else 0 | ||
|
|
||
| h1: int | None = None | ||
| h2: int | None = None | ||
| try: | ||
| h1 = _winapi.CreateNamedPipe( | ||
| address, | ||
| openmode, | ||
| _winapi.PIPE_WAIT, | ||
| 1, | ||
| obsize, | ||
| ibsize, | ||
| _winapi.NMPWAIT_WAIT_FOREVER, | ||
| _winapi.NULL, | ||
| ) | ||
|
|
||
| h2 = _winapi.CreateFile( | ||
| address, | ||
| access, | ||
| 0, | ||
| _winapi.NULL, | ||
| _winapi.OPEN_EXISTING, | ||
| flags_and_attribs, | ||
| _winapi.NULL, | ||
| ) | ||
|
|
||
| ov = _winapi.ConnectNamedPipe(h1, overlapped=True) | ||
| ov.GetOverlappedResult(True) | ||
| return h1, h2 | ||
| except: | ||
| if h1 is not None: | ||
| _winapi.CloseHandle(h1) | ||
| if h2 is not None: | ||
| _winapi.CloseHandle(h2) | ||
| raise | ||
|
|
||
|
|
||
| async def wait_child_exiting(process: "_subprocess.Process") -> None: | ||
| async def wait_child_exiting(process: _subprocess.Process) -> None: | ||
| # _handle is not in Popen stubs, though it is present on Windows. | ||
| await WaitForSingleObject(int(process._proc._handle)) # type: ignore[attr-defined] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,25 @@ | ||
| def test_trio_import() -> None: | ||
| import sys | ||
|
|
||
| for module in list(sys.modules.keys()): | ||
| if module.startswith("trio"): | ||
| del sys.modules[module] | ||
|
|
||
| import trio # noqa: F401 | ||
| from __future__ import annotations | ||
|
|
||
| import subprocess | ||
| import sys | ||
|
|
||
|
|
||
| def test_trio_import() -> None: | ||
| for module in list(sys.modules.keys()): | ||
| if module.startswith("trio"): | ||
| del sys.modules[module] | ||
|
|
||
| import trio # noqa: F401 | ||
|
|
||
|
|
||
| def test_trio_import_does_not_import_asyncio() -> None: | ||
| res = subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| "-c", | ||
| "import sys, trio; assert 'asyncio' not in sys.modules, f'unexpected asyncio modules: {[m for m in sys.modules if \"asyncio\" in m]}'", | ||
| ], | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
| assert res.returncode == 0, f"import trio loaded asyncio:\n{res.stderr}" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's... not rely on
_winapi:-)