Skip to content

The agent that grows with you

fix(pm): stream bootstrap output on Windows Python 3.11

ethernetb22c8add1446
diff --git a/pm/environment.py b/pm/environment.py
index 9f93e5a5525..674976bb157 100644
--- a/pm/environment.py
+++ b/pm/environment.py
@@ -99,6 +99,24 @@ def prune_site_pth(venv_dir: Path) -> None:
pass
+def _read_pipe(fd: int) -> bytes:
+ size = 65536
+ if sys.platform == "win32":
+ import _winapi
+ import msvcrt
+
+ # Bootstrap can run on Python 3.11, before Windows os.set_blocking
+ # exists. Peek keeps the sole reader bounded without changing pipe mode.
+ try:
+ available, _ = _winapi.PeekNamedPipe(msvcrt.get_osfhandle(fd), 0)
+ except BrokenPipeError:
+ return b""
+ if not available:
+ raise BlockingIOError
+ size = min(size, available)
+ return os.read(fd, size)
+
+
def _run_streaming(command: list[str], *, cwd: Path, env: dict[str, str],
timeout: int, output: TextIO) -> subprocess.CompletedProcess:
"""Keep CI progress live, a bounded diagnostic tail, and a wall-clock timeout."""
@@ -112,8 +130,8 @@ def _run_streaming(command: list[str], *, cwd: Path, env: dict[str, str],
try:
# A descendant can keep stdout open after proc exits. Nonblocking reads
# bound that drain without leaving a thread stuck in readline()/close().
- # PM's Python >=3.14 supports nonblocking pipes on Windows as well as POSIX.
- os.set_blocking(pipe.fileno(), False)
+ if sys.platform != "win32":
+ os.set_blocking(pipe.fileno(), False)
decoder = io.IncrementalNewlineDecoder(
codecs.getincrementaldecoder(pipe.encoding)(errors="replace"), translate=True,
)
@@ -122,7 +140,7 @@ def _run_streaming(command: list[str], *, cwd: Path, env: dict[str, str],
if remaining <= 0:
raise subprocess.TimeoutExpired(command, timeout, stderr=tail)
try:
- data = os.read(pipe.fileno(), 65536)
+ data = _read_pipe(pipe.fileno())
except BlockingIOError:
time.sleep(min(.05, remaining))
continue
diff --git a/tests/pm/test_bootstrap_import_closure.py b/tests/pm/test_bootstrap_import_closure.py
index 5117e10f433..5b24464460c 100644
--- a/tests/pm/test_bootstrap_import_closure.py
+++ b/tests/pm/test_bootstrap_import_closure.py
@@ -6,6 +6,8 @@ import shutil
import subprocess
import sys
+import pytest
+
def test_minimal_bootstrap_closure_reaches_pm_paths_and_locks(tmp_path):
repo = Path(__file__).resolve().parents[2]
@@ -54,14 +56,17 @@ assert callable(sign_managed_python)
assert result.returncode == 0, result.stderr
[email protected]("linux", "macos", "windows")
def test_runtime_staging_streams_uv_output_without_tomllib(tmp_path):
- """The PM runtime is staged by whatever python the host has (the Docker
- arm64 image bootstraps from a 3.10 system python); streaming ``uv venv``
- output must not pull in ``pm.workspace``, whose ``tomllib`` import needs 3.11+.
+ """Bootstrap runs before PM selects its Python: Docker has 3.10 and
+ historical Windows updaters have 3.11, without os.set_blocking for pipes.
"""
repo = Path(__file__).resolve().parents[2]
script = """
+import os
import sys
+if sys.platform == 'win32' and hasattr(os, 'set_blocking'):
+ del os.set_blocking
class NoTomllib:
def find_spec(self, fullname, path=None, target=None):
if fullname in ('tomllib', 'pm.workspace', 'pm.plugin_declarations'):