|
1 | 1 | import subprocess |
| 2 | +import sys |
2 | 3 |
|
3 | 4 | import testlib |
4 | 5 |
|
@@ -152,3 +153,41 @@ def test_eof_too_early(self): |
152 | 153 | finally: |
153 | 154 | proc.stdout.close() |
154 | 155 | 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