fix(maintenance): recover stale O_EXCL lock files left by crashed holders #792
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.48) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 501 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.372
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #792"
general
weight: 0.35
tonydzi
Fixes #788
Root cause
_file_lock()insrc/memsearch/maintenance.py(pre-fix tree at09e07d8) guards each maintenance task with anO_CREAT | O_EXCLsentinel lock file and removes it only in the holding process's ownfinallyblock. A run that dies hard — SIGKILL, OOM kill, or host exit reaping the session-start hook child — never reaches thatfinally, so the lock file stays behind forever. Every later maintenance wake hits theexcept FileExistsError:branch, which yieldsFalsewithout reading or probing the PID stored inside the lock file, and the call site (src/memsearch/maintenance.py:101-104) maps that toMaintenanceResult(action="locked", skipped=True)— maintenance silently skips, indefinitely. Observed in the wild for ~40 days (see #788).Fix
Keeps the single
O_CREAT | O_EXCLmechanism — noflock/msvcrtsecond mechanism is introduced:FileExistsError,_file_lock()now reads the holder PID from the lock file (_read_lock_pid) and probes it withos.kill(pid, 0):-
ProcessLookupError→ holder is provably dead → stale lock;-
PermissionError→ holder is alive (owned by another user) → keep skipping;- Windows: the PID probe is skipped entirely —
os.kill(pid, 0)terminates the target process there instead of probing, so Windows relies on the mtime fallback only.STALE_LOCK_AGE_SECONDS = 86400(24h), aligned with the defaultmin_interval_hours = 24(src/memsearch/config.py:157). A lock older than one default interval cannot belong to a run that is still making progress, and the age threshold also bounds PID-reuse false positives (_lock_is_stale).unlink()ed and theO_CREAT | O_EXCLopen retried exactly once; losing that race to another process is equivalent to finding a live holder and yieldsFalse(bounded, no loop).run_due_tasks(src/memsearch/maintenance.py:102) and the skills path (src/memsearch/skills.py:434) — go through the same function, so both get the recovery automatically.Test evidence
All commands run locally on macOS (arm64, dependencies via
uv).Before (unpatched tree at
09e07d8+ new tests):ImportError: STALE_LOCK_AGE_SECONDS— the constant only exists after the fix).uv run python -c ...): lock file holding a dead PID →acquired: Falseon 3 consecutive attempts; garbage PID + 25h-old mtime →False.timeout 300 uv run pytest tests/test_maintenance.py tests/test_maintenance_runner.py -q→ 38 passed in 10.44s (all green before the patch).After (this branch):
timeout 300 uv run pytest tests/test_maintenance.py tests/test_maintenance_runner.py -q→ 45 passed (38 baseline + 7 new); re-verified on the final tree: 45 passed in 0.80s.timeout 600 uv run pytest tests -q→ 569 passed, 8 skipped in 135.25s; re-verified on the final tree: 569 passed, 8 skipped in 119.85s.timeout 120 uv run ruff check src/memsearch/maintenance.py tests/test_maintenance.py→ All checks passed (exit 0).uv run ruff format --checkon the same two files → 2 files already formatted.if os.name == "nt": pytest.skip(...)) because PID liveness probing is POSIX-only there; the Windows path (probe skipped, mtime-only fallback) is designed to be compatible but was not executed on Windows — no Windows machine was available for this change.🤖 Prepared with AI assistance (code, tests, and this description), as disclosed in the claim comment on #788.