Fix 100% CPU spin when MCP server child exits#1
Open
Dtensor wants to merge 1 commit intomacOS26:mainfrom
Open
Conversation
When a child MCP server process exits (or the write end of its pipe closes for any reason), NSFileHandle.readabilityHandler does not stop firing. availableData returns empty Data on every invocation, and the underlying dispatch source is re-armed immediately, so each leaked handler pegs one CPU core until the process is killed. Two leaked pipes (stdout + stderr) = 200% CPU forever. Diagnosed via `sample` — two `com.apple.NSFileHandle.fd_monitoring` serial queues burning ~100% CPU each in -[NSConcreteFileHandle availableData] -> fstat -> read(0 bytes) with the closure frames pointing at the two readabilityHandlers in StdioConnection.init. Fix: in both handlers, treat an empty availableData as EOF and set `handle.readabilityHandler = nil` to tear down the dispatch source. Also clear the reader's handler if `self` has deallocated — same dead-pipe scenario, just triggered by the client side going away. Behavior on live streams is unchanged; the empty-data branch is only taken on EOF.
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
NSFileHandle.readabilityHandlerkeeps firing after the child process exits —availableDatareturns emptyDataon every fire, the dispatch source re-arms immediately, and each leaked handler burns one CPU core.StdioConnection.init, both the stdout reader and the stderr drainer treat empty data as a no-op. When the MCP server crashes or closes its pipes, the two handlers spin forever at ~100% CPU each (200% total) until the host process is killed.availableDataas EOF and sethandle.readabilityHandler = nilto tear down the dispatch source. Also clear the reader's handler onself == nil(symmetric dead-pipe case when the client is gone).Repro
StdioConnection.SIGKILLon its pid, or any crash).sample <pid>shows twocom.apple.NSFileHandle.fd_monitoringserial queues spinning on-[NSConcreteFileHandle availableData] → fstat → read(0 bytes), with the two closures inStdioConnection.initon top of the stack.After fix
fd_monitoringhot threads insampleoutput.Behavior change
availableDatais the documented signal for EOF on an anonymousPipe(), so no false positives on real data.Test plan
main(2x NSFileHandle fd_monitoring at ~100% each).