test(writer-lock): hold the lock with a live child, not a pid assumed alive

The live-holder case wrote `pid: 1` and leaned on its own comment, "On Linux,
PID 1 is almost always alive". Windows has no PID 1, so tryAcquireWriterLock
read the holder as dead and acquired — the correct behaviour for a dead
holder, and a failing assertion. The platform-specific part was the fixture,
not the lock.

A parked child process is alive on every platform, and not stealing from a
live holder is what the lock actually promises, so the case now tests the
promise rather than a number that happens to name a running process. The child
is reaped in afterEach.

Fixes #1752.
This commit is contained in:
Aaron Queen
2026-09-08 00:14:14 -06:00
parent 48711140e7
commit bee140288d
+13 -5
View File
@@ -58,18 +58,26 @@ describe('writer lock (#1740)', () => {
it('reports taken when a live foreign pid holds the lock', () => {
const root = makeProject();
// Use our own pid first, then overwrite with a fake live-looking pid by
// writing a pid that is alive: process.pid of this test — simulate foreign
// by writing a different alive pid. On Linux, PID 1 is almost always alive.
// A foreign process that is genuinely alive, rather than a pid assumed to
// be: PID 1 is init on Linux but does not exist on Windows, where the lock
// then reads the holder as dead and correctly acquires — the assertion was
// failing on the fixture, not on the lock. A parked child is alive
// everywhere, and it is what the lock actually promises not to steal from.
// Uses the shared holder so afterEach reaps it (same as the fallback-
// engine case below).
holder = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 60000)'], {
stdio: 'ignore',
});
if (!holder.pid) throw new Error('Failed to spawn writer-lock holder');
fs.writeFileSync(
getWriterPidPath(root),
JSON.stringify({ pid: 1, mode: 'direct', startedAt: Date.now() }) + '\n',
JSON.stringify({ pid: holder.pid, mode: 'direct', startedAt: Date.now() }) + '\n',
{ flag: 'wx' },
);
const r = tryAcquireWriterLock(root, 'direct');
expect(r.kind).toBe('taken');
if (r.kind === 'taken') {
expect(r.existing?.pid).toBe(1);
expect(r.existing?.pid).toBe(holder.pid);
const msg = writerLockHeldMessage(r.existing, r.pidPath);
expect(msg).toMatch(/writer lock held/i);
expect(msg).toMatch(/CODEGRAPH_NO_DAEMON/);