Fix urgent interrupt input cleanup

This commit is contained in:
bincxz
2026-06-27 17:46:24 +08:00
parent 5080d62a8b
commit 0f85eaba8e
3 changed files with 56 additions and 0 deletions
@@ -79,6 +79,7 @@ import {
createTerminalInterruptTrace,
logTerminalInterruptTrace,
} from "./terminalInterruptDiagnostics";
import { clearTerminalInputStateForInterrupt } from "./terminalInterruptInputState";
import { getFlowControllerForTerm } from "./terminalSessionAttachment";
import { prioritizeTerminalInput } from "./terminalOutputPipeline";
import {
@@ -822,6 +823,11 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime
logTerminalInterruptTrace("renderer-keydown-send", interruptTrace, {
priority,
});
clearTerminalInputStateForInterrupt({
commandBufferRef: ctx.commandBufferRef,
serialLineBufferRef: ctx.serialLineBufferRef,
onAutocompleteInput: ctx.onAutocompleteInput,
});
if (ctx.terminalBackend.interruptSession) {
ctx.terminalBackend.interruptSession(id, interruptTrace);
} else {
@@ -0,0 +1,29 @@
import assert from "node:assert/strict";
import test from "node:test";
import { clearTerminalInputStateForInterrupt } from "./terminalInterruptInputState";
test("interrupt input state clearing matches the normal Ctrl+C input bookkeeping", () => {
const commandBufferRef = { current: "sudo apt" };
const serialLineBufferRef = { current: "pending serial input" };
const autocompleteInputs: string[] = [];
clearTerminalInputStateForInterrupt({
commandBufferRef,
serialLineBufferRef,
onAutocompleteInput: (data) => autocompleteInputs.push(data),
});
assert.equal(commandBufferRef.current, "");
assert.equal(serialLineBufferRef.current, "");
assert.deepEqual(autocompleteInputs, ["\x03"]);
});
test("interrupt input state clearing tolerates terminals without serial line mode", () => {
const commandBufferRef = { current: "echo pending" };
assert.doesNotThrow(() => {
clearTerminalInputStateForInterrupt({ commandBufferRef });
});
assert.equal(commandBufferRef.current, "");
});
@@ -0,0 +1,21 @@
type StringRef = {
current: string;
};
type InterruptInputStateOptions = {
commandBufferRef: StringRef;
serialLineBufferRef?: StringRef;
onAutocompleteInput?: (data: string) => void;
};
export function clearTerminalInputStateForInterrupt({
commandBufferRef,
serialLineBufferRef,
onAutocompleteInput,
}: InterruptInputStateOptions): void {
commandBufferRef.current = "";
if (serialLineBufferRef) {
serialLineBufferRef.current = "";
}
onAutocompleteInput?.("\x03");
}