Repository navigation
Description
Summary
A Python started through one of the install manager's alias launchers (for example
%LOCALAPPDATA%\Python\bin\python3.exe) intermittently can't start subprocesses:
subprocess.run(..., stdout=PIPE) raises OSError: [WinError 6] The handle is invalid
once stdin (fd 0) has been swapped with os.dup2 and swapped back. pytest's fd-level
capture does this around every test, so any test suite that starts subprocesses with
partial redirection fails at random when run through the alias. The same runtime started
directly, or through py.exe, never fails.
Environment
- Windows 11 Pro 10.0.26200, x64
- Python install manager 26.4.240.0 (also seen with the aliases written by 25.2;
pymanager install --refreshdoesn't change it; 26.5b1 not tried) - Runtime: PythonCore 3.14.3 (64-bit), installed by the manager
- Same result from PowerShell 5.1, and from Git Bash
Repro (repro.py)
import os
import subprocess
import sys
fails = 0
for i in range(20):
saved = os.dup(0)
with open(os.devnull, "rb") as null:
os.dup2(null.fileno(), 0) # swap stdin, as pytest's fd capture does
os.dup2(saved, 0) # and swap it back
os.close(saved)
try:
subprocess.run(["cmd", "/c", "exit"], stdout=subprocess.PIPE)
except OSError as e:
fails += 1
print(f"round {i}: {e!r}")
print(f"{sys.executable}: {fails} of 20 rounds failed")Results (from PowerShell)
| Started as | Rounds failed |
|---|---|
& "$env:LOCALAPPDATA\Python\bin\python3.exe" repro.py |
14 of 20 |
& "$env:LOCALAPPDATA\Python\pythoncore-3.14-64\python.exe" repro.py |
0 of 20 |
py -3.14 repro.py |
0 of 20 |
A longer variant that also swaps fds 1 and 2 to a temp file each round fails in exactly
every other round (25 of 50, three runs out of three, last retested with 3.14.3) through the
alias, and 0 of 150 directly. Swapping only fds 1 and 2, without fd 0, fails 0 times either way.
What we checked
- The environment the child sees is the same both ways (only
_differs). - The process isn't in a job object.
- At startup,
msvcrt.get_osfhandle(n)andGetStdHandleagree for fds 0-2, and all three
handles passGetHandleInformation, through the alias as well. - In the failing case, at the moment
Popenraises,GetStdHandle(STD_INPUT_HANDLE)fails
GetHandleInformationwith error 6, soPopen(which duplicates the inherited stdin for
STARTF_USESTDHANDLESwhen only stdout is redirected) gets an invalid handle.
Expected
A runtime started through an alias behaves like the same runtime started directly or through
py.exe.
Workaround
Pass stdin=subprocess.DEVNULL (the repro with it: 0 of 60 rounds failed through the alias),
or start the runtime's python.exe directly, or via py.
Cause
The alias launcher links the C runtime statically and runs Python in-process through python3.dll's
Py_Main, so the shared UCRT that Python uses never has its app type set (_query_app_type() returns 0
through the alias, 1 through python.exe). With an unknown app type, the UCRT doesn't update the process's
standard handles when os.dup2 replaces fds 0-2, so GetStdHandle(STD_INPUT_HANDLE) keeps returning the
closed handle (every other round, as the handle value is reused). Calling _set_app_type on ucrtbase.dll
in launch_by_dll, before Py_Main (console, or GUI for the windowed launcher), fixes it: the repro fails
14 of 20 with a launcher built from main and 0 of 20 with the change. I'll open a PR.
Activity
zooba commentedon Nov 21, 2026
This is super interesting to know! Thanks for digging it up.
gh-445: Set the UCRT's app type in the alias launcher before calling …