diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6351dda51..83ff0bddb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,8 @@ jobs: # ─────────────────────────────────────────────────────────────── test: name: Unit Tests - runs-on: ubuntu-latest + # Playwright 1.58.2 cannot install Chromium dependencies on Ubuntu 26.04. + runs-on: ubuntu-24.04 services: redis: image: redis:7-alpine diff --git a/packages/cli/src/runner/daemon.ts b/packages/cli/src/runner/daemon.ts index 8c45cf3dd..463e28ec3 100644 --- a/packages/cli/src/runner/daemon.ts +++ b/packages/cli/src/runner/daemon.ts @@ -1893,6 +1893,7 @@ export async function runDaemon(_args: string[] = []): Promise { // viewers automatically when they connect to a session. } catch (err) { const message = err instanceof Error ? err.message : String(err); + logWarn(`session ${sessionId} spawn failed: ${message}`); emitChildFailure(message, classifySpawnFailure(message, null)); } }; diff --git a/packages/cli/src/runner/session-spawner.test.ts b/packages/cli/src/runner/session-spawner.test.ts index a697f6051..f936e5523 100644 --- a/packages/cli/src/runner/session-spawner.test.ts +++ b/packages/cli/src/runner/session-spawner.test.ts @@ -1179,6 +1179,58 @@ describe("session-spawner child", () => { } }); + test("a wake replaces a stale adopted entry whose suspended worker exited without IPC", async () => { + class FakeChild extends EventEmitter { + pid = 6001; + killed = false; + exitCode: number | null = null; + send = mock((_msg: unknown) => {}); + } + const children: FakeChild[] = []; + const spawnMock = mock(() => { + const c = new FakeChild(); + children.push(c); + return c; + }); + mock.module("node:child_process", () => ({ ...realChildProcess, spawn: spawnMock, execFile: mock(() => {}) })); + mock.module("../extensions/session-attachments.js", () => ({ cleanupSessionAttachments: mock(async () => {}) })); + mock.module("./logger.js", () => ({ logInfo: mock(() => {}) })); + mock.module("./runner-usage-cache.js", () => ({ + runnerUsageCacheFilePath: () => "/tmp/test-usage-cache.json", + trackSessionCwd: mock(() => {}), + untrackSessionCwd: mock(() => {}), + refreshAndWriteRunnerUsageCache: mock(async () => {}), + })); + mock.module("./workspace.js", () => ({ isCwdAllowed: () => true })); + mock.module("./session-procs.js", () => ({ + ensureSessionProcDir: () => {}, + sessionProcFilePath: (_sessionId: string) => "/tmp/test-session-adopted-wake.procs", + readRecordedGroupPids: () => [], + recordSessionGroupPid: () => {}, + removeSessionProcFile: () => {}, + })); + + const { spawnSession } = await import("./session-spawner.js"); + const tempCwd = mkdtempSync(join(tmpdir(), "session-spawner-adopted-wake-")); + try { + const runningSessions = new Map([ + ["sess-adopted", { sessionId: "sess-adopted", child: null, startedAt: 0, adopted: true }], + ]); + const args = ["k", "https://relay.example", tempCwd, runningSessions, new Set(), new Set(), undefined] as const; + + // A plain (non-wake) spawn still refuses to clobber a live adopted session. + expect(() => spawnSession("sess-adopted", ...args)).toThrow("Session already running"); + expect(children).toHaveLength(0); + + spawnSession("sess-adopted", ...args, { isWake: true }); + expect(children).toHaveLength(1); + expect(runningSessions.get("sess-adopted")?.child).toBe(children[0]); + expect(runningSessions.get("sess-adopted")?.adopted).toBeUndefined(); + } finally { + rmSync(tempCwd, { recursive: true, force: true }); + } + }); + test("a wake deferred by a pending suspend is dropped if the session is killed before the old worker exits (review 994-r2 #1)", async () => { const isCwdAllowed = mock((_cwd: string | undefined) => true); class FakeChild extends EventEmitter { diff --git a/packages/cli/src/runner/session-spawner.ts b/packages/cli/src/runner/session-spawner.ts index 81c2939a8..b1ecf399d 100644 --- a/packages/cli/src/runner/session-spawner.ts +++ b/packages/cli/src/runner/session-spawner.ts @@ -391,7 +391,17 @@ export function spawnSession( existing.pendingWakeTimer = timer; return; } - throw new Error(`Session already running: ${sessionId}`); + // An adopted entry (its worker outlived a daemon restart) has no child + // handle or IPC channel: its pre_suspend never reaches us, and a + // suspend sends no session_ended, so the entry outlives the worker. + // The relay only wakes a session it holds as suspended/offline, so a + // wake proves the adopted worker is gone — replace the stale entry. + if (existing.adopted && !existing.child && options?.isWake) { + logInfo(`session ${sessionId} wake replaces stale adopted entry`); + runningSessions.delete(sessionId); + } else { + throw new Error(`Session already running: ${sessionId}`); + } } // Ensure the recorded-group pid file directory exists before the worker