|
1 | 1 | import contextlib
|
| 2 | +import queue |
| 3 | +import signal |
2 | 4 | import sys
|
3 | 5 | import time
|
4 | 6 | import unittest
|
| 7 | +import unittest.mock |
5 | 8 | from pickle import PicklingError
|
6 | 9 | from concurrent import futures
|
7 |
| -from concurrent.futures.process import BrokenProcessPool |
| 10 | +from concurrent.futures.process import BrokenProcessPool, _ThreadWakeup |
8 | 11 |
|
9 | 12 | from test import support
|
10 | 13 |
|
@@ -241,6 +244,73 @@ def test_crash_big_data(self):
|
241 | 244 |
|
242 | 245 | executor.shutdown(wait=True)
|
243 | 246 |
|
| 247 | + def test_gh105829_should_not_deadlock_if_wakeup_pipe_full(self): |
| 248 | + # Issue #105829: The _ExecutorManagerThread wakeup pipe could |
| 249 | + # fill up and block. See: https://github.com/python/cpython/issues/105829 |
| 250 | + |
| 251 | + # Lots of cargo culting while writing this test, apologies if |
| 252 | + # something is really stupid... |
| 253 | + |
| 254 | + self.executor.shutdown(wait=True) |
| 255 | + |
| 256 | + if not hasattr(signal, 'alarm'): |
| 257 | + raise unittest.SkipTest( |
| 258 | + "Tested platform does not support the alarm signal") |
| 259 | + |
| 260 | + def timeout(_signum, _frame): |
| 261 | + import faulthandler |
| 262 | + faulthandler.dump_traceback() |
| 263 | + |
| 264 | + raise RuntimeError("timed out while submitting jobs?") |
| 265 | + |
| 266 | + thread_run = futures.process._ExecutorManagerThread.run |
| 267 | + def mock_run(self): |
| 268 | + # Delay thread startup so the wakeup pipe can fill up and block |
| 269 | + time.sleep(3) |
| 270 | + thread_run(self) |
| 271 | + |
| 272 | + class MockWakeup(_ThreadWakeup): |
| 273 | + """Mock wakeup object to force the wakeup to block""" |
| 274 | + def __init__(self): |
| 275 | + super().__init__() |
| 276 | + self._dummy_queue = queue.Queue(maxsize=1) |
| 277 | + |
| 278 | + def wakeup(self): |
| 279 | + self._dummy_queue.put(None, block=True) |
| 280 | + super().wakeup() |
| 281 | + |
| 282 | + def clear(self): |
| 283 | + try: |
| 284 | + while True: |
| 285 | + self._dummy_queue.get_nowait() |
| 286 | + except queue.Empty: |
| 287 | + super().clear() |
| 288 | + |
| 289 | + with (unittest.mock.patch.object(futures.process._ExecutorManagerThread, |
| 290 | + 'run', mock_run), |
| 291 | + unittest.mock.patch('concurrent.futures.process._ThreadWakeup', |
| 292 | + MockWakeup)): |
| 293 | + with self.executor_type(max_workers=2, |
| 294 | + mp_context=self.get_context()) as executor: |
| 295 | + self.executor = executor # Allow clean up in fail_on_deadlock |
| 296 | + |
| 297 | + job_num = 100 |
| 298 | + job_data = range(job_num) |
| 299 | + |
| 300 | + # Need to use sigalarm for timeout detection because |
| 301 | + # Executor.submit is not guarded by any timeout (both |
| 302 | + # self._work_ids.put(self._queue_count) and |
| 303 | + # self._executor_manager_thread_wakeup.wakeup() might |
| 304 | + # timeout, maybe more?). In this specific case it was |
| 305 | + # the wakeup call that deadlocked on a blocking pipe. |
| 306 | + old_handler = signal.signal(signal.SIGALRM, timeout) |
| 307 | + try: |
| 308 | + signal.alarm(int(self.TIMEOUT)) |
| 309 | + self.assertEqual(job_num, len(list(executor.map(int, job_data)))) |
| 310 | + finally: |
| 311 | + signal.alarm(0) |
| 312 | + signal.signal(signal.SIGALRM, old_handler) |
| 313 | + |
244 | 314 |
|
245 | 315 | create_executor_tests(globals(), ExecutorDeadlockTest,
|
246 | 316 | executor_mixins=(ProcessPoolForkMixin,
|
|
0 commit comments