Skip to content

fix(terminal): stop the hidden-output restore re-arming itself after dispose - #14749

Merged
nwparker merged 1 commit into
mainfrom
nwparker/pty-connection-hidden-restore-loop
Aug 15, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/pty-connection-hidden-restore-loop

Conversation

@nwparker

@nwparker nwparker commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 +154 0 +154
Prod 1 +6 0 +6

The bug

requestHiddenOutputRestoreIfNeeded tracks its in-flight task with a .finally handler that re-arms the restore:

trackedHiddenOutputRestore = hiddenOutputRestoreTask.finally(() => {
  …
  requestHiddenOutputRestoreIfNeeded()   // re-arms the same chain
})

The task body is while (!disposed) { … }. Once the pane is disposed it exits immediately, so the promise settles at once, the .finally runs, re-arms, and the new task exits immediately too. That is an unbounded self-feeding promise chain: it consumed ~4GB in ~12s and starved the microtask queue.

The pane is gone at that point and there is nothing to restore, so the handler now returns early when disposed.

Why it never surfaced before

The pre-split pty-connection.test.ts was 25,841 lines, and a test that ran later in the file happened to tear the loop down. Splitting it into focused suites left the arming tests at the end of a file, which turned this into a reproducible OOM — that is how it was found.

Test

pty-connection-post-dispose-restore-termination.test.ts drives the restore into its armed state, disposes the pane, and counts how many times the chain re-reads isVisibleRef.current (the .finally reads it on every re-arm, so it is a direct, deterministic measure — no timing or memory heuristics).

Confirmed red before the fix:

AssertionError: expected 196 to be less than 50
FATAL ERROR: … JavaScript heap out of memory

and green after.

Verification

  • Full suite: 52,813 passing, 0 failures
  • pnpm typecheck: exit 0
  • All 580 pty-connection suites pass

Note

This also removes the ordering constraint flagged in #14728: the split files kept the arming tests upstream of the test that neutralized the loop. With the source fixed, that ordering is no longer load-bearing. I left the current arrangement alone rather than churn the files in the same PR.

@coderabbitai

coderabbitai Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Updated connectPanePty so hidden-output restore finalization stops when the pane is disposed. Added a Vitest regression test that mocks PTY dependencies, simulates hidden and visible output, disposes the pane binding, flushes asynchronous work, and verifies that visibility checks remain bounded.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug, fix, regression test, and verification, but it omits several required template sections, including the linked issue and visual proof. Add the required template sections, especially “Fixes #”, visual proof or N/A with a reason, testing checkboxes, AI disclosure, review, checklist, and author details.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the primary fix to stop hidden-output restoration from re-arming after disposal.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/pty-connection-post-dispose-restore-termination.test.ts (1)

86-90: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Shorten the test comments.

Replace these multi-line explanations with concise one-line rationale comments. The test name already describes the regression.

Proposed update
-  // Why this exists: the hidden-output restore task used to re-arm itself from its
-  // own `.finally`. After dispose the task body exits immediately, so the handler
-  // re-ran instantly and re-armed again — an unbounded self-feeding promise chain
-  // that consumed ~4GB in ~12s. It only stayed invisible because the pre-split
-  // 25k-line suite happened to run a later test that tore the loop down.
+  // Regression: disposal must stop hidden-output restore re-arming.
@@
-    // Why a counting getter: the restore task's `.finally` reads isVisibleRef.current
-    // on every re-arm, so read count is a direct, deterministic measure of how many
-    // times the chain cycled — no timing or memory heuristics needed.
+    // Count visibility reads to detect unexpected restore re-arming.

As per coding guidelines, comments must be concise, non-obvious, and brief.

Also applies to: 118-120

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d136fb26-0308-4da9-9e58-99a9f96d51d7

📥 Commits

Reviewing files that changed from the base of the PR and between 5b7f442 and cef8b6f.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/pty-connection-post-dispose-restore-termination.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts

Comment on lines +134 to +147
const disposable = connectPanePty(pane as never, createManager(1) as never, deps as never)
await flushAsyncTicks(6)

// Arm the restore: hidden bytes land while the pane is hidden, then it is revealed.
capturedDataCallback.current?.(hidden, { seq: hidden.length, rawLength: hidden.length })
visible = true
capturedDataCallback.current?.(live, {
seq: hidden.length + live.length,
rawLength: live.length
})
await flushAsyncTicks(20)

disposable.dispose()
const readsAtDispose = visibilityReads

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Establish the restore path before disposal.

The optional capturedDataCallback.current?.(...) calls silently skip both PTY events when setup does not register the callback. The final assertion can then pass with zero visibility reads. Fail when the callback is absent. Assert that the restore path read visibility before disposal.

Proposed update
     const disposable = connectPanePty(pane as never, createManager(1) as never, deps as never)
     await flushAsyncTicks(6)
+    const onData = capturedDataCallback.current
+    if (!onData) {
+      throw new Error('PTY data callback was not registered')
+    }
 
     // Arm the restore: hidden bytes land while the pane is hidden, then it is revealed.
-    capturedDataCallback.current?.(hidden, { seq: hidden.length, rawLength: hidden.length })
+    onData(hidden, { seq: hidden.length, rawLength: hidden.length })
     visible = true
