unseen: bound the per-repo Python parse phase (--parse-deadline)

Git subprocess calls were deadline-bounded (Git.command_timeout uses the
remaining per-repo budget), but a pure-Python parse between git calls
(repository_changes -> history/blame/diff on a giant file) had no
deadline: when it hung, no further git call fired the git deadline, so
the worker stalled indefinitely (observed >720s with zero git
processes). An external watchdog was papering over it at ~20 min/stall.

Add extract.process_deadline(seconds): a SIGALRM/setitimer wall-clock
alarm installed on the worker's main thread that raises
ExtractionTimeout("wallclock_timeout"), with no-op guards (None/<=0, off
the main thread, or no SIGALRM support) and strict timer-cancel +
handler-restore in finally. Wrap the whole extract_repository body in it
so the timeout propagates to the existing `except source.Rejected` and
journals as a clean rejection, not a worker failure.

Budget = --extract-deadline (git, 240s) + --parse-deadline (new, 120s)
= 360s total ceiling, so git-bound repos keep their full budget (yield
unchanged) while a hung parse is rejected well before any external
watchdog. Record parse_deadline_seconds in the report.

295 unseen tests pass (incl. timeout + handler-restore + no-op cases),
ruff clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
Marius Ciepluch
2026-10-10 14:46:18 +02:00
co-authored by Claude Opus 4.8
parent c4c08d1ca1
commit a49b15b720
4 changed files with 201 additions and 7 deletions
+68
View File
@@ -1,8 +1,11 @@
"""Offline extraction: an injected local Git client over a synthetic temporary history."""
import importlib
import signal
import subprocess
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from dataclasses import replace
from pathlib import Path
@@ -13,6 +16,71 @@ e = importlib.import_module("benchmarks.unseen.extract")
s = importlib.import_module("benchmarks.unseen.source")
@pytest.mark.parametrize("slow", [False, True])
def test_process_deadline_restores_alarm(slow):
original = signal.getsignal(signal.SIGALRM)
assert signal.getitimer(signal.ITIMER_REAL) == (0, 0)
if slow:
with pytest.raises(e.ExtractionTimeout, match="^wallclock_timeout$"), e.process_deadline(0.05):
time.sleep(0.2)
else:
with e.process_deadline(0.2):
assert signal.getitimer(signal.ITIMER_REAL)[0] > 0
assert signal.getsignal(signal.SIGALRM) == original
assert signal.getitimer(signal.ITIMER_REAL) == (0, 0)
@pytest.mark.parametrize("seconds", [None, 0, -1])
def test_process_deadline_disabled(seconds, monkeypatch):
monkeypatch.setattr(signal, "signal", lambda *args: pytest.fail("disabled deadline installed a handler"))
monkeypatch.setattr(signal, "setitimer", lambda *args: pytest.fail("disabled deadline armed a timer"))
with e.process_deadline(seconds):
pass
def test_process_deadline_off_main_thread(monkeypatch):
monkeypatch.setattr(signal, "signal", lambda *args: pytest.fail("thread installed a handler"))
monkeypatch.setattr(signal, "setitimer", lambda *args: pytest.fail("thread armed a timer"))
def work():
with e.process_deadline(0.01):
time.sleep(0.02)
return "finished"
with ThreadPoolExecutor(max_workers=1) as pool:
assert pool.submit(work).result() == "finished"
@pytest.mark.parametrize("operation", ["signal", "setitimer"])
def test_process_deadline_unsupported_operation(operation, monkeypatch):
original = signal.getsignal(signal.SIGALRM)
def unsupported(*args):
raise ValueError("unsupported")
monkeypatch.setattr(signal, operation, unsupported)
with e.process_deadline(0.01):
time.sleep(0.02)
assert signal.getsignal(signal.SIGALRM) == original
assert signal.getitimer(signal.ITIMER_REAL) == (0, 0)
def test_process_deadline_does_not_swallow_body_value_error():
original = signal.getsignal(signal.SIGALRM)
with pytest.raises(ValueError, match="body failure"), e.process_deadline(0.2):
raise ValueError("body failure")
assert signal.getsignal(signal.SIGALRM) == original
assert signal.getitimer(signal.ITIMER_REAL) == (0, 0)
def test_process_deadline_inside_git_remains_rejection(tmp_path):
def runner(*args, **kwargs):
time.sleep(0.2)
with pytest.raises(e.ExtractionTimeout, match="^wallclock_timeout$"), e.process_deadline(0.05):
e.Git(tmp_path, runner=runner).run("log")
@pytest.fixture
def history(tmp_path):
calls = []