Repository navigation
Description
Before submitting
- I searched existing issues and did not find a duplicate.
- I included enough detail to reproduce or investigate the problem.
Area
apps/server
Steps to reproduce
The server saves a cleaned copy of each terminal's output as its history and sends it to a client that attaches. While a control sequence is unfinished, the server holds every piece of PTY output until the sequence ends, so it can decide whether history keeps or strips the sequence. Nothing limits held text across OSC, DCS, PM, APC, CSI, and escape sequences in apps/server/src/terminal/Manager.ts.
- Open the terminal drawer in T3 Code and start
/bin/shwithPS1='demo$ '. - Run
seq 1 200000; printf 'DONE\n', thenprintf '\033['; seq 1 200000; printf 'mDONE\n'. - Run
seq 1 2000000; printf 'DONE\n', thenprintf '\033]0;stuck'; seq 1 2000000; printf '\007DONE\n'.
Each command ends its own sequence, so the plain prompt only matters for matching the times below.
Expected behavior
The second command in each pair takes about as long as the first. The server also limits held text.
While a control string is unfinished, history holds nothing after its introducer, the two characters that open the string. That matches what an already attached screen shows. A client attaching while a sequence is unfinished is a separate gap this issue does not cover.
Actual behavior
In the web client on main at 721542cac7, each command ran five times, and the times below are medians. Each timing measures from the Enter key to the first frame that shows DONE. Pair one took 0.24 s plain and 11.47 s after the unfinished CSI. Pair two took 2.12 s plain and 22.63 s after the unfinished OSC. The web runs printed DONE in black on green so the page could detect the frame, and used the prompt demo $ .
The server joins each piece to held text synchronously in the output loop. Each piece copies everything held so far, so total time grows with the square of output. For a CSI, the server also rescans the held text from the start. Held text grows with no limit. When the sequence ends, the server appends it whole to history. Its line feeds push earlier lines past the 5,000-line limit. History then drops what is left of the sequence because the trim cut through it. In a separate run of the server alone on a real /bin/sh PTY with no browser, only DONE and the demo$ prompt remained, 12 characters.
Impact
Minor bug or occasional failure. Reviewers found it during code review of another terminal fix. No user has reported it. Reviewers reproduced it on purpose.
A program must print one of six introducers and keep printing output that never ends it:
- OSC,
ESC ].BEL,CAN,SUB, orESCplus another character ends it. - DCS, PM, and APC,
ESC P,ESC ^, andESC _.CAN,SUB,ESCplus another character, or a character with a UTF-8 byte from0x80to0x9F, such asU+0100, ends them. - CSI,
ESC [.ESC,CAN,SUB, any byte from0x40to0x7E, which includes all ASCII letters, or a character with a UTF-8 byte from0x80to0x9Fends it. - Escape sequence,
ESC. Any character outside0x20to0x2F, the space and punctuation from!through/, ends it.
Only the four string kinds outlast a plain prompt. A prompt with an escape sequence ends them. A CSI stays unfinished only when output has no letters, such as seq, so even a plain demo$ prompt ends it. In five runs of that real PTY test on main on macOS, the longest event-loop stall was 24 to 32 ms for CSI and 10 to 16 ms for OSC, against 1 to 5 ms plain, so the server keeps answering.
Version or commit
main at 721542cac7.
Environment
macOS 27.2.1, Apple M5 Pro, Node 26.13.1, /bin/sh with a plain prompt. Web runs used headless Chromium against the dev stack.
The steps also reproduce on Linux in a Debian 12 Docker container on arm64 with dash as /bin/sh. On base 721542cac7, the web client medians of five runs were 133 ms plain and 3,794 ms after the unfinished CSI, and 878 ms plain and 3,844 ms after the unfinished OSC.
Related
#39861 fixes this bug.
#29604 and #30479 end an unfinished control string at the next ESC. This bug happens when no ESC follows. #23819 stopped rescanning a held string on each piece. #23817 noted copying the growing held string remained a separate cost.
The timing probe
This is a probe and not a regression test. It measures wall time, which the project's test rules forbid in committed tests.
Add it inside the TerminalManager suite in apps/server/src/terminal/Manager.test.ts, where createManager is in scope, run vp test run src/terminal/Manager.test.ts -t "TMP a string" from apps/server, and read /tmp/never-ending-string.txt.
it.effect("TMP a string that never ends", () =>
Effect.gen(function* () {
const lines: string[] = [];
const chunk = "0123456789abcde\n".repeat(1024);
for (const [label, introducer] of [
["no open string", ""],
["open OSC string", "\u001b]0;stuck"],
] as const) {
for (const megabytes of [4, 16, 32]) {
const { manager, ptyAdapter } = yield* createManager(100000);
yield* manager.open(openInput());
const pty = ptyAdapter.processes[0]!;
pty.emitData(`before\n${introducer}`);
const started = performance.now();
for (let index = 0; index < megabytes * 64; index += 1) pty.emitData(chunk);
lines.push(`${label}: ${megabytes} MiB took ${Math.round(performance.now() - started)} ms`);
yield* manager.close({ threadId: "thread-1", deleteHistory: true });
}
}
const fs = yield* FileSystem.FileSystem;
yield* fs.writeFileString("/tmp/never-ending-string.txt", lines.join("\n") + "\n");
}),
);One run on main at e37e45fd5b in 16 KiB pieces:
| Plain output | No unfinished string | After ESC ]0;stuck |
|---|---|---|
| 4 MiB | 16 ms | 101 ms |
| 16 MiB | 35 ms | 978 ms |
| 32 MiB | 68 ms | 3,599 ms |
Found by Claude Opus 5.5 and Claude Fable 5.2 during review of another terminal fix. Measured by Claude Fable 5.2.
Activity