-    capturedDataCallback.current?.(live, {
+    onData(live, {
       seq: hidden.length + live.length,
       rawLength: live.length
     })
     await flushAsyncTicks(20)
+    expect(visibilityReads).toBeGreaterThan(0)
 
     disposable.dispose()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const disposable = connectPanePty(pane as never, createManager(1) as never, deps as never)
await flushAsyncTicks(6)
// Arm the restore: hidden bytes land while the pane is hidden, then it is revealed.
capturedDataCallback.current?.(hidden, { seq: hidden.length, rawLength: hidden.length })
visible = true
capturedDataCallback.current?.(live, {
seq: hidden.length + live.length,
rawLength: live.length
})
await flushAsyncTicks(20)
disposable.dispose()
const readsAtDispose = visibilityReads
const disposable = connectPanePty(pane as never, createManager(1) as never, deps as never)
await flushAsyncTicks(6)
const onData = capturedDataCallback.current
if (!onData) {
throw new Error('PTY data callback was not registered')
}
// Arm the restore: hidden bytes land while the pane is hidden, then it is revealed.
onData(hidden, { seq: hidden.length, rawLength: hidden.length })
visible = true
onData(live, {
seq: hidden.length + live.length,
rawLength: live.length
})
await flushAsyncTicks(20)
expect(visibilityReads).toBeGreaterThan(0)
disposable.dispose()
const readsAtDispose = visibilityReads

Comment on lines +148 to +152
await flushAsyncTicks(200)

// A terminated chain settles in a handful of turns; the self-feeding loop grew
// once per microtask turn and would blow past this by orders of magnitude.
expect(visibilityReads - readsAtDispose).toBeLessThan(50)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Fail before the regression exhausts the test worker.

The PR objective reports worker OOM at 196 pre-fix cycles. This test waits 200 microtasks before its assertion. A regression can exhaust the worker before the assertion runs. Use fewer ticks and a lower bound that still distinguishes the one-read-per-turn loop.

Proposed update
-    await flushAsyncTicks(200)
+    await flushAsyncTicks(20)
@@
-    expect(visibilityReads - readsAtDispose).toBeLessThan(50)
+    expect(visibilityReads - readsAtDispose).toBeLessThan(10)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
await flushAsyncTicks(200)
// A terminated chain settles in a handful of turns; the self-feeding loop grew
// once per microtask turn and would blow past this by orders of magnitude.
expect(visibilityReads - readsAtDispose).toBeLessThan(50)
await flushAsyncTicks(20)
// A terminated chain settles in a handful of turns; the self-feeding loop grew
// once per microtask turn and would blow past this by orders of magnitude.
expect(visibilityReads - readsAtDispose).toBeLessThan(10)

…dispose

`requestHiddenOutputRestoreIfNeeded` tracked its in-flight task with a `.finally`
handler that re-armed the restore. The task body is `while (!disposed)`, so once
the pane is disposed it exits immediately, the handler runs at once and re-arms
again — an unbounded self-feeding promise chain that consumed ~4GB in ~12s and
starved the microtask queue.

The pane is gone at that point and there is nothing to restore, so the handler
now returns early when disposed.

This was only invisible because the 25k-line pty-connection suite happened to run
a later test that tore the loop down; splitting that file into focused suites left
the arming tests at the end of a file and turned it into a reproducible OOM.

The regression test drives the restore into its armed state, disposes the pane and
counts how many times the chain re-reads `isVisibleRef`. Before this change it
cycled 196 times and OOM-killed the worker; after, it settles immediately.
@nwparker
nwparker force-pushed the nwparker/pty-connection-hidden-restore-loop branch from 8f7b911 to 31398af Compare August 15, 2026 11:47
@nwparker
nwparker merged commit c0a7754 into main Aug 15, 2026
45 checks passed
AmethystLiang added a commit that referenced this pull request Aug 16, 2026
…dispose (#14749)

Cherry-pick production hunk only from c0a7754. Omit the new test file:
it imports #14728 fixtures that are not on this cut.

(cherry picked from commit c0a7754)
AmethystLiang pushed a commit that referenced this pull request Aug 19, 2026
…dispose (#14749)

`requestHiddenOutputRestoreIfNeeded` tracked its in-flight task with a `.finally`
handler that re-armed the restore. The task body is `while (!disposed)`, so once
the pane is disposed it exits immediately, the handler runs at once and re-arms
again — an unbounded self-feeding promise chain that consumed ~4GB in ~12s and
starved the microtask queue.

The pane is gone at that point and there is nothing to restore, so the handler
now returns early when disposed.

This was only invisible because the 25k-line pty-connection suite happened to run
a later test that tore the loop down; splitting that file into focused suites left
the arming tests at the end of a file and turned it into a reproducible OOM.

The regression test drives the restore into its armed state, disposes the pane and
counts how many times the chain re-reads `isVisibleRef`. Before this change it
cycled 196 times and OOM-killed the worker; after, it settles immediately.

(cherry picked from commit c0a7754)
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…dispose (stablyai#14749)

`requestHiddenOutputRestoreIfNeeded` tracked its in-flight task with a `.finally`
handler that re-armed the restore. The task body is `while (!disposed)`, so once
the pane is disposed it exits immediately, the handler runs at once and re-arms
again — an unbounded self-feeding promise chain that consumed ~4GB in ~12s and
starved the microtask queue.

The pane is gone at that point and there is nothing to restore, so the handler
now returns early when disposed.

This was only invisible because the 25k-line pty-connection suite happened to run
a later test that tore the loop down; splitting that file into focused suites left
the arming tests at the end of a file and turned it into a reproducible OOM.

The regression test drives the restore into its armed state, disposes the pane and
counts how many times the chain re-reads `isVisibleRef`. Before this change it
cycled 196 times and OOM-killed the worker; after, it settles immediately.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant