Skip to content

Commit a17ee01

Browse files
committed
tests: Add first_stage_test with closed STDIN
Without the next commit, this hangs forever in the select call. Signed-off-by: Marc Hartmayer <[email protected]>
1 parent 2808084 commit a17ee01

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/first_stage_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import subprocess
2+
import sys
23

34
import testlib
45

@@ -152,3 +153,41 @@ def test_eof_too_early(self):
152153
finally:
153154
proc.stdout.close()
154155
proc.stderr.close()
156+
157+
def test_timeout_error(self):
158+
options = mitogen.parent.Options(max_message_size=123)
159+
conn = mitogen.parent.Connection(options, self.router)
160+
conn.context = mitogen.core.Context(None, 123)
161+
args = conn.get_boot_command()
162+
163+
# The boot command should write an ECO marker to stdout, read the
164+
# preamble from stdin, then execute it.
165+
166+
# This test attaches closes stdin to create a specific failure
167+
# 1. Fork child tries to read from STDIN, but fails as it is closed
168+
# 2. Fork child raises TimeoutError
169+
# 3. Fork child's file descriptors (write pipes) are closed by the OS
170+
# 4. Fork parent does `dup(<read pipe>, <stdin>)` and `exec(<python>)`
171+
# 5. Python reads `b''` (i.e. EOF) from stdin (a closed pipe)
172+
# 6. Python runs `''` (a valid script) and exits with success
173+
174+
proc = subprocess.Popen(
175+
args=args,
176+
stdout=subprocess.PIPE,
177+
stderr=subprocess.PIPE,
178+
preexec_fn=sys.stdin.close,
179+
)
180+
try:
181+
try:
182+
stdout, stderr = proc.communicate(timeout=12)
183+
except TypeError:
184+
stdout, stderr = proc.communicate()
185+
except:
186+
proc.kill()
187+
self.fail("Timeout situation was not recognized")
188+
self.assertEqual(0, proc.returncode)
189+
self.assertEqual(stdout, mitogen.parent.BootstrapProtocol.EC0_MARKER + b("\n"))
190+
self.assertIn(
191+
b("TimeoutError"),
192+
stderr,
193+
)

0 commit comments

Comments
 (0)