Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions apps/server/src/terminal/Layers/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/terminal/Layers/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
Expand Down
21 changes: 21 additions & 0 deletions apps/web/src/components/ThreadTerminalDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ interface TerminalViewportProps {
terminalId: string;
cwd: string;
runtimeEnv?: Record<string, string>;
onSessionExited: () => void;
focusRequestId: number;
autoFocus: boolean;
resizeEpoch: number;
Expand All @@ -123,6 +124,7 @@ function TerminalViewport({
terminalId,
cwd,
runtimeEnv,
onSessionExited,
focusRequestId,
autoFocus,
resizeEpoch,
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -335,6 +344,16 @@ function TerminalViewport({
activeTerminal,
details.length > 0 ? `Process exited (${details})` : "Process exited",
);
if (hasHandledExitRef.current) {
return;
}
hasHandledExitRef.current = true;
window.setTimeout(() => {
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
if (!hasHandledExitRef.current) {
return;
}
onSessionExitedRef.current();
}, 0);
}
});

Expand Down Expand Up @@ -783,6 +802,7 @@ export default function ThreadTerminalDrawer({
terminalId={terminalId}
cwd={cwd}
{...(runtimeEnv ? { runtimeEnv } : {})}
onSessionExited={() => onCloseTerminal(terminalId)}
focusRequestId={focusRequestId}
autoFocus={terminalId === resolvedActiveTerminalId}
resizeEpoch={resizeEpoch}
Expand All @@ -800,6 +820,7 @@ export default function ThreadTerminalDrawer({
terminalId={resolvedActiveTerminalId}
cwd={cwd}
{...(runtimeEnv ? { runtimeEnv } : {})}
onSessionExited={() => onCloseTerminal(resolvedActiveTerminalId)}
focusRequestId={focusRequestId}
autoFocus
resizeEpoch={resizeEpoch}
Expand Down