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.
This commit is contained in:
l0ng-ai
2026-11-16 16:07:07 +08:00
committed by GitHub
parent 401d66d850
commit c6bd809d08
3 changed files with 93 additions and 8 deletions
+28
View File
@@ -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<void> | null = null;
try {
if (!(await stopRunningInstances(config))) return;
// From its session on, a further Debug asks to stop it (`stopRunningInstances`).
const debugging = new Promise<void>((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<string>();
async function debugLaunch(config: RunConfig, chain: readonly string[], show?: boolean): Promise<void> {
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.
+33 -1
View File
@@ -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)] });
+32 -7
View File
@@ -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<void> {
}
}
/** 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<boolean> {
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<void> {
const live = () =>