Compare commits

...
Author SHA1 Message Date
Julius Marminge e6fd90df22 Guard delayed terminal exit callback
- Skip `onSessionExited` if exit handling was reset before timeout runs
- Prevent stale async exit callbacks from firing in terminal drawer
2026-02-27 18:02:09 -08:00
Julius Marminge dd5b985d26 Handle terminal exit writes and auto-close exited terminal view
- Ignore late write calls after a terminal session has already exited
- Add regression test for trailing writes after exit
- Close terminal drawer session once an exit event is handled
2026-02-27 17:55:47 -08:00
3 changed files with 39 additions and 0 deletions

No files matched your search

@@ -394,6 +394,21 @@ describe("TerminalManager", () => {
manager.dispose();
});
it("ignores trailing writes after terminal exit", async () => {
const { manager, ptyAdapter } = makeManager();
await manager.open(openInput());
const process = ptyAdapter.processes[0];
expect(process).toBeDefined();
if (!process) return;
process.emitExit({ exitCode: 0, signal: 0 });
await expect(manager.write({ threadId: "thread-1", data: "\r" })).resolves.toBeUndefined();
expect(process.writes).toEqual([]);
manager.dispose();
});
it("emits subprocess activity events when child-process state changes", async () => {
let hasRunningSubprocess = false;
const { manager } = makeManager(5, {
@@ -438,6 +438,9 @@ export class TerminalManagerRuntime extends EventEmitter<TerminalManagerEvents>
const input = decodeTerminalWriteInput(raw);
const session = this.requireSession(input.threadId, input.terminalId);
if (!session.process || session.status !== "running") {
if (session.status === "exited") {
return;
}
throw new Error(
`Terminal is not running for thread: ${input.threadId}, terminal: ${input.terminalId}`,
);
@@ -112,6 +112,7 @@ interface TerminalViewportProps {
terminalId: string;
cwd: string;
runtimeEnv?: Record<string, string>;
onSessionExited: () => void;
focusRequestId: number;
autoFocus: boolean;
resizeEpoch: number;
@@ -123,6 +124,7 @@ function TerminalViewport({
terminalId,
cwd,
runtimeEnv,
onSessionExited,
focusRequestId,
autoFocus,
resizeEpoch,
@@ -131,6 +133,12 @@ function TerminalViewport({
const containerRef = useRef<HTMLDivElement>(null);
const terminalRef = useRef<Terminal | null>(null);
const fitAddonRef = useRef<FitAddon | null>(null);
const onSessionExitedRef = useRef(onSessionExited);
const hasHandledExitRef = useRef(false);
useEffect(() => {
onSessionExitedRef.current = onSessionExited;
}, [onSessionExited]);
useEffect(() => {
const mount = containerRef.current;
@@ -306,6 +314,7 @@ function TerminalViewport({
}
if (event.type === "started" || event.type === "restarted") {
hasHandledExitRef.current = false;
activeTerminal.write("\u001bc");
if (event.snapshot.history.length > 0) {
activeTerminal.write(event.snapshot.history);
@@ -335,6 +344,16 @@ function TerminalViewport({
activeTerminal,
details.length > 0 ? `Process exited (${details})` : "Process exited",
);
if (hasHandledExitRef.current) {
return;
}
hasHandledExitRef.current = true;
window.setTimeout(() => {
if (!hasHandledExitRef.current) {
return;
}
onSessionExitedRef.current();
}, 0);
}
});
@@ -783,6 +802,7 @@ export default function ThreadTerminalDrawer({
terminalId={terminalId}
cwd={cwd}
{...(runtimeEnv ? { runtimeEnv } : {})}
onSessionExited={() => onCloseTerminal(terminalId)}
focusRequestId={focusRequestId}
autoFocus={terminalId === resolvedActiveTerminalId}
resizeEpoch={resizeEpoch}
@@ -800,6 +820,7 @@ export default function ThreadTerminalDrawer({
terminalId={resolvedActiveTerminalId}
cwd={cwd}
{...(runtimeEnv ? { runtimeEnv } : {})}
onSessionExited={() => onCloseTerminal(resolvedActiveTerminalId)}
focusRequestId={focusRequestId}
autoFocus
resizeEpoch={resizeEpoch}