Skip to content

Commit a168c24

Browse files
committed
mitogen: Report child Python version to parent during bootstrap
1 parent c6d6ea8 commit a168c24

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

mitogen/core.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def set_blocking(fd, blocking):
170170

171171
PY24 = sys.version_info < (2, 5)
172172
PY3 = sys.version_info > (3,)
173-
if PY3:
173+
if sys.version_info >= (3, 0):
174174
import pickle
175175
import _thread as thread
176176
from io import BytesIO
@@ -237,6 +237,13 @@ def set_blocking(fd, blocking):
237237
#: writing small trailer chunks.
238238
CHUNK_SIZE = 131072
239239

240+
#: Markers sent by bootstrap stages when it's ready to receive the next stage,
241+
#: e.g. compressed :class:`mitogen.core`.
242+
EC0 = b('MITO000')
243+
EC1 = b('MITO001')
244+
EC2 = b('MITO002 %d %d' % sys.version_info[0:2])
245+
EC2_PATTERN = b(r'MITO002 (\d) (\d\d?)')
246+
240247
_tls = threading.local()
241248

242249

@@ -4304,7 +4311,7 @@ def main(self):
43044311
_v and LOG.debug('Recovered sys.executable: %r', sys.executable)
43054312

43064313
if self.config.get('send_ec2', True):
4307-
self.stream.transmit_side.write(b('MITO002\n'))
4314+
self.stream.transmit_side.write(EC2 + b('\n'))
43084315
self.broker._py24_25_compat()
43094316
self.log_handler.uncork()
43104317
self.dispatcher.run()

mitogen/fork.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def _child_main(self, childfp):
213213
self.options.on_fork()
214214
mitogen.core.set_blocking(childfp.fileno(), True)
215215

216-
childfp.send(b('MITO002\n'))
216+
childfp.send(mitogen.core.EC2 + b('\n'))
217217

218218
# Expected by the ExternalContext.main().
219219
os.dup2(childfp.fileno(), 1)

mitogen/parent.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,18 +1186,8 @@ def on_unrecognized_partial_line_received(self, line):
11861186

11871187
class BootstrapProtocol(RegexProtocol):
11881188
"""
1189-
Respond to stdout of a child during bootstrap. Wait for :attr:`EC0_MARKER`
1190-
to be written by the first stage to indicate it can receive the bootstrap,
1191-
then await :attr:`EC1_MARKER` to indicate success, and
1192-
:class:`MitogenProtocol` can be enabled.
1193-
"""
1194-
#: Sentinel value emitted by the first stage to indicate it is ready to
1195-
#: receive the compressed bootstrap. For :mod:`mitogen.ssh` this must have
1196-
#: length of at least `max(len('password'), len('debug1:'))`
1197-
EC0_MARKER = b('MITO000')
1198-
EC1_MARKER = b('MITO001')
1199-
EC2_MARKER = b('MITO002')
1200-
1189+
Respond to readiness markers sent to parent by child doing bootstrap.
1190+
"""
12011191
def __init__(self, broker):
12021192
super(BootstrapProtocol, self).__init__()
12031193
self._writer = mitogen.core.BufferedWriter(broker, self)
@@ -1213,7 +1203,11 @@ def _on_ec1_received(self, line, match):
12131203
LOG.debug('%r: first stage received mitogen.core source', self)
12141204

12151205
def _on_ec2_received(self, line, match):
1216-
LOG.debug('%r: new child booted successfully', self)
1206+
py_major, py_minor = int(match.group(1)), int(match.group(2))
1207+
LOG.debug(
1208+
'%r: new child booted successfully on Python %d.%d',
1209+
self, py_major, py_minor,
1210+
)
12171211
self.stream.conn._complete_connection()
12181212
return False
12191213

@@ -1222,9 +1216,9 @@ def on_unrecognized_line_received(self, line):
12221216
line.decode('utf-8', 'replace'))
12231217

12241218
PATTERNS = [
1225-
(re.compile(EC0_MARKER), _on_ec0_received),
1226-
(re.compile(EC1_MARKER), _on_ec1_received),
1227-
(re.compile(EC2_MARKER), _on_ec2_received),
1219+
(re.compile(mitogen.core.EC0), _on_ec0_received),
1220+
(re.compile(mitogen.core.EC1), _on_ec1_received),
1221+
(re.compile(mitogen.core.EC2_PATTERN), _on_ec2_received),
12281222
]
12291223

12301224

tests/first_stage_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def test_valid_syntax(self):
312312
stdout, stderr = proc.communicate()
313313
self.assertEqual(0, proc.returncode)
314314
self.assertEqual(stdout,
315-
mitogen.parent.BootstrapProtocol.EC0_MARKER+b('\n'))
315+
mitogen.core.EC0 + b('\n'))
316316
self.assertIn(
317317
b("Error -3 while decompressing data"), # Unknown compression method
318318
stderr,
@@ -356,7 +356,7 @@ def test_premature_eof(self):
356356
self.assertEqual(0, returncode)
357357
self.assertEqual(
358358
proc.stdout.read(),
359-
mitogen.parent.BootstrapProtocol.EC0_MARKER + b("\n"),
359+
mitogen.core.EC0 + b("\n"),
360360
)
361361
self.assertIn(
362362
b("Error -5 while decompressing data"),

0 commit comments

Comments
 (0)