fix(debugger): adopt child sessions whose ids an earlier backend used (#5197)

A window whose project moves to another backend process (a remote reconnect, or a new openidea-server in the smoke tests) sees session ids numbered from 1 again. ownSession checked the ids of forgotten sessions before looking for a known parent, so a new child's started event reusing such an id was dropped, along with every later event of that child: the Debug tool window never showed the pause. started is a session's first event and can't be a late event of a forgotten session, so it no longer stops there; a child is still adopted only under a session of this window, and other late events of forgotten ids stay ignored.

Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
This commit is contained in:
LU XIANGRUI
2026-11-22 18:51:58 +08:00
committed by GitHub
co-authored by wildcatDownstairs
parent 1f39eb8e63
commit b9f9e2668a
2 changed files with 88 additions and 1 deletions
@@ -0,0 +1,85 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { LaunchConfig } from "@/platform/ipc";
import { useProject } from "@/platform/project";
import { handleDebugEvent, resetSessions, startSession, useDebug } from "../state";
const ipc = vi.hoisted(() => {
let next = 0;
return {
/** A backend process numbers its sessions from 1. */
restart: () => (next = 0),
debugStart: vi.fn(async (_launch: unknown) => ({ status: "ok", data: ++next })),
debugStop: vi.fn(async (_id: number) => ({ status: "ok", data: null })),
};
});
vi.mock("@/platform/ipc", async (original) => {
const real = await original<typeof import("@/platform/ipc")>();
const { restart: _restart, ...commands } = ipc;
return { ...real, isTauri: () => true, commands: { ...real.commands, ...commands } };
});
const launch: LaunchConfig = {
name: "pricing > withTax > adds the rate",
adapter: "js-debug",
request: "launch",
program: null,
args: [],
cwd: "/p",
env: {},
extraJson: JSON.stringify({ type: "pwa-node", runtimeExecutable: "npm", runtimeArgs: ["run", "test"] }),
};
/** js-debug's session for a process the debuggee started (`startDebugging`). */
const child = (id: number, parent: number, name: string) =>
handleDebugEvent(id, { kind: "started", name, adapter: "js-debug", parent });
const breakpoint = (id: number) =>
handleDebugEvent(id, {
kind: "stopped",
threadId: 1,
reason: "breakpoint",
description: null,
allThreadsStopped: false,
});
beforeEach(() => {
useDebug.setState({ sessions: {}, active: null });
resetSessions();
ipc.restart();
useProject.setState({
project: { root: "/p" } as ReturnType<typeof useProject.getState>["project"],
settings: null,
});
});
describe("sessions of a window whose backend changes", () => {
it("adopts the children of a new session although an earlier backend used their ids", async () => {
// The first backend: `npm run test` and the process js-debug attached to under it.
expect(await startSession(launch)).toBe(1);
child(2, 1, "npm-cli.js [100]");
expect(useDebug.getState().sessions[2]?.parent).toBe(1);
// The window reopens its project on another backend (a remote reconnect): ids start over.
resetSessions();
ipc.restart();
expect(await startSession(launch)).toBe(1);
child(2, 1, "npm-cli.js [200]");
child(3, 2, "mocha.js [201]");
breakpoint(3);
const { sessions, active } = useDebug.getState();
expect([sessions[2]?.name, sessions[2]?.parent]).toEqual(["npm-cli.js [200]", 1]);
expect([sessions[3]?.name, sessions[3]?.parent, sessions[3]?.status]).toEqual(["mocha.js [201]", 2, "paused"]);
expect(active).toBe(3);
});
it("still ignores late events of the sessions it forgot", async () => {
await startSession(launch);
child(2, 1, "npm-cli.js [100]");
resetSessions();
breakpoint(2);
handleDebugEvent(1, { kind: "output", category: "stdout", output: "late\n" });
expect(useDebug.getState().sessions).toEqual({});
expect(useDebug.getState().console).toEqual({});
});
});
+3 -1
View File
@@ -307,7 +307,9 @@ function ownSession(id: DebugSessionId, event: DebugEvent): DebugSession | null
const st = useDebug.getState();
const existing = st.sessions[id];
if (existing) return existing;
if (owned.has(id)) return null;
// `started` is a session's first event, sent once when the backend creates it: never a late event
// of a forgotten session, though its id may be one (another backend numbers from 1 again).
if (owned.has(id) && event.kind !== "started") return null;
if (event.kind === "started" && event.parent != null) {
if (!st.sessions[event.parent]) return null;
return adoptSession(id, { name: event.name, adapter: event.adapter, parent: event.parent });