[codex] Bound local HTTP API concurrency#502
Conversation
|
@codex review |
There was a problem hiding this comment.
Pull request overview
This PR bounds the local loopback HTTP API’s per-connection worker concurrency to prevent unbounded OS thread growth under local stress traffic, while keeping normal behavior unchanged for typical clients.
Changes:
- Add a connection limiter capped at 16 concurrent active connections; reject excess connections with
503 {"error":"server_busy"}. - Add a 5s write timeout alongside the existing read timeout for connection handling.
- Add unit tests to verify permit release behavior and the 503 busy response shape/headers.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Codex Review: Didn't find any major issues. Delightful! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Another great contribution. Thank you so much. |
0ba118b to
7e8ab3f
Compare
robinebers
left a comment
There was a problem hiding this comment.
Hey! 👋 This is Rob's AI reviewer. Thanks for the careful follow-up to #487!
Really clean change overall — atomic CAS loop is correct, Drop-based permit release means no leaks, the 503 path preserves CORS/Connection: close, and the new tests cover both the limiter invariant and the response shape. Two small things before merge:
-
Document the new 503 in
docs/local-http-api.md— the "Error Responses" section currently listsprovider_not_found,not_found,method_not_allowedas the possible error codes. Worth addingserver_busyand a short note that the server may return503 Service Unavailablewhen the concurrent-connection cap is reached, so API consumers know to back off and retry rather than treat it as a hard failure. -
Drop the unused
#[derive(Clone)]onConnectionLimiter— the limiter is constructed once instart_serverand never cloned (permits hold their ownArc<AtomicUsize>), so the derive is dead. Tiny nit.
Everything else (timeouts, conservative cap, scope staying out of host_api.rs, test coverage) looks great.
|
Addressed both requested changes in the latest push:
Verified locally with:
Note: bun run build succeeds, but Vite prints the existing Node 22.0.0 version warning. |
|
Follow-up: updated the branch with current origin/main using a normal merge commit, no force-push. Re-verified after the merge:
The Vite Node 22.0.0 warning is still present, but the build exits successfully. |
Summary
503 {"error":"server_busy"}without spawning another worker when the cap is reachedWhy
This is a small follow-up slice of #487. The local API currently accepts each TCP connection and immediately spawns an OS thread for it. Under accidental or intentional local stress traffic, that can grow worker threads without a local app-side bound.
The new connection limiter keeps normal local API behavior unchanged for regular clients, while making overload explicit and bounded. This is intentionally independent from the probe-resource PR stack (#498/#499/#500) and avoids touching
host_api.rs, which already has active PRs.Notes
The local API is read-only and localhost-bound, so returning
503 server_busyduring overload is preferable to creating unbounded OS threads. The cap is deliberately conservative; normal use should be one or a few concurrent requests.Validation
cargo test local_http_api::server::testscargo test --lib -- --skip ccusage_timeout_kills_descendant_and_closes_pipesbun run buildnode ./node_modules/vitest/vitest.mjs runI skipped
ccusage_timeout_kills_descendant_and_closes_pipesin the broad Rust run because that unrelated flaky test is fixed separately in #501 but is not inmainyet.Part of #487.
Summary by cubic
Bound the local HTTP API to 16 concurrent connections and return 503 {"error":"server_busy"} without spawning a worker when at capacity. Added 5s read/write timeouts, tests for permit release and 503 JSON/headers, and updated docs to include
server_busywith backoff guidance.Written for commit 0708889. Summary will update on new commits. Review in cubic