|
| 1 | +"""Minimal reproduction of multiprocessing Manager + fork failure.""" |
| 2 | + |
| 3 | +import multiprocessing |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import time |
| 7 | +import traceback |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +pytestmark = pytest.mark.skipif(not hasattr(os, "fork"), reason="requires os.fork") |
| 12 | + |
| 13 | + |
| 14 | +def test_basic_manager(): |
| 15 | + """Test Manager without fork - does it work at all?""" |
| 16 | + print("=== Test 1: Basic Manager (no fork) ===") |
| 17 | + ctx = multiprocessing.get_context("fork") |
| 18 | + manager = ctx.Manager() |
| 19 | + try: |
| 20 | + ev = manager.Event() |
| 21 | + print(f" Event created: {ev}") |
| 22 | + ev.set() |
| 23 | + print(f" Event set, is_set={ev.is_set()}") |
| 24 | + assert ev.is_set() |
| 25 | + print(" PASS") |
| 26 | + finally: |
| 27 | + manager.shutdown() |
| 28 | + |
| 29 | + |
| 30 | +def test_manager_with_process(): |
| 31 | + """Test Manager shared between parent and child process.""" |
| 32 | + print("\n=== Test 2: Manager with forked child ===") |
| 33 | + ctx = multiprocessing.get_context("fork") |
| 34 | + manager = ctx.Manager() |
| 35 | + try: |
| 36 | + result = manager.Value("i", 0) |
| 37 | + ev = manager.Event() |
| 38 | + |
| 39 | + def child_fn(): |
| 40 | + try: |
| 41 | + ev.set() |
| 42 | + result.value = 42 |
| 43 | + except Exception as e: |
| 44 | + print(f" CHILD ERROR: {e}", file=sys.stderr) |
| 45 | + traceback.print_exc() |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + print(f" Starting child process...") |
| 49 | + process = ctx.Process(target=child_fn) |
| 50 | + process.start() |
| 51 | + print(f" Waiting for child (pid={process.pid})...") |
| 52 | + process.join(timeout=10) |
| 53 | + |
| 54 | + if process.exitcode != 0: |
| 55 | + print(f" FAIL: child exited with code {process.exitcode}") |
| 56 | + return False |
| 57 | + |
| 58 | + print(f" Child done. result={result.value}, event={ev.is_set()}") |
| 59 | + assert result.value == 42 |
| 60 | + assert ev.is_set() |
| 61 | + print(" PASS") |
| 62 | + return True |
| 63 | + finally: |
| 64 | + manager.shutdown() |
| 65 | + |
| 66 | + |
| 67 | +def test_manager_server_alive_after_fork(): |
| 68 | + """Test that Manager server survives after forking a child.""" |
| 69 | + print("\n=== Test 3: Manager server alive after fork ===") |
| 70 | + ctx = multiprocessing.get_context("fork") |
| 71 | + manager = ctx.Manager() |
| 72 | + try: |
| 73 | + ev = manager.Event() |
| 74 | + |
| 75 | + # Fork a child that does nothing with the manager |
| 76 | + pid = os.fork() |
| 77 | + if pid == 0: |
| 78 | + # Child - exit immediately |
| 79 | + os._exit(0) |
| 80 | + |
| 81 | + # Parent - wait for child |
| 82 | + os.waitpid(pid, 0) |
| 83 | + |
| 84 | + # Now try to use the manager in the parent |
| 85 | + print(f" After fork, trying to use Manager in parent...") |
| 86 | + ev.set() |
| 87 | + print(f" ev.is_set() = {ev.is_set()}") |
| 88 | + assert ev.is_set() |
| 89 | + print(" PASS") |
| 90 | + return True |
| 91 | + finally: |
| 92 | + manager.shutdown() |
| 93 | + |
| 94 | + |
| 95 | +def test_manager_server_alive_after_fork_with_child_usage(): |
| 96 | + """Test that Manager server survives when child also uses it.""" |
| 97 | + print("\n=== Test 4: Manager server alive after fork + child usage ===") |
| 98 | + ctx = multiprocessing.get_context("fork") |
| 99 | + manager = ctx.Manager() |
| 100 | + try: |
| 101 | + child_ev = manager.Event() |
| 102 | + parent_ev = manager.Event() |
| 103 | + |
| 104 | + def child_fn(): |
| 105 | + try: |
| 106 | + child_ev.set() |
| 107 | + except Exception as e: |
| 108 | + print(f" CHILD ERROR: {e}", file=sys.stderr) |
| 109 | + traceback.print_exc() |
| 110 | + sys.exit(1) |
| 111 | + |
| 112 | + process = ctx.Process(target=child_fn) |
| 113 | + process.start() |
| 114 | + process.join(timeout=10) |
| 115 | + |
| 116 | + if process.exitcode != 0: |
| 117 | + print(f" FAIL: child exited with code {process.exitcode}") |
| 118 | + return False |
| 119 | + |
| 120 | + # Now use manager in parent AFTER child is done |
| 121 | + print(f" Child done. Trying parent usage...") |
| 122 | + parent_ev.set() |
| 123 | + print(f" child_ev={child_ev.is_set()}, parent_ev={parent_ev.is_set()}") |
| 124 | + assert child_ev.is_set() |
| 125 | + assert parent_ev.is_set() |
| 126 | + print(" PASS") |
| 127 | + return True |
| 128 | + finally: |
| 129 | + manager.shutdown() |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + test_basic_manager() |
| 134 | + |
| 135 | + passed = 0 |
| 136 | + total = 10 |
| 137 | + for i in range(total): |
| 138 | + print(f"\n--- Iteration {i + 1}/{total} ---") |
| 139 | + ok = True |
| 140 | + ok = ok and test_manager_with_process() |
| 141 | + ok = ok and test_manager_server_alive_after_fork() |
| 142 | + ok = ok and test_manager_server_alive_after_fork_with_child_usage() |
| 143 | + if ok: |
| 144 | + passed += 1 |
| 145 | + else: |
| 146 | + print(f" FAILED on iteration {i + 1}") |
| 147 | + |
| 148 | + print(f"\n=== Results: {passed}/{total} passed ===") |
| 149 | + sys.exit(0 if passed == total else 1) |
0 commit comments