Skip to content

fix(lock): a dead pid loses its lock immediately, not after five minutes (#635) - #861

Draft
hr1juldey wants to merge 1 commit into
trailhq:mainfrom
hr1juldey:fix/635-dead-pid-lock
Draft

hr1juldey wants to merge 1 commit into
trailhq:mainfrom
hr1juldey:fix/635-dead-pid-lock

Conversation

@hr1juldey

Copy link
Copy Markdown

Fixes #635 (the stale-lock half; see scope note at the end).

Root cause

lockStale only consulted pid liveness after the LOCK_STALE_MS floor (if (age < LOCK_STALE_MS) return false; came first). A SIGKILLed prompt hook leaves a seconds-old .sync.lock naming a pid that no longer exists — and for the next five minutes every query treats it as held: it waits, then answers from the stale graph, exactly the repro's "lock survived with a dead pid; next ask waited ~3.8 s".

Fix

In lockStale (util/state.ts), the pid check now runs before the age floor:

  • a readable pid that is dead → the lock is reclaimable immediately, at any age;
  • a live pid → unchanged: the lock holds until LOCK_MAX_MS (15 min), so a slow first build is still protected — that rule existed because age alone once stole a 5-minute build's lock mid-build;
  • a payload with no readable pid → the old age-only rule, both directions;
  • past LOCK_MAX_MS → reclaimable regardless, unchanged.

lockHeldByOther moves with it: a fresh lock naming a dead pid no longer reads as "another process is building", so the next caller takes over and rebuilds instead of waiting out a corpse. The one existing test that asserted the old behavior (dead pid's fresh lock "is still another's") moved with the change, and gained a genuinely-live-child case in its place.

Tests (RED→GREEN, adversarial)

  • a one-second-old lock with a dead pid: lockHeldIn false, acquireLock true — reclaimed immediately (RED before);
  • a one-second-old lock with a live pid: still held — the age rule still protects slow builds;
  • an unparseable payload: young garbage stays held, old garbage is reclaimable (the fallback rule both ways);
  • lockHeldByOther: a genuinely live child process (async spawn, killed in finally) reads as "wait"; an exited pid's fresh lock reads as "take over".
  • Full suite: fail-6 (the six pre-existing main failures), 4551 tests.

Scope note

The child-budget half of #635 — the settings "timeout": 60 read as milliseconds and floored to 4000 ms (compounding #283), and the inline rebuild exceeding that budget — is a separate surface from the lock; this PR deliberately does not touch it.

…tes (trailhq#635)

The host can SIGKILL a prompt hook mid-rebuild (Claude Code's own hook
timeout does exactly this). The lock file survives naming a pid that no
longer exists, and lockStale only consulted pid liveness after
LOCK_STALE_MS — so for the next five minutes every query waited out the
lock and answered from a stale graph, exactly the trailhq#635 repro.

The pid check now runs first: a dead owner holds nothing, at any age.
The age rules keep protecting live owners untouched — a slow first
build keeps its lock past LOCK_STALE_MS, any lock past LOCK_MAX_MS
yields whoever holds it (the pid may be a recycled number), and a
payload with no readable pid keeps the old age-only rule.

lockHeldByOther moves with it: a fresh lock naming a dead pid no longer
reads as "another process is building", so the next caller takes over
and rebuilds instead of waiting out a corpse.

Scope note: the child-budget half of trailhq#635 (settings "timeout" units,
compounding trailhq#283) is a separate surface and is not touched here.
@github-actions

Copy link
Copy Markdown
Contributor

🌱 graft blast radius

1 area changed → 1 area can be affected. 2 dependent symbols, depth 2.
Tests: no test reaches State Locking.
Tag: @anirudhkumar-nanonets — State Locking, Query Freshness Gate

flowchart TB
  A0(("Query Freshness Gate<br/>2 symbols"))
  classDef reached fill:#D9EDF3,stroke:#3AA7C9,stroke-width:1.5px,color:#0E313C;
  class A0 reached;
Loading
Can be affected Symbols Nearest hop Reached from
Query Freshness Gate 2 src/graph/refresh.ts:L135-L160 buildMissingMap — calls, depth 2 State Locking
Who knows this code — 1 person across 2 areas
Area Who knows it
State Locking · changed @anirudhkumar-nanonets — 13 commits, last 16d ago
Query Freshness Gate · affected @anirudhkumar-nanonets — 9 commits, last 16d ago

Ownership is git history over each area's own files, weighted towards recent work (120-day half-life). Merge commits and bots are dropped, and you are dropped from your own PR. A name with no @ has no GitHub handle in its commit email — tag them by hand, or add a .mailmap entry. A suggestion from history, not a CODEOWNERS rule.

All 2 dependent symbols, grouped by area

Query Freshness Gate — 2 symbols in 1 file

  • src/graph/refresh.ts:L135-L160 — buildMissingMap (calls, depth 2)
  • src/graph/refresh.ts:L228-L235 — waitForLock (calls, depth 2)
Test signal per changed area — 1 ✗

Reached = a node under a test path has a resolved edge into the changed symbol. It undercounts anything called indirectly — through a CLI, a spawned process or a dynamic import — so read a low ratio as “look here”, never as a coverage gate.

  • ✗ State Locking — 0 of 1 reached · no test file reaches it
    • not reached: lockStale

graft blast · origin/main...HEAD · depth 2 · 2 changed files

Open the interactive graph → — click an area to see its dependent symbols at file:line.

github-actions Bot added a commit that referenced this pull request Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompt hook's graft ask child outlives its execFileSync timeout during an inline graph rebuild; SIGKILL leaves .sync.lock with a dead pid

1 participant