Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/runner/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,7 @@ export async function runDaemon(_args: string[] = []): Promise<number> {
// 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));
}
};
Expand Down
52 changes: 52 additions & 0 deletions packages/cli/src/runner/session-spawner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>([
["sess-adopted", { sessionId: "sess-adopted", child: null, startedAt: 0, adopted: true }],
]);
const args = ["k", "https://relay.example", tempCwd, runningSessions, new Set<string>(), new Set<string>(), 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 {
Expand Down
12 changes: 11 additions & 1 deletion packages/cli/src/runner/session-spawner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading