|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Helpers for tests that spawn ``multiprocessing.Process`` children. |
| 5 | +
|
| 6 | +These exist primarily to defend IPC tests against a class of CI hang where a |
| 7 | +child process spawns too slowly and the parent does not implement proper guards |
| 8 | +for that (see issue #2004). Without intervention, a zombie child holds an IPC |
| 9 | +memory handle and blocks the parent's ``mr.close()`` in fixture teardown, |
| 10 | +leading to deadlock and wedging the test runner for hours. |
| 11 | +""" |
| 12 | + |
| 13 | +import contextlib |
| 14 | +import multiprocessing.process |
| 15 | +import weakref |
| 16 | + |
| 17 | +from cuda_python_test_helpers import under_compute_sanitizer |
| 18 | + |
| 19 | +CHILD_TIMEOUT_SEC_DEFAULT = 30 |
| 20 | +CHILD_TIMEOUT_SEC_SANITIZER = 120 |
| 21 | + |
| 22 | + |
| 23 | +def child_timeout_sec() -> int: |
| 24 | + """Return the per-process join/wait timeout for IPC-style tests. |
| 25 | +
|
| 26 | + Compute-sanitizer significantly slows process startup and CUDA context |
| 27 | + teardown, so we use a larger budget when it is active. |
| 28 | + """ |
| 29 | + return CHILD_TIMEOUT_SEC_SANITIZER if under_compute_sanitizer() else CHILD_TIMEOUT_SEC_DEFAULT |
| 30 | + |
| 31 | + |
| 32 | +def kill_subprocesses(*processes): |
| 33 | + """Kill any of the given Process objects that are still alive. |
| 34 | +
|
| 35 | + Returns the list of processes that were killed (i.e. that were still alive |
| 36 | + when the call was made). Callers should ``assert not survivors`` to convert |
| 37 | + a non-empty return value into a clean test failure, e.g.:: |
| 38 | +
|
| 39 | + proc_a.join(timeout=CHILD_TIMEOUT_SEC) |
| 40 | + proc_b.join(timeout=CHILD_TIMEOUT_SEC) |
| 41 | + survivors = kill_subprocesses(proc_a, proc_b) |
| 42 | + assert not survivors, f"timed out waiting on: {[p.name for p in survivors]}" |
| 43 | + assert proc_a.exitcode == 0 |
| 44 | + assert proc_b.exitcode == 0 |
| 45 | +
|
| 46 | + Killing survivors before the subsequent asserts prevents a zombie child |
| 47 | + from holding IPC handles past the test body and blocking fixture |
| 48 | + teardown. |
| 49 | + """ |
| 50 | + killed = [] |
| 51 | + for proc in processes: |
| 52 | + try: |
| 53 | + alive = proc.is_alive() |
| 54 | + except (ValueError, AssertionError): |
| 55 | + # is_alive() raises if the Process was never started or has |
| 56 | + # already been closed; nothing to clean up. |
| 57 | + continue |
| 58 | + if not alive: |
| 59 | + continue |
| 60 | + with contextlib.suppress(ValueError, AssertionError): |
| 61 | + proc.kill() |
| 62 | + proc.join() |
| 63 | + killed.append(proc) |
| 64 | + return killed |
| 65 | + |
| 66 | + |
| 67 | +@contextlib.contextmanager |
| 68 | +def track_child_processes(): |
| 69 | + """Context manager that kills any ``multiprocessing.Process`` children still |
| 70 | + alive at exit. |
| 71 | +
|
| 72 | + Patches ``multiprocessing.process.BaseProcess.__init__`` to record every |
| 73 | + ``Process`` instance constructed inside the ``with`` block. This covers |
| 74 | + the delegating ``mp.Process`` class as well as direct ``SpawnProcess`` / |
| 75 | + ``ForkProcess`` instances (including those created by ``mp.Pool``), since |
| 76 | + all of them inherit from ``BaseProcess``. On exit, any tracked process |
| 77 | + that is still alive is killed and joined. |
| 78 | +
|
| 79 | + This protects fixture teardown (e.g. ``ipc_memory_resource``'s |
| 80 | + ``mr.close()``) from blocking on IPC handles held by a stuck child -- |
| 81 | + see issue #2004. |
| 82 | + """ |
| 83 | + tracked = weakref.WeakSet() |
| 84 | + base = multiprocessing.process.BaseProcess |
| 85 | + original_init = base.__init__ |
| 86 | + |
| 87 | + def tracking_init(self, *args, **kwargs): |
| 88 | + original_init(self, *args, **kwargs) |
| 89 | + tracked.add(self) |
| 90 | + |
| 91 | + base.__init__ = tracking_init |
| 92 | + try: |
| 93 | + yield |
| 94 | + finally: |
| 95 | + base.__init__ = original_init |
| 96 | + for proc in list(tracked): |
| 97 | + # is_alive() / kill() raise ValueError if the Process was never |
| 98 | + # started or has already been closed; nothing to clean up in that |
| 99 | + # case. |
| 100 | + with contextlib.suppress(ValueError): |
| 101 | + if proc.is_alive(): |
| 102 | + proc.kill() |
| 103 | + proc.join() |
0 commit comments