Compare commits

..
Author SHA1 Message Date
paseo-bot[bot] acde63e614 test(terminal): prove an obsolete worker's program gone by its socket, not its pid (#11967)
On Windows the inspector reports no birth times, so the hard-cut test
could only prove the program gone by its pid not answering. Windows
reuses a freed pid at once, so a busy runner could keep the pid alive
under another process for the whole 10 s wait. The program now listens
on a socket of its own, and the test waits for that socket to close.

Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
2026-12-04 06:00:53 +00:00
3 changed files with 69 additions and 49 deletions
@@ -239,7 +239,7 @@ describe("desktop-settings", () => {
expect(persisted.settings.daemon.manageBuiltInDaemon).toBe(false);
});
it("does not rewrite migrated settings while reading them", async () => {
it("does not rewrite existing settings while reading them", async () => {
const userDataPath = await createTempUserDataDir();
directories.add(userDataPath);
const raw = JSON.stringify({
@@ -253,7 +253,6 @@ describe("desktop-settings", () => {
},
migrations: {
legacyRendererSettingsImported: false,
daemonStopOnQuitDefaultApplied: true,
},
});
await writeFile(settingsFilePath(userDataPath), raw);
@@ -266,7 +265,7 @@ describe("desktop-settings", () => {
expect(persisted).toBe(raw);
});
it("persists the stop-on-quit migration on the first read", async () => {
it("resets the pre-existing keep-running default so the daemon stops with the app", async () => {
const userDataPath = await createTempUserDataDir();
directories.add(userDataPath);
await writeFile(
@@ -277,7 +276,7 @@ describe("desktop-settings", () => {
releaseChannel: "stable",
daemon: { manageBuiltInDaemon: true, keepRunningAfterQuit: true },
},
futureSetting: { value: 7 },
migrations: { legacyRendererSettingsImported: true },
}),
);
const store = createDesktopSettingsStore({ userDataPath });
@@ -285,12 +284,6 @@ describe("desktop-settings", () => {
const settings = await store.get();
expect(settings.daemon.keepRunningAfterQuit).toBe(false);
const persisted = JSON.parse(await readFile(settingsFilePath(userDataPath), "utf8"));
expect(persisted).toMatchObject({
settings: { daemon: { manageBuiltInDaemon: true, keepRunningAfterQuit: false } },
migrations: { daemonStopOnQuitDefaultApplied: true },
futureSetting: { value: 7 },
});
});
it("keeps an explicit keep-running choice across restarts", async () => {
@@ -238,9 +238,12 @@ function hasLegacyRendererOwnedPatch(patch: DesktopSettingsPatch): boolean {
return patch.releaseChannel !== undefined || patch.daemon?.manageBuiltInDaemon !== undefined;
}
function applyDaemonStopOnQuitDefault(
document: PersistedDesktopSettingsDocument,
): PersistedDesktopSettingsDocument {
function coerceDocument(input: unknown): PersistedDesktopSettingsDocument {
const document = PersistedDocumentSchema.parse(input);
if (document.migrations.daemonStopOnQuitDefaultApplied) {
return document;
}
return {
...document,
settings: {
@@ -280,39 +283,45 @@ export function createDesktopSettingsStore({
cachedDocument = document;
}
async function loadDocumentUnlocked({
recoverUnreadable = false,
} = {}): Promise<PersistedDesktopSettingsDocument> {
async function loadDocumentUnlocked(): Promise<PersistedDesktopSettingsDocument> {
if (cachedDocument) {
return cachedDocument;
}
let document: PersistedDesktopSettingsDocument;
let raw: string;
try {
const raw = await readFile(filePath, "utf8");
document = PersistedDocumentSchema.parse(JSON.parse(raw));
raw = await readFile(filePath, "utf8");
} catch (error) {
if (!recoverUnreadable && (!isNodeError(error) || error.code !== "ENOENT")) {
if (!isNodeError(error) || error.code !== "ENOENT") {
throw error;
}
const defaults = buildDefaultDocument();
await writeDocument(defaults);
return defaults;
const document = buildDefaultDocument();
await writeDocument(document);
return document;
}
if (!document.migrations.daemonStopOnQuitDefaultApplied) {
const migrated = applyDaemonStopOnQuitDefault(document);
await writeDocument(migrated);
return migrated;
}
const document = coerceDocument(JSON.parse(raw));
cachedDocument = document;
return document;
}
async function initializeLegacyRendererMigrationUnlocked(): Promise<PersistedDesktopSettingsDocument> {
try {
return await loadDocumentUnlocked();
} catch {
const document = buildDefaultDocument();
await writeDocument(document);
return document;
}
}
return {
async get(): Promise<DesktopSettings> {
const document = await enqueueOperation(() => loadDocumentUnlocked());
const document = await enqueueOperation(async () => {
if (cachedDocument) {
return cachedDocument;
}
return loadDocumentUnlocked();
});
return toDesktopSettings(document.settings);
},
@@ -337,7 +346,7 @@ export function createDesktopSettingsStore({
async migrateLegacyRendererSettings(legacySettings: unknown): Promise<DesktopSettings> {
return enqueueOperation(async () => {
const current = await loadDocumentUnlocked({ recoverUnreadable: true });
const current = await initializeLegacyRendererMigrationUnlocked();
if (current.migrations.legacyRendererSettingsImported) {
return toDesktopSettings(current.settings);
}
@@ -26,7 +26,7 @@ import {
rmSync,
writeFileSync,
} from "node:fs";
import { delimiter, join } from "node:path";
import { basename, delimiter, join } from "node:path";
import { tmpdir } from "node:os";
import pino from "pino";
import {
@@ -1667,25 +1667,53 @@ async function startManagerWithWorkerProtocol(
}
}
// Whether the program still accepts connections on its socket. Unlike its pid, which Windows
// hands to another process as soon as the program ends and records without a birth time, the
// socket closes with the program and nothing else answers on it.
async function programListens(socketPath: string): Promise<boolean> {
return await new Promise((resolve) => {
const socket = createConnection(socketPath);
socket.once("connect", () => {
socket.destroy();
resolve(true);
});
socket.once("error", () => resolve(false));
});
}
// A terminal running an idle program in a worker that reports `protocolVersion`, left running
// after its daemon lets go (stop --keep-terminals).
// after its daemon lets go (stop --keep-terminals). The program listens on `programSocket`.
async function leaveWorkerWithTerminal(
paseoHome: string,
protocolVersion: number,
): Promise<{ terminalId: string; programPid: number; workerPid: number; cwd: string }> {
): Promise<{
terminalId: string;
programPid: number;
programSocket: string;
workerPid: number;
cwd: string;
}> {
const cwd = mkdtempSync(join(tmpdir(), "worker-terminal-manager-protocol-"));
temporaryDirs.push(cwd);
const programSocket =
process.platform === "win32"
? `\\\\.\\pipe\\paseo-test-program-${basename(cwd)}`
: join(cwd, "program.sock");
const previous = await startManagerWithWorkerProtocol(paseoHome, protocolVersion);
const session = await previous.createTerminal({
workspaceId: "ws-protocol",
cwd,
...nodeTerminalCommand("setInterval(() => {}, 1000);"),
...nodeTerminalCommand(
`require("node:net").createServer().listen(${JSON.stringify(programSocket)}); setInterval(() => {}, 1000);`,
),
});
await waitForCondition(() => programListens(programSocket), 10000);
const [record] = await workerRecords(paseoHome);
await previous.detach();
return {
terminalId: session.id,
programPid: (await recordedTerminalPids(paseoHome)).get(session.id)!,
programSocket,
workerPid: record!.pid,
cwd,
};
@@ -1697,24 +1725,14 @@ it.each([1, 4, 5])(
const paseoHome = createTemporaryHome();
const old = await leaveWorkerWithTerminal(paseoHome, version);
const [record] = await workerRecords(paseoHome);
const program = record!.terminals.find((entry) => entry.id === old.terminalId)!;
const inspector = createTerminalWorkerProcessInspector();
manager = await startRealWorkerManager({ paseoHome });
expect(await readUnconfirmedTerminalCleanup(paseoHome)).toEqual([]);
// Startup launches a replacement worker after cleanup. Either old PID may already
// belong to another process, so verify that the recorded birth identities are gone.
// Startup launches a replacement worker after cleanup. The program's PID may already
// belong to another process, so its own socket is what proves it gone.
await waitForCondition(() => !isProcessAlive(record!.pid), 10000);
await expect
.poll(
async () =>
!isProcessAlive(program.pid) ||
classifyTerminalProgramProcess(program, await inspector.inspect(program.pid)).status ===
"unrelated",
{ timeout: 10000 },
)
.toBe(true);
await expect.poll(() => programListens(old.programSocket), { timeout: 10000 }).toBe(false);
expect(manager.getTerminal(old.terminalId)).toBeUndefined();
const workers = await workerRecords(paseoHome);
expect(workers).toHaveLength(1);