Skip to content

Commit ce2c918

Browse files
authored
Pass log-file and timeout args to daemon as proper args (#15227)
Currently, when starting the mypy daemon, we're passing the timeout and log_file as part of the pickled options. This can cause problems if the daemon has issues with the pickling. In this case we won't be able to log what the problem was. Let's pass the log_file and timeout as first class args. This also has the advantage that we can now start the daemon in the foreground with these args from the command line in a human writeable way.
1 parent 16667f3 commit ce2c918

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

mypy/dmypy/client.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def __init__(self, prog: str) -> None:
244244
p.add_argument(
245245
"--timeout", metavar="TIMEOUT", type=int, help="Server shutdown timeout (in seconds)"
246246
)
247+
p.add_argument("--log-file", metavar="FILE", type=str, help="Direct daemon stdout/stderr to FILE")
247248
p.add_argument(
248249
"flags", metavar="FLAG", nargs="*", type=str, help="Regular mypy flags (precede with --)"
249250
)
@@ -608,21 +609,22 @@ def do_daemon(args: argparse.Namespace) -> None:
608609
# Lazy import so this import doesn't slow down other commands.
609610
from mypy.dmypy_server import Server, process_start_options
610611

612+
if args.log_file:
613+
sys.stdout = sys.stderr = open(args.log_file, "a", buffering=1)
614+
fd = sys.stdout.fileno()
615+
os.dup2(fd, 2)
616+
os.dup2(fd, 1)
617+
611618
if args.options_data:
612619
from mypy.options import Options
613620

614-
options_dict, timeout, log_file = pickle.loads(base64.b64decode(args.options_data))
621+
options_dict = pickle.loads(base64.b64decode(args.options_data))
615622
options_obj = Options()
616623
options = options_obj.apply_changes(options_dict)
617-
if log_file:
618-
sys.stdout = sys.stderr = open(log_file, "a", buffering=1)
619-
fd = sys.stdout.fileno()
620-
os.dup2(fd, 2)
621-
os.dup2(fd, 1)
622624
else:
623625
options = process_start_options(args.flags, allow_sources=False)
624-
timeout = args.timeout
625-
Server(options, args.status_file, timeout=timeout).serve()
626+
627+
Server(options, args.status_file, timeout=args.timeout).serve()
626628

627629

628630
@action(help_parser)

mypy/dmypy_server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ def daemonize(
5656
It also pickles the options to be unpickled by mypy.
5757
"""
5858
command = [sys.executable, "-m", "mypy.dmypy", "--status-file", status_file, "daemon"]
59-
pickled_options = pickle.dumps((options.snapshot(), timeout, log_file))
59+
pickled_options = pickle.dumps(options.snapshot())
6060
command.append(f'--options-data="{base64.b64encode(pickled_options).decode()}"')
61+
if timeout:
62+
command.append(f"--timeout={timeout}")
63+
if log_file:
64+
command.append(f"--log-file={log_file}")
6165
info = STARTUPINFO()
6266
info.dwFlags = 0x1 # STARTF_USESHOWWINDOW aka use wShowWindow's value
6367
info.wShowWindow = 0 # SW_HIDE aka make the window invisible

0 commit comments

Comments
 (0)