[issue41619] Subprocesses created with DETACHED_PROCESS can pop up a console window

Eryk Sun report at bugs.python.org
Sun Aug 23 10:14:32 EDT 2020


Eryk Sun <eryksun at gmail.com> added the comment:

Creating the py.exe process with creationflags=DETACHED_PROCESS sets a special ConsoleHandle value in its ProcessParameters that makes the base API skip allocating a console session at process startup. However, the launcher itself spawns python.exe normally, without the DETACHED_PROCESS flag. So the base API in the python.exe process allocates a new console session that creates a window.

Using creationflags=CREATE_NO_WINDOW resolves the problem. This flag makes the base API in the py.exe process allocate a new console session that doesn't create a window. The child python.exe process inherits this windowless console session. Note that CREATE_NO_WINDOW is ignored if combined with CREATE_NEW_CONSOLE or DETACHED_PROCESS, since the combination makes no sense.

You can also use creationflags=CREATE_NEW_CONSOLE with startupinfo=STARTUPINFO(dwFlags=STARTF_USESHOWWINDOW), in which case py.exe allocates a console session with a hidden window. This option is useful if a console application should be able to show the console window later on via ShowWindow(GetConsoleWindow(), SW_SHOW). With CREATE_NO_WINDOW, in contrast, there is no window to show.

It should work if creationflags=DETACHED_PROCESS is combined with startupinfo=STARTUPINFO(dwFlags=STARTF_USESHOWWINDOW) because the launcher is supposed to clone its startup information to the child python.exe process, including dwFlags. But there's a bug in run_child in PC/launcher.c. It sets si.dwFlags to STARTF_USESTDHANDLES instead of using a bitwise OR to set the flag value.

----------
nosy: +eryksun

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41619>
_______________________________________


More information about the Python-bugs-list mailing list