From c6bd809d0809f5dc10fa51494e05fde0c655c8b4 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Mon, 16 Nov 2026 16:07:07 +0800 Subject: [PATCH] fix(run): Debug on a running configuration asks to stop it instead of starting another (#3710) Debug skipped the single-instance check Run has, so each click started one more debug session of the same configuration. Debug now asks "not allowed to run in parallel" and stops the configuration's running and debugged instances first (unless it allows multiple instances); a click while the first Debug is still preparing is ignored. Run also counts the configuration's debug sessions as running instances. --- src/features/run/index.ts | 28 ++++++++++++++++++++++ src/features/run/session.test.tsx | 34 ++++++++++++++++++++++++++- src/features/run/store.ts | 39 +++++++++++++++++++++++++------ 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/features/run/index.ts b/src/features/run/index.ts index 24e2d59c3..7c67b5d22 100644 --- a/src/features/run/index.ts +++ b/src/features/run/index.ts @@ -92,6 +92,7 @@ import { selectConfig, selectedConfig, startRun, + stopRunningInstances, startSampleOutput, stopSession, subscribeOutput, @@ -171,6 +172,33 @@ async function debugConfig(config: RunConfig, chain: readonly string[] = [], sho return; } if (remoteDebugBlocked(config)) return; + if (config.allowParallel) return debugLaunch(config, chain, show); + // Clicked again while it is still preparing (building): that launch goes on, no second one. + if (debugLaunching.has(config.id)) return; + debugLaunching.add(config.id); + let stopWatching = () => {}; + let launched: Promise | null = null; + try { + if (!(await stopRunningInstances(config))) return; + // From its session on, a further Debug asks to stop it (`stopRunningInstances`). + const debugging = new Promise((resolve) => { + stopWatching = useDebugRunSessions.subscribe((s) => { + if (s.serviceSessions.some((d) => d.config.id === config.id && d.status !== "terminated")) resolve(); + }); + }); + launched = debugLaunch(config, chain, show); + await Promise.race([launched.catch(() => {}), debugging]); + } finally { + stopWatching(); + debugLaunching.delete(config.id); + } + await launched; +} + +/** Configurations whose Debug is between the click and the debugger's session. */ +const debugLaunching = new Set(); + +async function debugLaunch(config: RunConfig, chain: readonly string[], show?: boolean): Promise { if (!(await prepareLaunch(config, chain, show))) return; if (config.kind === UNITY_CONFIG_KIND) return showUnityAttach(config); // Python on a Docker / Compose target: debugpy in the container, the debugger attaches. diff --git a/src/features/run/session.test.tsx b/src/features/run/session.test.tsx index e596d3b44..42e6157c9 100644 --- a/src/features/run/session.test.tsx +++ b/src/features/run/session.test.tsx @@ -22,6 +22,7 @@ import { resetRuns, runtime, startRun, + stopRunningInstances, useRun, runSelectedTests, rerunFailed, @@ -64,7 +65,7 @@ const running = (id: string, c: RunConfig, runId: number | null = null): RunSess beforeEach(() => { resetRuns(); - useDebugRunSessions.setState({ sessions: [] }); + useDebugRunSessions.setState({ sessions: [], serviceSessions: [] }); confirm.mockClear(); confirm.mockResolvedValue(true); provideToolWindowService({ show: vi.fn(), isVisible: () => true } as unknown as ToolWindowService); @@ -96,6 +97,37 @@ describe("single-instance runs", () => { expect(useRun.getState().sessions.map((s) => s.id)).toEqual(["run1"]); }); + const debugging = (c: RunConfig) => { + const stop = vi.fn(async () => {}); + useDebugRunSessions.setState({ + serviceSessions: [ + { id: 1, config: c, startedAt: 0, status: "running", processId: null, rerun: null, stop, show: () => {} }, + ], + }); + return stop; + }; + + it("Debug on a configuration being debugged or run asks, then stops those instances", async () => { + const app = config("app"); + const stop = debugging(app); + useRun.setState({ sessions: [running("run1", app)] }); + expect(await stopRunningInstances(app)).toBe(true); + expect(confirm).toHaveBeenCalledWith("app", 2); + expect(stop).toHaveBeenCalled(); + confirm.mockResolvedValue(false); + expect(await stopRunningInstances(app)).toBe(false); + expect(await stopRunningInstances(config("other"))).toBe(true); + expect(await stopRunningInstances(config("app", { allowParallel: true }))).toBe(true); + }); + + it("Run on a configuration being debugged asks and stops the debug session", async () => { + const app = config("app"); + const stop = debugging(app); + await startRun(app); + expect(confirm).toHaveBeenCalledWith("app", 1); + expect(stop).toHaveBeenCalled(); + }); + it("starts another instance when parallel runs are allowed", async () => { const app = config("app", { allowParallel: true }); useRun.setState({ sessions: [running("run1", app)] }); diff --git a/src/features/run/store.ts b/src/features/run/store.ts index 240018faa..24ce4d6f7 100644 --- a/src/features/run/store.ts +++ b/src/features/run/store.ts @@ -862,13 +862,11 @@ async function startRunIn( const others = config.allowParallel ? [] : state.sessions.filter((s) => s.config.id === config.id && busy(s) && s.id !== session?.id); - if (others.length) { - if ( - workflowSettings().run.confirmRerun && - !(await confirmRerun(config.name, others.length + (session && busy(session) ? 1 : 0))) - ) - return null; - await Promise.all(others.map((s) => stopSession(s.id))); + const debugging = config.allowParallel ? [] : liveDebugSessions(config.id); + if (others.length || debugging.length) { + const instances = others.length + debugging.length + (session && busy(session) ? 1 : 0); + if (workflowSettings().run.confirmRerun && !(await confirmRerun(config.name, instances))) return null; + await Promise.all([...others.map((s) => stopSession(s.id)), ...debugging.map((d) => d.stop())]); session ??= others[0]; } const stopping = others.map((s) => s.id); @@ -1378,6 +1376,33 @@ export async function stopSession(sessionId: string): Promise { } } +/** The debugger's live sessions of the configuration `configId`. */ +function liveDebugSessions(configId: string) { + return useDebugRunSessions + .getState() + .serviceSessions.filter((d) => d.config.id === configId && d.status !== "terminated"); +} + +/** + * Before a configuration that doesn't allow parallel runs is debugged: as in IDEA, asks before + * stopping its running and debugged instances, then waits for their processes to exit. + * False when the user keeps them. + */ +export async function stopRunningInstances(config: RunConfig): Promise { + if (config.allowParallel) return true; + const runs = useRun.getState().sessions.filter((s) => s.config.id === config.id && isLive(s)); + const debugging = liveDebugSessions(config.id); + if (!runs.length && !debugging.length) return true; + if (workflowSettings().run.confirmRerun && !(await confirmRerun(config.name, runs.length + debugging.length))) + return false; + await Promise.all([...runs.map((s) => stopSession(s.id)), ...debugging.map((d) => d.stop())]); + await waitForExit( + runs.map((s) => s.id), + null, + ); + return true; +} + /** Resolves once the given sessions' processes exited (or after `timeoutMs`, unless `null`). */ export function waitForExit(sessionIds: readonly string[], timeoutMs: number | null = 10_000): Promise { const live = () =>