Skip to content

Commit f788761

Browse files
committed
Clear terminal output buffers after command completes
1 parent 966ed76 commit f788761

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/integrations/terminal/BaseTerminalProcess.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ export abstract class BaseTerminalProcess extends EventEmitter<RooTerminalProces
137137
*/
138138
abstract getUnretrievedOutput(): string
139139

140+
/**
141+
* Trims already retrieved content from the internal output buffer.
142+
*
143+
* This prevents unbounded memory growth when processing large
144+
* command outputs by discarding data that has already been
145+
* consumed by callers of `getUnretrievedOutput`.
146+
*/
147+
protected trimRetrievedOutput(): void {
148+
if (this.lastRetrievedIndex > 0) {
149+
this.fullOutput = this.fullOutput.slice(this.lastRetrievedIndex)
150+
this.lastRetrievedIndex = 0
151+
}
152+
}
153+
140154
protected startHotTimer(data: string) {
141155
this.isHot = true
142156

src/integrations/terminal/ExecaTerminalProcess.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export class ExecaTerminalProcess extends BaseTerminalProcess {
146146
this.emitRemainingBufferIfListening()
147147
this.stopHotTimer()
148148
this.emit("completed", this.fullOutput)
149+
this.lastRetrievedIndex = this.fullOutput.length
150+
this.trimRetrievedOutput()
149151
this.emit("continue")
150152
this.subprocess = undefined
151153
}

src/integrations/terminal/TerminalProcess.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ export class TerminalProcess extends BaseTerminalProcess {
254254
// so that api request stalls to let diagnostics catch up").
255255
this.stopHotTimer()
256256
this.emit("completed", this.removeEscapeSequences(this.fullOutput))
257+
this.lastRetrievedIndex = this.fullOutput.length
258+
this.trimRetrievedOutput()
257259
this.emit("continue")
258260
}
259261

0 commit comments

Comments
 (0)