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
This commit is contained in:
Miguel Ángel
2026-11-16 00:29:53 +00:00
committed by GitHub
parent 5dc3b336aa
commit ace8a78dcc
2 changed files with 94 additions and 1 deletions
@@ -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(
+16 -1
View File
@@ -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)}`);