| 21 | |
| 22 | @contextmanager |
| 23 | def cleanup_processes(processes: list[BaseProcess]) -> Generator[None]: |
| 24 | try: |
| 25 | yield |
| 26 | finally: |
| 27 | for process in processes: |
| 28 | # Terminating an unstarted process raises over the assertion that escaped before it started. |
| 29 | if process.pid is not None: |
| 30 | process.terminate() |
| 31 | process.join(timeout=_REAP_DEADLINE) |
| 32 | if process.is_alive(): # pragma: no cover # SIGTERM only loses this race on a saturated runner |
| 33 | process.kill() |
| 34 | process.join(timeout=_REAP_DEADLINE) |
| 35 | # Left open, the finalizer runs during whichever test the collector lands in, as an unraisable exception. |
| 36 | process.close() |
| 37 | |
| 38 | |
| 39 | def break_then_hang_after_exchange( |