fix(debugger): stay suspended on a thread its breakpoint still holds after another resumes (#4858)

Java debugging suspends only the thread that hits a breakpoint, but the Debug window tracked one stopped thread: after resuming it, the window showed Running with Resume and Step disabled while another thread was still stopped. The session now tracks every thread stopped alone (stopped adds, continued removes) and moves to a remaining stopped thread when the shown one continues; steps report all_threads_continued false since they run only their thread.

Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com>

Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com>
This commit is contained in:
ntbang0901
2026-11-20 23:50:14 +08:00
committed by GitHub
parent 39671ff55b
commit 4bc87f5d84
5 changed files with 122 additions and 7 deletions
+2 -2
View File
@@ -1713,7 +1713,7 @@ impl DebugManager {
// A step runs its thread; other threads stopped by their own events stay stopped.
session.suspended.resumed(ticket, thread_id, false);
stream_trace::release_done(&session);
session.emit(ide_core::event::DebugEvent::Continued { thread_id, all_threads_continued: true });
session.emit(ide_core::event::DebugEvent::Continued { thread_id, all_threads_continued: false });
Ok(())
}
@@ -1825,7 +1825,7 @@ impl DebugManager {
step_settled(&session, sent)?;
session.suspended.resumed(ticket, thread_id, false);
stream_trace::release_done(&session);
session.emit(ide_core::event::DebugEvent::Continued { thread_id, all_threads_continued: true });
session.emit(ide_core::event::DebugEvent::Continued { thread_id, all_threads_continued: false });
Ok(())
}
+6
View File
@@ -2763,6 +2763,12 @@ async fn step_of_one_thread_keeps_the_others_suspended() {
assert_eq!(session.suspended.ticket(), ticket + 1);
h.manager.step_in_target(id, 7, 1).await.unwrap();
assert!(!session.suspended.get());
// The UI stays on a thread that is still stopped only when told the step ran one thread.
for thread in [7, 8] {
let continued =
h.wait_event(|e| matches!(e, DebugEvent::Continued { thread_id, .. } if *thread_id == thread)).await;
assert!(matches!(continued, DebugEvent::Continued { all_threads_continued: false, .. }), "{continued:?}");
}
}
#[tokio::test]
@@ -26,7 +26,15 @@ vi.mock("@/workbench/contributions", async (orig) => ({
...(await orig<typeof import("@/workbench/contributions")>()),
maybeEditorService: () => ({ openFile: vi.fn(async () => {}) }),
}));
import { adoptSession, handleDebugEvent, selectThread, resumeExecution, useDebug, useExecutionPoint } from "../state";
import {
adoptSession,
handleDebugEvent,
isThreadSuspended,
selectThread,
resumeExecution,
useDebug,
useExecutionPoint,
} from "../state";
const path = "/proj/MsMemberUtils.java";
const frame = (line: number, id = 100): StackFrame => ({
@@ -126,6 +134,56 @@ describe("execution point state", () => {
expect(useExecutionPoint.getState().path).toBeNull();
});
it("moves to another thread still stopped by its own breakpoint when the current one continues", async () => {
stopped(5);
await settle();
ipc.debugStackTrace.mockResolvedValueOnce(ipc.ok([frame(9, 200)]));
stopped(1);
await settle();
ipc.debugStackTrace.mockResolvedValueOnce(ipc.ok([frame(6841)]));
await resumeExecution(1, async () => {
handleDebugEvent(1, { kind: "continued", threadId: 1, allThreadsContinued: false });
});
await settle();
expect(useDebug.getState().sessions[1]).toMatchObject({
status: "paused",
stoppedThread: 5,
selectedThread: 5,
stopReason: "step",
selectedFrame: 100,
});
expect(useExecutionPoint.getState()).toMatchObject({ path, line: 6841 });
expect(isThreadSuspended(useDebug.getState().sessions[1]!, 5)).toBe(true);
expect(isThreadSuspended(useDebug.getState().sessions[1]!, 1)).toBe(false);
handleDebugEvent(1, { kind: "continued", threadId: 5, allThreadsContinued: false });
expect(useDebug.getState().sessions[1]?.status).toBe("running");
});
it("forgets a stopped thread that continues while another one is shown", async () => {
stopped(5);
await settle();
stopped(1);
await settle();
handleDebugEvent(1, { kind: "continued", threadId: 5, allThreadsContinued: false });
expect(useDebug.getState().sessions[1]).toMatchObject({ status: "paused", stoppedThread: 1 });
handleDebugEvent(1, { kind: "continued", threadId: 1, allThreadsContinued: false });
expect(useDebug.getState().sessions[1]?.status).toBe("running");
});
it.each([
["every thread continues", false, true],
["the stop suspended every thread", true, false],
])("runs when %s", async (_, allStopped, allContinued) => {
stopped(5, allStopped);
await settle();
stopped(1, allStopped);
await settle();
expect(isThreadSuspended(useDebug.getState().sessions[1]!, 5)).toBe(true);
handleDebugEvent(1, { kind: "continued", threadId: 1, allThreadsContinued: allContinued });
expect(useDebug.getState().sessions[1]?.status).toBe("running");
expect(isThreadSuspended(useDebug.getState().sessions[1]!, 5)).toBe(false);
});
it.each(["continued", "terminated"] as const)("clears the execution point on %s", async (kind) => {
stopped();
await settle();
+53 -3
View File
@@ -84,6 +84,13 @@ export interface DebugSession {
stoppedThread: number | null;
stopReason: string | null;
stopDescription: string | null;
/**
* Threads stopped alone (a `stopped` event without `allThreadsStopped`), oldest first. Adapters
* such as java-debug keep them suspended while another thread runs.
*/
suspendedThreads?: SuspendedThread[];
/** The last stop suspended every thread. */
allThreadsStopped?: boolean;
threads: ThreadInfo[];
selectedThread: number | null;
frames: StackFrame[];
@@ -107,6 +114,12 @@ export interface DebugSession {
flutterAppStarted?: boolean;
}
export interface SuspendedThread {
id: number;
reason: string;
description: string | null;
}
export interface ConsoleLine {
id: number;
category: string;
@@ -1051,6 +1064,11 @@ export function pausedThread(s: DebugSession): number | null {
return s.selectedThread ?? s.stoppedThread ?? s.threads[0]?.id ?? null;
}
export function isThreadSuspended(s: DebugSession, threadId: number): boolean {
if (s.status !== "paused") return false;
return !!s.allThreadsStopped || s.stoppedThread === threadId || !!s.suspendedThreads?.some((t) => t.id === threadId);
}
/** The selected frame of the paused session (top frame when none is selected). */
export function selectedFrame(s: DebugSession): StackFrame | null {
return s.frames.find((f) => f.id === s.selectedFrame) ?? s.frames[0] ?? null;
@@ -1116,7 +1134,8 @@ export function selectFrame(id: DebugSessionId, frameId: number, reveal = true)
void loadLocals(id, frameId);
}
async function loadFrames(id: DebugSessionId, threadId: number, reveal: boolean) {
/** `newStop`: false when returning to an earlier stop, whose breakpoint hit was handled when it happened. */
async function loadFrames(id: DebugSessionId, threadId: number, reveal: boolean, newStop = true) {
const before = useDebug.getState().sessions[id];
let frames: StackFrame[] = [];
try {
@@ -1128,7 +1147,7 @@ async function loadFrames(id: DebugSessionId, threadId: number, reveal: boolean)
if (!s || s.status !== "paused" || s.selectedThread !== threadId || s.frames !== before?.frames) return;
patch(id, { frames });
const top = topFrame(frames);
if (top?.path && (s.stopReason === "breakpoint" || s.stopReason === "function breakpoint")) {
if (newStop && top?.path && (s.stopReason === "breakpoint" || s.stopReason === "function breakpoint")) {
void logSuspendingHit(id, top);
consumeTemporary(top.path, top.line);
}
@@ -1377,8 +1396,13 @@ export function handleDebugEvent(id: DebugSessionId, event: DebugEvent) {
break;
case "stopped": {
const isBp = event.reason === "breakpoint" || event.reason === "function breakpoint";
const alone = (s.suspendedThreads ?? []).filter((t) => t.id !== event.threadId);
if (!event.allThreadsStopped)
alone.push({ id: event.threadId, reason: event.reason, description: event.description });
patch(id, {
status: "paused",
suspendedThreads: event.allThreadsStopped ? [] : alone,
allThreadsStopped: event.allThreadsStopped,
stoppedThread: event.threadId,
selectedThread: event.threadId,
stopReason: event.reason,
@@ -1410,13 +1434,39 @@ export function handleDebugEvent(id: DebugSessionId, event: DebugEvent) {
case "continued": {
const cur = useDebug.getState().sessions[id]!;
if (cur.status === "terminated") break;
if (!event.allThreadsContinued && event.threadId !== (cur.selectedThread ?? cur.stoppedThread)) break;
const rest = event.allThreadsContinued ? [] : (cur.suspendedThreads ?? []).filter((t) => t.id !== event.threadId);
if (!event.allThreadsContinued && event.threadId !== (cur.selectedThread ?? cur.stoppedThread)) {
patch(id, { suspendedThreads: rest });
break;
}
stopTaskbarFlash();
const next = rest.at(-1);
if (next) {
// Like IDEA, the session stays suspended on a thread its own breakpoint still holds.
patch(id, {
status: "paused",
suspendedThreads: rest,
stoppedThread: next.id,
selectedThread: next.id,
stopReason: next.reason,
stopDescription: next.description,
frames: [],
selectedFrame: null,
});
useDebug.setState((st) => ({ stopSeq: st.stopSeq + 1 }));
publishContext();
updateExecutionPoint();
void loadThreads(id);
void loadFrames(id, next.id, true, false);
break;
}
patch(id, {
status: "running",
frames: [],
selectedFrame: null,
stoppedThread: null,
suspendedThreads: [],
allThreadsStopped: false,
});
updateExecutionPoint();
break;
+2 -1
View File
@@ -32,6 +32,7 @@ import {
closeSessionTab,
evaluate,
isLive,
isThreadSuspended,
selectFrame,
selectSession,
selectThread,
@@ -406,7 +407,7 @@ function FramesPane({ session }: { session: DebugSession }) {
value: String(t.id),
label: t.name,
verbatim: true,
detail: t.id === session.stoppedThread ? "suspended" : undefined,
detail: isThreadSuspended(session, t.id) ? "suspended" : undefined,
}))}
onChange={(v) => void selectThread(session.id, +v)}
aria-label="Thread"