← Back to Pull requests

fix(maintenance): recover stale O_EXCL lock files left by crashed holders #792

Open
unknown opened this pull request with 501 lines diff across 6 files
View on GitHub
unknown unknown commented on Oct 11, 2026 22:05 UTC
Author

Fixes #788

Root cause

_file_lock() in src/memsearch/maintenance.py (pre-fix tree at 09e07d8) guards each maintenance task with an O_CREAT | O_EXCL sentinel lock file and removes it only in the holding process's own finally block. A run that dies hard — SIGKILL, OOM kill, or host exit reaping the session-start hook child — never reaches that finally, so the lock file stays behind forever. Every later maintenance wake hits the except FileExistsError: branch, which yields False without reading or probing the PID stored inside the lock file, and the call site (src/memsearch/maintenance.py:101-104) maps that to MaintenanceResult(action="locked", skipped=True) — maintenance silently skips, indefinitely. Observed in the wild for ~40 days (see #788).

Fix

Keeps the single O_CREAT | O_EXCL mechanism — no flock/msvcrt second mechanism is introduced:

  • On FileExistsError, _file_lock() now reads the holder PID from the lock file (_read_lock_pid) and probes it with os.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.
  • Alive or undeterminable holders fall back to an mtime staleness threshold: STALE_LOCK_AGE_SECONDS = 86400 (24h), aligned with the default min_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).

  • A proven-stale lock is unlink()ed and the O_CREAT | O_EXCL open retried exactly once; losing that race to another process is equivalent to finding a live holder and yields False (bounded, no loop).

  • Behavior when the lock is genuinely held is unchanged. Both call sites — 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):

  • The 7 new regression tests fail at collection on the unpatched tree (ImportError: STALE_LOCK_AGE_SECONDS — the constant only exists after the fix).

  • The issue's minimal repro (uv run python -c ...): lock file holding a dead PID → acquired: False on 3 consecutive attempts; garbage PID + 25h-old mtime → False.

  • Baseline: 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.

  • Full suite: 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 --check on the same two files → 2 files already formatted.

  • Behavior script (five cases): dead PID → acquired with zero residue; live PID → skipped with the lock file left untouched; garbage PID + fresh mtime → skipped; garbage PID + 25h mtime → acquired; normal hold-then-exit → zero residue.

  • The dead-PID regression test skips itself on Windows (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.

    Diff: 501 lines (+375 / -126) across 6 files Reviewer: tonydzi Review duration: 5142m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.5% (MEDIUM) · Higher = larger blast radius
    47.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.3% · Safe to merge
    25.3%