Skip to content
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ To avail of fixes in an unreleased version, please download a ZIP file
In progress (unreleased)
------------------------

* :gh:issue:`1266` :mod:`mitogen`: Prevent hung bootstrap processes, add 5
second timeout to first stage


v0.3.34 (2025-11-27)
--------------------
Expand Down
13 changes: 9 additions & 4 deletions mitogen/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,10 @@ def _first_stage():
if os.uname()[0]+os.uname()[2][:2]+sys.version[:3]=='Darwin212.7':os.environ['PYTHON_LAUNCHED_FROM_WRAPPER']='1'
os.environ['ARGV0']=sys.executable
os.execl(sys.executable,sys.executable+'(mitogen:%s)'%sys.argv[2])
# Timeout execution after a few seconds, to prevent hung processes.
# Cause might be closed/reset pipe/stream backing stdin (fd=0);
# or blocking write to interpreter stdin (fd=W, fd=w).
signal.alarm(5)
os.write(1,'MITO000\n'.encode())
C=''.encode()
while int(sys.argv[3])-len(C)and select.select([0],[],[]):C+=os.read(0,int(sys.argv[3])-len(C))
Expand Down Expand Up @@ -1463,8 +1467,9 @@ def get_python_argv(self):
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,
Expand All @@ -1475,11 +1480,11 @@ def get_boot_command(self):
# Just enough to decode, decompress, and exec the first stage.
# Priorities: wider compatibility, faster startup, shorter length.
# `sys.path=...` for https://github.com/python/cpython/issues/115911.
# `import os,select` here (not stage 1) to save a few bytes overall.
# `import os,...` here (not stage 1) saves a few bytes overall.
return self.get_python_argv() + [
'-c',
'import sys;sys.path=[p for p in sys.path if p];'
'import binascii,os,select,zlib;'
'import binascii,os,select,signal,zlib;'
'exec(zlib.decompress(binascii.a2b_base64(sys.argv[1]),-15))',
encoded.decode(),
self.options.remote_name,
Expand Down