Skip to content

Commit 7038a0b

Browse files
jesse-merhiclawsweeper
authored andcommitted
fix(gateway): release empty quiescence leases
1 parent 8763388 commit 7038a0b

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

‎src/gateway/worker-environments/workspace-quiescence-scripts.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,12 @@ for (const entry of input.processes) {
453453
if (processIdentity(entry.pid) !== entry.start) continue;
454454
try { process.kill(entry.pid, "SIGCONT"); } catch (error) { if (!error || (error.code !== "ESRCH" && error.code !== "EPERM")) throw error; }
455455
}
456-
if (input.watchdog !== null && processIdentity(input.watchdog.pid) === input.watchdog.start) {
456+
let watchdogStart = null;
457+
try { if (input.watchdog !== null) watchdogStart = processIdentity(input.watchdog.pid); } catch (error) {
458+
// An empty shared-host lease has nothing to strand, so ps cannot block its release.
459+
if (input.processes.length > 0) throw error;
460+
}
461+
if (input.watchdog !== null && watchdogStart === input.watchdog.start) {
457462
try { process.kill(input.watchdog.pid, "SIGTERM"); } catch (error) { if (!error || (error.code !== "ESRCH" && error.code !== "EPERM")) throw error; }
458463
}
459464
// The watchdog stays alive across the whole resume loop now, so it can win the unlink race.

‎src/gateway/worker-environments/workspace-sync-scripts.test.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,17 +325,15 @@ esac
325325
});
326326
});
327327

328-
it("bounds identity lookups so a stalled host ps cannot strand frozen processes", async () => {
328+
it("releases an empty shared-host lease without depending on ps", async () => {
329329
const input = await fixture();
330330
const healthyPs = await fs.readFile(path.join(input.bin, "ps"), "utf8");
331-
const nonce = await quiesce(input);
332-
const lease = JSON.parse(
333-
await fs.readFile(leasePath(input.home, input.workspace, nonce), "utf8"),
334-
) as { watchdog: { pid: number } };
331+
const nonce = await quiesce(input, true, "1000");
332+
const leaseFile = leasePath(input.home, input.workspace, nonce);
333+
const lease = JSON.parse(await fs.readFile(leaseFile, "utf8")) as { watchdog: { pid: number } };
335334

336-
// Every SIGCONT is gated on processIdentity, so an unbounded lookup leaves the whole
337-
// lease frozen with no remaining resumer. The stub ignores SIGTERM because that is the
338-
// shape execFileSync's timeout alone cannot escape.
335+
// Shared hosts intentionally freeze nothing. A ps outage must not block a no-op release or
336+
// turn a successfully reconciled worker result into an error.
339337
await fs.writeFile(path.join(input.bin, "ps"), STALLED_PS);
340338
await fs.chmod(path.join(input.bin, "ps"), 0o755);
341339

@@ -346,14 +344,22 @@ esac
346344
);
347345

348346
expect(result.termination).toBe("exit");
349-
expect(result.code).not.toBe(0);
347+
expect(result.code).toBe(0);
348+
await expect(fs.access(leaseFile)).rejects.toThrow();
349+
await vi.waitFor(
350+
() => {
351+
expect(() => process.kill(lease.watchdog.pid, 0)).toThrow();
352+
},
353+
{ timeout: 5_000 },
354+
);
350355
} finally {
351356
await fs.writeFile(path.join(input.bin, "ps"), healthyPs);
352357
await fs.chmod(path.join(input.bin, "ps"), 0o755);
353-
await resume(input, nonce);
354-
await vi.waitFor(() => {
355-
expect(() => process.kill(lease.watchdog.pid, 0)).toThrow();
356-
});
358+
try {
359+
process.kill(lease.watchdog.pid, "SIGTERM");
360+
} catch {
361+
// Expected once the missing lease has retired it.
362+
}
357363
}
358364
});
359365

0 commit comments

Comments
 (0)