Skip to content

Create new, hidden window when starting daemon on Windows #5994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions mypy/dmypy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,31 @@
MEM_PROFILE = False # type: Final # If True, dump memory profile after initialization

if sys.platform == 'win32':
from subprocess import STARTUPINFO

def daemonize(options: Options,
timeout: Optional[int] = None,
log_file: Optional[str] = None) -> int:
"""Create the daemon process via "dmypy daemon" and pass options via command line

This uses the DETACHED_PROCESS flag to invoke the Server.
See https://docs.microsoft.com/en-us/windows/desktop/procthread/process-creation-flags
When creating the daemon grandchild, we create it in a new console, which is
started hidden. We cannot use DETACHED_PROCESS since it will cause console windows
to pop up when starting. See
https://github.com/python/cpython/pull/4150#issuecomment-340215696
for more on why we can't have nice things.

It also pickles the options to be unpickled by mypy.
"""
command = [sys.executable, '-m', 'mypy.dmypy', 'daemon']
pickeled_options = pickle.dumps((options.snapshot(), timeout, log_file))
command.append('--options-data="{}"'.format(base64.b64encode(pickeled_options).decode()))
info = STARTUPINFO(dwFlags=0x1, # STARTF_USESHOWWINDOW aka use wShowWindow's value
wShowWindow=0, # SW_HIDE aka make the window invisible
)
try:
subprocess.Popen(command, creationflags=0x8) # DETACHED_PROCESS
subprocess.Popen(command,
creationflags=0x10, # CREATE_NEW_CONSOLE
startupinfo=info)
return 0
except subprocess.CalledProcessError as e:
return e.returncode
Expand Down