Repository navigation
Conversation
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
marked this pull request as draft
December 3, 2026 09:20
Author
|
(Opening a PR was the wrong thing. I've created an issue for discussion instead.) |
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.
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_operationobjects 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 --traceputs them all in one chain of pending reads on the embedded terminal's pty channel, inLocalProcess(the SwiftTerm fork inLocalPackages/SwiftTerm).Cause
LocalProcessreads the pty with a streamDispatchIOchannel set tolowWater: 1, issuing reads ofreadSize(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).childProcessReadissued 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
done == true) callback, so exactly one read is in flight.EINTRandEAGAIN. Before, it re-issued on a non-final callback, where the read was still open anyway.readsInFlight, a small public counter, so a test can check the invariant.Tests
LocalProcessReadTests: a child shell prints 30 one-byte bursts 20 ms apart. Afterwards 1 read is in flight; with the old code, 31.swift testrun on macOS has the same failures asmain: 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_operationholding 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
startProcessWithForkptypath runs in my tests. TheSubprocesspath shares the same read handler, but the package has it compiled out.🤖 Generated with Claude Code