You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Alias launchers (python3.exe in the aliases folder): child Popen fails with WinError 6 after fd 0 is swapped and restored #445
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 --refresh doesn'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)
importosimportsubprocessimportsysfails=0foriinrange(20):
saved=os.dup(0)
withopen(os.devnull, "rb") asnull:
os.dup2(null.fileno(), 0) # swap stdin, as pytest's fd capture doesos.dup2(saved, 0) # and swap it backos.close(saved)
try:
subprocess.run(["cmd", "/c", "exit"], stdout=subprocess.PIPE)
exceptOSErrorase:
fails+=1print(f"round {i}: {e!r}")
print(f"{sys.executable}: {fails} of 20 rounds failed")
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) and GetStdHandle agree for fds 0-2, and all three
handles pass GetHandleInformation, through the alias as well.
In the failing case, at the moment Popen raises, GetStdHandle(STD_INPUT_HANDLE) fails GetHandleInformation with error 6, so Popen (which duplicates the inherited stdin for STARTF_USESTDHANDLES when 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.