Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/util/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,22 @@ export function lockHeldByOther(cache: string): boolean {
}

/**
* A lock is reclaimable when its owner is gone and it is older than LOCK_STALE_MS, or
* when it is older than LOCK_MAX_MS whoever holds it. Age alone used to be enough, so a
* first build past five minutes on a slow laptop lost its lock to the next query, which
* started a second full build beside it, and five minutes later a third.
* A lock is reclaimable when its owner is gone — immediately, not after
* LOCK_STALE_MS: a SIGKILLed hook leaves a fresh lock naming a dead pid, and
* the age floor used to make every query wait out five minutes before
* answering from a stale graph (#635). The age rules still bound LIVE owners:
* a slow first build keeps its lock past LOCK_STALE_MS, and any lock past
* LOCK_MAX_MS yields whoever holds it (the pid may be a new process that got
* a dead owner's number). A payload with no readable pid keeps the old age
* rule, since there is nothing to interrogate.
*/
function lockStale(p: string): boolean {
let age: number;
try { age = Date.now() - statSync(p).mtimeMs; } catch { return true; }
if (age >= LOCK_MAX_MS) return true;
if (age < LOCK_STALE_MS) return false;
const pid = lockPid(p);
return pid === null || !pidAlive(pid);
if (pid === null) return age >= LOCK_STALE_MS; // unreadable pid: the old age rule
return !pidAlive(pid); // a dead owner holds nothing, immediately (#635); a live owner keeps the lock until LOCK_MAX_MS
}

function lockPid(p: string): number | null {
Expand Down
55 changes: 51 additions & 4 deletions test/claude-state.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, existsSync, mkdirSync, readdirSync, utimesSync, writeFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { spawn, spawnSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
Expand Down Expand Up @@ -85,9 +85,16 @@ test('lockHeldByOther: another live process, not this one or its parent', () =>
assert.equal(lockHeldByOther(cacheDir(d)), false, 'our own lock');
lockOf(d, process.ppid, 0);
assert.equal(lockHeldByOther(cacheDir(d)), false, 'our parent: sync-run holding it for its build');
const other = spawnSync(process.execPath, ['-e', '']); // gone, but its fresh lock is still another's
lockOf(d, other.pid!, 0);
assert.equal(lockHeldByOther(cacheDir(d)), true);
const live = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 4000)']);
try {
lockOf(d, live.pid!, 0);
assert.equal(lockHeldByOther(cacheDir(d)), true, 'another live process is building — wait');
} finally {
live.kill();
}
const gone = spawnSync(process.execPath, ['-e', '']); // already exited
lockOf(d, gone.pid!, 0);
assert.equal(lockHeldByOther(cacheDir(d)), false, 'a dead owner holds nothing — take over instead of waiting (#635)');
});

test('writeJsonAtomic leaves no scratch file behind when the write fails', () => {
Expand All @@ -114,3 +121,43 @@ test('writeJsonAtomic leaves no scratch file behind when the write fails', () =>
'no .tmp residue',
);
});

/*
* Issue #635: the host SIGKILLs a prompt hook mid-rebuild; the lock file
* survives naming a pid that no longer exists. The pid-liveness check in
* lockStale only ran after LOCK_STALE_MS, so for the next five minutes every
* query waited out the lock and answered from a stale graph. A dead owner
* holds nothing — the pid check must come before the age floor. The age
* floors stay exactly as they are for live owners (a slow first build keeps
* its lock for LOCK_STALE_MS, any owner past LOCK_MAX_MS yields).
*/
test('a lock with a dead pid is reclaimable immediately, not after five minutes (#635)', () => {
const d = fresh();
lockOf(d, deadPid(), 1000); // one second old — the SIGKILL just happened
assert.equal(lockHeldIn(cacheDir(d)), false, 'a dead owner holds nothing');
assert.equal(acquireLock(d), true, 'reclaimed immediately');
});

test('a young lock with a live pid stays held — the age rule still protects slow builds (#635)', () => {
const d = fresh();
lockOf(d, process.pid, 1000);
assert.equal(lockHeldIn(cacheDir(d)), true);
assert.equal(acquireLock(d), false, 'a running build keeps its fresh lock');
});

test('a lock payload with no readable pid keeps the age rule (#635)', () => {
// No pid to interrogate: age alone must decide, both ways. The garbage
// payload goes in first and the mtime is backdated after, so the write
// itself does not reset the age.
const d = fresh();
const young = lockOf(d, process.pid, 1000);
writeFileSync(young, 'not json at all');
assert.equal(lockHeldIn(cacheDir(d)), true, 'young garbage stays held');

const d2 = fresh();
const old = lockOf(d2, process.pid, LOCK_STALE_MS + 1000);
writeFileSync(old, 'not json at all');
const t = (Date.now() - LOCK_STALE_MS - 1000) / 1000;
utimesSync(old, t, t);
assert.equal(lockHeldIn(cacheDir(d2)), false, 'old garbage is reclaimable');
});