-
Notifications
You must be signed in to change notification settings - Fork 944
Fix: bound socket thread pool #2604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
NewAlexandria
wants to merge
2
commits into
manaflow-ai:main
Choose a base branch
from
NewAlexandria:fix/bound-socket-thread-pool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+67
−3
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| ## Summary | ||
|
|
||
| Replace unbounded `Thread.detachNewThread` for socket client handlers with a | ||
| bounded concurrent `DispatchQueue` and semaphore, preventing runaway thread | ||
| accumulation when the main thread is temporarily unresponsive. | ||
|
|
||
| ## Problem | ||
|
|
||
| Each incoming socket connection (e.g. `cmux claude-hook` calls from Claude Code | ||
| hooks) spawns a new `NSThread` via `Thread.detachNewThread`. When the main | ||
| thread stalls — for example during a heavy SwiftUI render pass — handler threads | ||
| that call `@MainActor`-isolated methods block on `DispatchQueue.main.sync`. | ||
| Because thread creation is unbounded, these blocked threads pile up indefinitely. | ||
|
|
||
| In a real incident this produced **150+ blocked threads** and a **10.93 GB** | ||
| memory footprint, making the app permanently unresponsive and unrecoverable | ||
| without a force kill. | ||
|
|
||
| ## Fix | ||
|
|
||
| - Add a private concurrent `DispatchQueue` (`com.cmuxterm.socket-clients`) for | ||
| client handler dispatch. | ||
| - Gate handler execution behind a `DispatchSemaphore(value: 64)` to cap | ||
| concurrent handlers. | ||
| - Handlers that cannot acquire a slot within 30 seconds return `ERROR: Server busy` | ||
| and close the connection, applying backpressure to callers instead of | ||
| accumulating memory. | ||
| - The accept loop's own `Thread.detachNewThread` (single long-lived thread) is | ||
| unchanged. | ||
|
|
||
| ## Test plan | ||
|
|
||
| - [ ] Verify normal `cmux` CLI commands still work (ping, report_*, etc.) | ||
| - [ ] Simulate main thread stall with a debug sleep and confirm thread count | ||
| stays bounded | ||
| - [ ] Confirm "Server busy" response when all slots are exhausted | ||
| - [ ] Run existing socket tests (`tests_v2/`) against a tagged debug build |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This timeout check runs only after the
asyncblock starts, so under a connection flood the accept loop still enqueues every accepted socket immediately and can accumulate a large backlog of open FDs before anyServer busyrejection happens. In the overload case where 64 handlers are blocked, queued closures may not execute promptly, which means the advertised 30s backpressure window is not enforced from accept time and the process can still hit FD/memory pressure before shedding load.Useful? React with 👍 / 👎.