From ace8a78dcc14499b82b077301bcc4633c99e66c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 16 Nov 2026 00:29:53 +0000 Subject: [PATCH] fix(cli): preview --stop signals only a process the system says owns the port (#7882) * fix(cli): preview --stop signals only a process the system says owns the port * style(cli): format the preview stop tests --- .../cli/src/commands/previewLifecycle.test.ts | 78 +++++++++++++++++++ packages/cli/src/commands/previewLifecycle.ts | 17 +++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands/previewLifecycle.test.ts b/packages/cli/src/commands/previewLifecycle.test.ts index ecf973d713..02afcd69e7 100644 --- a/packages/cli/src/commands/previewLifecycle.test.ts +++ b/packages/cli/src/commands/previewLifecycle.test.ts @@ -30,6 +30,7 @@ const server: ActiveServer = { projectDir, version: "test", pid: "4321", + pidSource: "os", }; function tempDir(prefix: string): string { @@ -1379,6 +1380,83 @@ describe("background preview lifecycle", () => { expect(kill).not.toHaveBeenCalled(); }); + it("refuses a PID the listener reported about itself when the system named no owner", async () => { + const stateHome = tempDir("hf-preview-state-"); + const claimed = { ...server, pid: "999", pidSource: "self-reported" as const }; + const kill = vi.fn(); + + await expect( + stopBackgroundPreview(projectDir, 3002, { + scan: async () => [claimed], + probe: async () => claimed, + kill, + sleep: async () => {}, + stateHome, + }), + ).rejects.toThrow(/ownership could not be proven/); + + expect(kill).not.toHaveBeenCalled(); + }); + + it("stops only its own saved wrapper when the listener's PID is self-reported", async () => { + const stateHome = tempDir("hf-preview-state-"); + writePreviewSession( + { + pid: 4321, + wrapperIdentity: "wrapper-birth", + port: 3210, + projectDir, + logPath: "/tmp/p.log", + }, + stateHome, + ); + let running = true; + const claimed = { ...server, pid: "999", pidSource: "self-reported" as const }; + const kill = vi.fn((pid: number) => { + if (pid === 4321) running = false; + }); + + const result = await stopBackgroundPreview(projectDir, 3002, { + probe: async () => (running ? claimed : null), + kill, + isDescendant: () => false, + identity: (pid) => (pid === 4321 ? "wrapper-birth" : null), + sleep: async () => {}, + stateHome, + }); + + expect(result).toBe(true); + expect(kill.mock.calls).toEqual([[4321]]); + }); + + it("refuses a self-reported PID when the saved wrapper's birth identity has changed", async () => { + const stateHome = tempDir("hf-preview-state-"); + writePreviewSession( + { + pid: 4321, + wrapperIdentity: "wrapper-birth", + port: 3210, + projectDir, + logPath: "/tmp/p.log", + }, + stateHome, + ); + const claimed = { ...server, pid: "999", pidSource: "self-reported" as const }; + const kill = vi.fn(); + + await expect( + stopBackgroundPreview(projectDir, 3002, { + probe: async () => claimed, + kill, + identity: () => "reused-pid-birth", + sleep: async () => {}, + stateHome, + }), + ).rejects.toThrow(/ownership could not be proven/); + + expect(kill).not.toHaveBeenCalled(); + }); + it("reaps the saved wrapper when the live server is proven to be its descendant", async () => { const stateHome = tempDir("hf-preview-state-"); writePreviewSession( diff --git a/packages/cli/src/commands/previewLifecycle.ts b/packages/cli/src/commands/previewLifecycle.ts index 0f3e380c18..e3309acc5c 100644 --- a/packages/cli/src/commands/previewLifecycle.ts +++ b/packages/cli/src/commands/previewLifecycle.ts @@ -511,6 +511,21 @@ function ownedStopTargetPid( return isDescendant(liveServerPid, saved.pid) ? saved.pid : liveServerPid; } +function provenStopTargetPid( + projectDir: string, + server: ActiveServer, + saved: PreviewSession | null, + liveServerPid: number, + dependencies: LifecycleDependencies, +): number { + if (server.pidSource === "os") return ownedStopTargetPid(saved, liveServerPid, dependencies); + if (saved && isCurrentPreviewWrapper(saved, dependencies)) return saved.pid; + throw new Error( + `preview ownership could not be proven for ${resolve(projectDir)}: the system did not say which process listens on port ${server.port}. ` + + "Stop that preview from the terminal it runs in.", + ); +} + async function stopOwnedPreviewBeforeReplacement( owned: ActiveServer | null, projectDir: string, @@ -744,7 +759,7 @@ export async function stopBackgroundPreview( } const kill = dependencies.kill ?? stopProcess; - await kill(ownedStopTargetPid(saved, pid, dependencies)); + await kill(provenStopTargetPid(projectDir, server, saved, pid, dependencies)); if (!(await awaitServerGone(projectDir, server.port, dependencies))) { throw new Error(`background preview did not stop for ${resolve(projectDir)}`);