fix: solve #2968 — skip writes to backpressured terminal sockets#2969
Closed
github-actions[bot] wants to merge 1 commit into
Closed
fix: solve #2968 — skip writes to backpressured terminal sockets#2969github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
When a client socket's write buffer was full (socket.write() returned false), broadcastEvent continued writing to it on subsequent data frames. This grew Node's internal buffer without bound. When the socket finally drained, the massive buffer flushed at once, causing a visible freeze/catch-up stall — especially for high-repaint TUIs like Droid. Fix: skip socket.write() for sockets already in the drain-wait set. The terminal emulator still processes all data (preserving snapshot state), so the next TUI repaint naturally resyncs the display. Closes #2968
Contributor
There was a problem hiding this comment.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="apps/desktop/src/main/terminal-host/session.ts">
<violation number="1" location="apps/desktop/src/main/terminal-host/session.ts:1074">
P1: The backpressure skip check drops `exit`/`error` events as well as `data`. Restrict skipping to `data` events so clients still receive terminal lifecycle/error notifications.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Comment on lines
+1074
to
+1076
| if (this.clientSocketsWaitingForDrain.has(socket)) { | ||
| continue; | ||
| } |
Contributor
There was a problem hiding this comment.
P1: The backpressure skip check drops exit/error events as well as data. Restrict skipping to data events so clients still receive terminal lifecycle/error notifications.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/desktop/src/main/terminal-host/session.ts, line 1074:
<comment>The backpressure skip check drops `exit`/`error` events as well as `data`. Restrict skipping to `data` events so clients still receive terminal lifecycle/error notifications.</comment>
<file context>
@@ -1065,10 +1065,18 @@ export class Session {
+ // buffer flush causes a visible freeze / catch-up stall (#2968).
+ // The data is still processed by the emulator, so snapshot state
+ // stays consistent and the next TUI repaint naturally resyncs.
+ if (this.clientSocketsWaitingForDrain.has(socket)) {
+ continue;
+ }
</file context>
Suggested change
| if (this.clientSocketsWaitingForDrain.has(socket)) { | |
| continue; | |
| } | |
| if ( | |
| eventType === "data" && | |
| this.clientSocketsWaitingForDrain.has(socket) | |
| ) { | |
| continue; | |
| } |
Kitenite
added a commit
that referenced
this pull request
Mar 29, 2026
…limit warnings Combines the fixes from #2969 and #2962 into a single changeset: 1. Skip writes to backpressured sockets (#2969): When a client socket signals backpressure (write returns false), subsequent broadcastEvent calls skip that socket entirely instead of growing Node's internal write buffer without bound. The terminal emulator still processes all data so snapshot state stays consistent — the next TUI repaint after drain naturally resyncs the display. 2. Rate-limit backpressure warnings (#2962): Replace unbounded console.warn on every backpressure event with a rate-limited warnBackpressure() method. Only one warning is emitted per 5-second window; subsequent occurrences are counted and reported in the next warning (e.g. '247 similar warnings suppressed'). Under sustained high-output commands, a single pane could previously generate 400k+ identical warnings flooding the daemon log. Tests cover: writes skipped during backpressure, writes resume after drain, warning rate-limiting within the 5s window, and suppressed count reporting after the window elapses. Closes #2969 Closes #2962 Co-Authored-By: Mastra Code (anthropic/claude-opus-4-6) <noreply@mastra.ai>
Kitenite
added a commit
that referenced
this pull request
Mar 29, 2026
…limit warnings Combines the fixes from #2969 and #2962 into a single changeset: 1. Skip writes to backpressured sockets (#2969): When a client socket signals backpressure (write returns false), subsequent broadcastEvent calls skip that socket entirely instead of growing Node's internal write buffer without bound. The terminal emulator still processes all data so snapshot state stays consistent — the next TUI repaint after drain naturally resyncs the display. 2. Rate-limit backpressure warnings (#2962): Replace unbounded console.warn on every backpressure event with a rate-limited warnBackpressure() method. Only one warning is emitted per 5-second window; subsequent occurrences are counted and reported in the next warning (e.g. '247 similar warnings suppressed'). Under sustained high-output commands, a single pane could previously generate 400k+ identical warnings flooding the daemon log. Tests cover: writes skipped during backpressure, writes resume after drain, warning rate-limiting within the 5s window, and suppressed count reporting after the window elapses. Closes #2969 Closes #2962 Co-Authored-By: Mastra Code (anthropic/claude-opus-4-6) <noreply@mastra.ai>
siarhei-belavus
pushed a commit
to siarhei-belavus/localset
that referenced
this pull request
Mar 30, 2026
* fix(desktop): skip writes to backpressured terminal sockets and rate-limit warnings Combines the fixes from superset-sh#2969 and superset-sh#2962 into a single changeset: 1. Skip writes to backpressured sockets (superset-sh#2969): When a client socket signals backpressure (write returns false), subsequent broadcastEvent calls skip that socket entirely instead of growing Node's internal write buffer without bound. The terminal emulator still processes all data so snapshot state stays consistent — the next TUI repaint after drain naturally resyncs the display. 2. Rate-limit backpressure warnings (superset-sh#2962): Replace unbounded console.warn on every backpressure event with a rate-limited warnBackpressure() method. Only one warning is emitted per 5-second window; subsequent occurrences are counted and reported in the next warning (e.g. '247 similar warnings suppressed'). Under sustained high-output commands, a single pane could previously generate 400k+ identical warnings flooding the daemon log. Tests cover: writes skipped during backpressure, writes resume after drain, warning rate-limiting within the 5s window, and suppressed count reporting after the window elapses. Closes superset-sh#2969 Closes superset-sh#2962 Co-Authored-By: Mastra Code (anthropic/claude-opus-4-6) <noreply@mastra.ai> * fix(desktop): preserve terminal lifecycle events under backpressure * fix(desktop): preserve terminal output under backpressure * test(desktop): trim terminal host session coverage * fix(desktop): remove terminal backpressure warning --------- Co-authored-by: Mastra Code (anthropic/claude-opus-4-6) <noreply@mastra.ai> (cherry picked from commit 8ff299c)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
broadcastEventin the terminal host session continued callingsocket.write()on client sockets that were already backpressured (i.e.,socket.write()had previously returnedfalse). This grew Node's internal write buffer without bound. When the socket finally drained, the massive accumulated buffer flushed to the client all at once, causing the visible freeze/catch-up stall reported in the issue — especially for high-repaint TUIs like Droid.socket.write()for sockets already in theclientSocketsWaitingForDrainset. The terminal emulator still processes all data (preserving snapshot state), so the next TUI repaint naturally resyncs the display. When the socket drains and subprocess stdout resumes, new data flows normally without a flood of stale buffered data.Test plan
bun test apps/desktop/src/main/terminal-host/session.test.ts— all 6 tests passCloses #2968
Summary by cubic
Skip writes to backpressured terminal sockets to prevent buffer growth and UI freezes in high-repaint TUIs. Addresses #2968 by letting the terminal recover immediately after drain.
broadcastEvent, skipsocket.write()for sockets inclientSocketsWaitingForDrain.Written for commit 5c7a881. Summary will update on new commits.