Skip to content

gh-445: Set the UCRT's app type in the alias launcher before calling Py_Main - #446

Open
CandyRealDandy wants to merge 1 commit into
python:mainfrom
CandyRealDandy:launcher-app-type
Open

CandyRealDandy wants to merge 1 commit into
python:mainfrom
CandyRealDandy:launcher-app-type

Conversation

@CandyRealDandy

Copy link
Copy Markdown

Fixes #445.

The alias launcher links the C runtime statically and, for CPython, runs Py_Main in-process through python3.dll. Its own startup code sets the app type only in its static CRT, so the shared UCRT that Python uses stays at _crt_unknown_app (_query_app_type() returns 0 through the alias, 1 through python.exe). With an unknown app type the UCRT doesn't call SetStdHandle when os.dup2 replaces fds 0-2, so after pytest-style fd capture swaps stdin and back, GetStdHandle(STD_INPUT_HANDLE) returns the closed handle, and Popen with inherited stdin fails with WinError 6.

This sets the app type on ucrtbase.dll in launch_by_dll, before calling Py_Main: _crt_console_app for the console launcher, _crt_gui_app for the windowed one, as python.exe / pythonw.exe's own startup would. PY_WINDOWED gets a default of 0 in launcher.cpp, as in main.cpp.

Tested on Windows 11 x64 with PythonCore 3.14.3, the launcher built with MSVC (/MT, console subsystem) from main and from this branch, run as an alias (python3.exe + python3.exe.__target__):

Launcher Repro from #445 (3 runs) _query_app_type() stdin invalid after swap
main 14 of 20 failed, each run 0 7 of 10
this branch 0 of 20 failed, each run 1 0 of 10

No new compiler warnings (/std:c++20 /W4: the same 7 existing ones in launcher.cpp both ways). There's no existing test that runs the built launcher, so I haven't added one; happy to add an end-to-end check to the workflow if you'd like.

…lling Py_Main

The launcher links the C runtime statically, so its startup code sets the app
type only in its own copy. Python, loaded in-process through python3.dll, uses
the shared UCRT, whose app type stays unknown. The UCRT then doesn't update the
process's standard handles when Python swaps fds 0-2 with os.dup2 (as pytest's
fd capture does), GetStdHandle keeps returning a closed handle, and a subprocess
that inherits stdin fails with WinError 6.

Set it to console (or GUI for the windowed launcher) before calling Py_Main, as
python.exe's own startup would.
@python-cla-bot

python-cla-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@zooba

zooba commented Nov 21, 2026

Copy link
Copy Markdown
Member

Very good to know about this. Once CI passes (which I assume it will), I'll merge, but since I'm about to be away for the weekend that'll be a couple of days. If another core dev wants to merge, they can go ahead, otherwise I'll see it when I'm back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alias launchers (python3.exe in the aliases folder): child Popen fails with WinError 6 after fd 0 is swapped and restored

2 participants