From 733f4bca81d14da8af70cef61da74f3cab813503 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 30 Oct 2025 15:56:10 +0000 Subject: [PATCH 1/2] mitogen: Allow line comments in first stage, strip them. --- mitogen/parent.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mitogen/parent.py b/mitogen/parent.py index 6e30b1c6..71641d36 100644 --- a/mitogen/parent.py +++ b/mitogen/parent.py @@ -1463,8 +1463,9 @@ class Connection(object): return [self.options.python_path] def get_boot_command(self): - source = inspect.getsource(self._first_stage) - source = textwrap.dedent('\n'.join(source.strip().split('\n')[2:])) + lines = inspect.getsourcelines(self._first_stage)[0][2:] + # Remove line comments, leading indentation, trailing newline + source = textwrap.dedent(''.join(s for s in lines if '#' not in s))[:-1] source = source.replace(' ', ' ') compressor = zlib.compressobj( zlib.Z_BEST_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS, From 606a21fb2765a44b66a2eb299fd454bbe07f510d Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Tue, 9 Dec 2025 13:46:57 +0000 Subject: [PATCH 2/2] mitogen: Add first line comments to _first_stage() --- mitogen/parent.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mitogen/parent.py b/mitogen/parent.py index 71641d36..e91504ce 100644 --- a/mitogen/parent.py +++ b/mitogen/parent.py @@ -1415,7 +1415,6 @@ class Connection(object): # W: write side of interpreter stdin. # r: read side of core_src FD. # w: write side of core_src FD. - # C: the decompressed core source. # Final os.close(STDOUT_FILENO) to avoid --py-debug build corrupting stream with # "[1234 refs]" during exit. @@ -1437,8 +1436,12 @@ class Connection(object): os.environ['ARGV0']=sys.executable os.execl(sys.executable,sys.executable+'(mitogen:%s)'%sys.argv[2]) os.write(1,'MITO000\n'.encode()) + # Read `len(compressed preamble)` bytes sent by our Mitogen parent. + # `select()` handles non-blocking stdin (e.g. sudo + log_output). + # `C` accumulates compressed bytes. C=''.encode() while int(sys.argv[3])-len(C)and select.select([0],[],[]):C+=os.read(0,int(sys.argv[3])-len(C)) + # Raises `zlib.error` if compressed preamble is truncated or invalid C=zlib.decompress(C) f=os.fdopen(W,'wb',0) f.write(C)