From b9f9e2668a80950bcbb87425570313afc8bae4e0 Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Sun, 22 Nov 2026 19:51:58 +0900 Subject: [PATCH] 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> --- .../__tests__/sessionAdoption.test.ts | 85 +++++++++++++++++++ src/features/debugger/state.ts | 4 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/features/debugger/__tests__/sessionAdoption.test.ts diff --git a/src/features/debugger/__tests__/sessionAdoption.test.ts b/src/features/debugger/__tests__/sessionAdoption.test.ts new file mode 100644 index 000000000..aad838eaf --- /dev/null +++ b/src/features/debugger/__tests__/sessionAdoption.test.ts @@ -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(); + 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["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({}); + }); +}); diff --git a/src/features/debugger/state.ts b/src/features/debugger/state.ts index 3025f1d38..23c5c53b7 100644 --- a/src/features/debugger/state.ts +++ b/src/features/debugger/state.ts @@ -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 });