| """Fork-based crash isolation with a preloaded registry. |
| |
| Problem: importing the 188-substrate registry costs ~85s, and a mutant that |
| triggers an illegal memory access poisons the CUDA context so unrecoverably |
| that the process must exit. Paying the import per problem (for isolation) wastes |
| most of the runtime. |
| |
| Solution: the PARENT loads the registry once and never touches CUDA. For each |
| unit of work it fork()s a child; the child inherits the registry through |
| copy-on-write (no reload, no serialization), initializes CUDA itself, does the |
| work, and exits. If the child dies — poisoned context, watchdog kill, OOM — |
| the parent is unaffected and forks the next one. |
| |
| Constraint: the parent must NOT initialize CUDA (a CUDA context cannot be |
| inherited across fork), so keep all torch.cuda calls inside run_fn. |
| """ |
| import os |
| import sys |
|
|
|
|
| def run_isolated(run_fn, *args, timeout=None): |
| """Run run_fn(*args) in a forked child. Returns the child's exit code. |
| |
| 0 = completed; 3 = the runner's "context poisoned, respawn me" signal; |
| other = crash/kill. The parent never shares CUDA state with the child. |
| """ |
| pid = os.fork() |
| if pid == 0: |
| code = 0 |
| try: |
| run_fn(*args) |
| except SystemExit as e: |
| code = e.code if isinstance(e.code, int) else 1 |
| except Exception: |
| import traceback |
| traceback.print_exc() |
| code = 1 |
| finally: |
| sys.stdout.flush() |
| sys.stderr.flush() |
| os._exit(code) |
| if timeout is None: |
| _, status = os.waitpid(pid, 0) |
| return os.waitstatus_to_exitcode(status) |
| |
| import time |
| deadline = time.monotonic() + timeout |
| while True: |
| done, status = os.waitpid(pid, os.WNOHANG) |
| if done: |
| return os.waitstatus_to_exitcode(status) |
| if time.monotonic() > deadline: |
| os.kill(pid, 9) |
| os.waitpid(pid, 0) |
| return 124 |
| time.sleep(0.5) |
|
|
|
|
| def isolated_loop(problems, run_fn, label="run", timeout=2400, retries=6): |
| """Process problems one at a time, each in its own forked child. |
| |
| The registry stays resident in the parent, so a crash costs only the work |
| in flight — never a re-import. |
| """ |
| for p in problems: |
| for attempt in range(retries): |
| rc = run_isolated(run_fn, p, timeout=timeout) |
| if rc == 0: |
| break |
| if rc == 124: |
| print(f"[{label}] {p}: TIMEOUT (child killed)", flush=True) |
| break |
| print(f"[{label}] {p}: done", flush=True) |
|
|