|
1 | 1 | import subprocess |
| 2 | +import sys |
2 | 3 |
|
3 | 4 | import mitogen.core |
4 | 5 | import mitogen.parent |
@@ -141,3 +142,41 @@ def test_eof_too_early(self): |
141 | 142 | finally: |
142 | 143 | proc.stdout.close() |
143 | 144 | 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