Repository navigation
fix(terminal): stop the hidden-output restore re-arming itself after dispose - #14749
Conversation
📝 WalkthroughWalkthroughUpdated 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
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 winShorten 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
📒 Files selected for processing (2)
src/renderer/src/components/terminal-pane/pty-connection-post-dispose-restore-termination.test.tssrc/renderer/src/components/terminal-pane/pty-connection.ts
| 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 |
There was a problem hiding this comment.
🎯 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.
| 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 |
| 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) |
There was a problem hiding this comment.
🩺 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.
| 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) |
cef8b6f to
8f7b911
Compare
…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.
8f7b911 to
31398af
Compare
…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)
…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.
The bug
requestHiddenOutputRestoreIfNeededtracks its in-flight task with a.finallyhandler that re-arms the restore:The task body is
while (!disposed) { … }. Once the pane is disposed it exits immediately, so the promise settles at once, the.finallyruns, 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.tswas 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.tsdrives the restore into its armed state, disposes the pane, and counts how many times the chain re-readsisVisibleRef.current(the.finallyreads it on every re-arm, so it is a direct, deterministic measure — no timing or memory heuristics).Confirmed red before the fix:
and green after.
Verification
pnpm typecheck: exit 0pty-connectionsuites passNote
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.