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
3 changes: 3 additions & 0 deletions frontend/app/view/term/term.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ const TerminalView = ({ blockId, model }: ViewComponentProps<TermViewModel>) =>
model.termRef.current = termWrap;
setTermWrapInst(termWrap);
const rszObs = new ResizeObserver(() => {
if (termWrap.cachedAtBottomForResize == null) {
termWrap.cachedAtBottomForResize = termWrap.wasRecentlyAtBottom();
}
termWrap.handleResize_debounced();
});
rszObs.observe(connectElemRef.current);
Expand Down
44 changes: 44 additions & 0 deletions frontend/app/view/term/termwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export class TermWrap {
// xterm.js paste() method triggers onData event, which can cause duplicate sends
lastPasteData: string = "";
lastPasteTime: number = 0;
lastAtBottomTime: number = Date.now();
lastScrollAtBottom: boolean = true;
cachedAtBottomForResize: boolean | null = null;

constructor(
tabId: string,
Expand Down Expand Up @@ -226,6 +229,19 @@ export class TermWrap {
this.connectElem.removeEventListener("paste", pasteHandler, true);
},
});
const viewportElem = this.connectElem.querySelector(".xterm-viewport") as HTMLElement;
if (viewportElem) {
const scrollHandler = () => {
const atBottom = viewportElem.scrollTop + viewportElem.clientHeight >= viewportElem.scrollHeight - 20;
this.setAtBottom(atBottom);
};
viewportElem.addEventListener("scroll", scrollHandler);
this.toDispose.push({
dispose: () => {
viewportElem.removeEventListener("scroll", scrollHandler);
},
});
}
}

getZoneId(): string {
Expand Down Expand Up @@ -465,9 +481,30 @@ export class TermWrap {
}
}

setAtBottom(atBottom: boolean) {
if (this.lastScrollAtBottom && !atBottom) {
this.lastAtBottomTime = Date.now();
}
this.lastScrollAtBottom = atBottom;
if (atBottom) {
this.lastAtBottomTime = Date.now();
}
}

wasRecentlyAtBottom(): boolean {
if (this.lastScrollAtBottom) {
return true;
}
return Date.now() - this.lastAtBottomTime <= 1000;
}

handleResize() {
const oldRows = this.terminal.rows;
const oldCols = this.terminal.cols;
const atBottom = this.cachedAtBottomForResize ?? this.wasRecentlyAtBottom();
if (!atBottom) {
this.cachedAtBottomForResize = null;
}
this.fitAddon.fit();
if (oldRows !== this.terminal.rows || oldCols !== this.terminal.cols) {
const termSize: TermSize = { rows: this.terminal.rows, cols: this.terminal.cols };
Expand All @@ -478,6 +515,13 @@ export class TermWrap {
this.hasResized = true;
this.resyncController("initial resize");
}
if (atBottom) {
setTimeout(() => {
this.cachedAtBottomForResize = null;
this.terminal.scrollToBottom();
this.setAtBottom(true);
}, 20);
}
Comment on lines +518 to +524
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Clear pending resize timeout to prevent post-dispose terminal mutations.

Line 519 schedules async work, but the timer handle is not tracked or canceled. If dispose happens before it fires, callback code can still run and touch terminal state after teardown.

Suggested patch
 export class TermWrap {
@@
     cachedAtBottomForResize: boolean | null = null;
+    private resizeBottomTimeout: ReturnType<typeof window.setTimeout> | null = null;
@@
     dispose() {
+        if (this.resizeBottomTimeout != null) {
+            clearTimeout(this.resizeBottomTimeout);
+            this.resizeBottomTimeout = null;
+        }
         this.promptMarkers.forEach((marker) => {
             try {
                 marker.dispose();
@@
         if (atBottom) {
-            setTimeout(() => {
+            if (this.resizeBottomTimeout != null) {
+                clearTimeout(this.resizeBottomTimeout);
+            }
+            this.resizeBottomTimeout = window.setTimeout(() => {
                 this.cachedAtBottomForResize = null;
                 this.terminal.scrollToBottom();
                 this.setAtBottom(true);
+                this.resizeBottomTimeout = null;
             }, 20);
         }
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/app/view/term/termwrap.ts` around lines 518 - 524, The scheduled
resize timeout in the atBottom branch uses setTimeout but does not store the
timer id, allowing its callback to run after the terminal is disposed; update
the code around cachedAtBottomForResize so you assign the result of setTimeout
to a field (e.g., this.resizeTimeoutId or reuse cachedAtBottomForResize to hold
the timer id), clear any existing timeout before creating a new one, and ensure
the dispose/cleanup path clears that timer (clearTimeout(this.resizeTimeoutId))
so the callback (which calls this.terminal.scrollToBottom() and
this.setAtBottom(true)) cannot run after teardown.

}

processAndCacheData() {
Expand Down
Loading