Skip to content

Commit caa0706

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 a4d5f8c commit caa0706

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

0 commit comments

Comments
 (0)