Skip to content

fix(terminal): queue one pty read at a time, not one per output chunk - #160

Closed
lime-red wants to merge 1 commit into
langwatch:mainfrom
lime-red:fix/swiftterm-pty-read-queue
Closed

lime-red wants to merge 1 commit into
langwatch:mainfrom
lime-red:fix/swiftterm-pty-read-queue

Conversation

@lime-red

@lime-red lime-red commented Dec 2, 2026

Copy link
Copy Markdown

Problem

Kanban Code's memory grows steadily while a card's terminal is open. On my machine a heap snapshot two hours apart showed about 8,300 new OS_dispatch_operation objects and as many new serial dispatch queues (com.apple.libdispatch-io.opq): about one a second, for as long as the terminal shows anything that updates (a tmux status line clock, Claude's spinner).

leaks --trace puts them all in one chain of pending reads on the embedded terminal's pty channel, in LocalProcess (the SwiftTerm fork in LocalPackages/SwiftTerm).

Cause

LocalProcess reads the pty with a stream DispatchIO channel set to lowWater: 1, issuing reads of readSize (128 KB). With a low water mark of 1, DispatchIO calls the read handler for every chunk that arrives (done == false) and once more when the read ends (done == true, after 128 KB in total, at EOF or on error).

childProcessRead issued a new 128 KB read on every handler call, the partial ones included. On a stream channel, reads run one after another, so each extra read waits behind the open one. And that read stays open until 128 KB more output arrives. So every small burst of output added one queued read (with its own dispatch queue) that wasn't reached for a long time. Output is still delivered in order, so nothing visibly breaks; the queue just keeps growing.

Fix

  • Queue the next read only from the final (done == true) callback, so exactly one read is in flight.
  • Treat an empty read as EOF only when it is the final one.
  • On a final callback with no data, re-issue only for EINTR and EAGAIN. Before, it re-issued on a non-final callback, where the read was still open anyway.
  • Add readsInFlight, a small public counter, so a test can check the invariant.

Tests

  • New LocalProcessReadTests: a child shell prints 30 one-byte bursts 20 ms apart. Afterwards 1 read is in flight; with the old code, 31.
  • The terminal suites pass.
  • The full swift test run on macOS has the same failures as main: timing-sensitive vault and shell suites that pass with --no-parallel, and the transcript-recompression test.

In the running app, with a terminal open, heap snapshots 2 h 15 min apart showed OS_dispatch_operation holding at 1 → 1 (the unfixed build went 218 → 8,504 in two hours) and serial queues at 245 → 330 (unfixed: 459 → 8,759).

Blind spots

  • Only the macOS startProcessWithForkpty path runs in my tests. The Subprocess path shares the same read handler, but the package has it compiled out.
  • Upstream SwiftTerm (migueldeicaza/SwiftTerm) appears to have the same unconditional re-read. If this looks right, it's worth fixing there too.

🤖 Generated with Claude Code

LocalProcess re-issued its 128 KB DispatchIO read from every handler
call, including the partial (done == false) ones that lowWater 1
produces. Each extra read queued behind the open one with its own
dispatch queue and only finished after 128 KB more output, so a
terminal with a ticking status line leaked about one read a second:
8,500 operations and 8,700 serial queues in two hours.

Queue the next read only from the final callback, and only treat an
empty read as EOF when it is final.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lime-red
lime-red marked this pull request as draft December 3, 2026 09:20
@lime-red lime-red closed this Dec 3, 2026
@lime-red

lime-red commented Dec 3, 2026

Copy link
Copy Markdown
Author

(Opening a PR was the wrong thing. I've created an issue for discussion instead.)

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.

2 participants