Skip to content

Commit 03a0a15

Browse files
committed
mitogen: Kill (hung) bootstrap processes after 5 second timeout
Since using select.select() in the first stage (to handle an obscure corner case where stdin appears to be non-blocking) there has been a report of first stage processes running for ever in an infinite loop - reading 0 bytes from stdin. This attempts to do an end run around that problem by aborting if the bootstrap takes longer than a few seconds for *any* reason. Existing retry logic should deal with it as before. 5 seconds is a best guess at a suitable timeout.
1 parent 82a0efc commit 03a0a15

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ To avail of fixes in an unreleased version, please download a ZIP file
2121
In progress (unreleased)
2222
------------------------
2323

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

2528
v0.3.34 (2025-11-27)
2629
--------------------

mitogen/parent.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,10 @@ def _first_stage():
14361436
if os.uname()[0]+os.uname()[2][:2]+sys.version[:3]=='Darwin212.7':os.environ['PYTHON_LAUNCHED_FROM_WRAPPER']='1'
14371437
os.environ['ARGV0']=sys.executable
14381438
os.execl(sys.executable,sys.executable+'(mitogen:%s)'%sys.argv[2])
1439+
# Timeout execution after a few seconds, to prevent hung processes.
1440+
# Cause might be closed/reset pipe/stream backing stdin (fd=0);
1441+
# or blocking write to interpreter stdin (fd=W, fd=w).
1442+
signal.alarm(5)
14391443
os.write(1,'MITO000\n'.encode())
14401444
C=''.encode()
14411445
while int(sys.argv[3])-len(C)and select.select([0],[],[]):C+=os.read(0,int(sys.argv[3])-len(C))
@@ -1476,11 +1480,11 @@ def get_boot_command(self):
14761480
# Just enough to decode, decompress, and exec the first stage.
14771481
# Priorities: wider compatibility, faster startup, shorter length.
14781482
# `sys.path=...` for https://github.com/python/cpython/issues/115911.
1479-
# `import os,select` here (not stage 1) to save a few bytes overall.
1483+
# `import os,...` here (not stage 1) saves a few bytes overall.
14801484
return self.get_python_argv() + [
14811485
'-c',
14821486
'import sys;sys.path=[p for p in sys.path if p];'
1483-
'import binascii,os,select,zlib;'
1487+
'import binascii,os,select,signal,zlib;'
14841488
'exec(zlib.decompress(binascii.a2b_base64(sys.argv[1]),-15))',
14851489
encoded.decode(),
14861490
self.options.remote_name,

0 commit comments

Comments
 (0)