perf(app): the backend listens before it announces its port — a launch stops paying half a second - #506
Merged
brcampidelli merged 1 commit intoSep 17, 2026
Conversation
…h stops paying half a second Measured first (bench/startup, registered, US$ 0): spawn to bound 1.02 s, to a health answer 1.55 s, imports 0.90 s with no single one above 15% (litellm is not imported at boot), harness overhead per turn 16 ms, idle CPU 0.0 s, RSS 91 MB. The 0.53 s between the bind and the first answer was the client's connect, not the server: _bind_app_socket bound the socket and left listen() to uvicorn, the port file was written in between, and on Windows a SYN to a bound-but-not-listening socket is dropped, so the shell's first connect sat on the 500 ms retransmit timer on every launch. Now the socket listens before it is announced. Re-measured: spawn to health 1.03 s from 1.55 s (-34%); nothing else moved. Pinned by a test that connects with no server accepting and fails on either OS without the listen(). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
brcampidelli
deleted the
perf/the-app-socket-listens-before-the-port-is-announced
branch
September 17, 2026 07:09
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.
What
The desktop backend
listen()s on its socket before it writes the port file the Tauri shell waits on. One line in_bind_app_socket; a launch stops paying ~0.5 s on Windows (and one 150 ms retry elsewhere) for a connection that met a bound socket nobody was listening on yet. Item 8 of the list audited on 2026-09-16 ("Speed") — measured first, then the one fix the registered rule allowed.The measurement (
bench/startup, registered before it ran, US$ 0)Our own numbers, on this machine (i7 laptop, Windows 11, repo venv) — the marketing figures on the audited list were never ours and are not cited:
/api/healthanswersPredictions vs numbers, published as registered: cold start inside the 1.5–4 s range but the named culprit was wrong —
litellmis not imported at boot at all (already lazy); the boot is imports (0.90 of 1.02 s) spread overchimera.cli.main454 ms,httpcore131,fastapi123,code_api93, with nothing above 15%. Turn overhead predicted 30–150 ms, measured 16 ms. Idle held.Where the half second went
Not the server: its event loop was idle 10 ms after the port file was written (stack-sampled), and a request sent 1.5 s after the bind was answered in 12 ms. It was the client's connect.
_bind_app_socketbound the socket and leftlisten()to uvicorn; the port file — the shell's discovery channel — was written in between; and on Windows a SYN to a bound-but-not-listening socket is dropped, not refused, so the shell's firstconnect_timeout(500 ms)sat on the 500 ms SYN retransmit timer on every launch. On POSIX the SYN is refused andwait_for_listeningsleeps 150 ms and retries.bench/startup/probe_connect.pyis the diagnostic that found it: a raw-socket client inside the app's own boot —connect()0.51 s, request 6 ms.Not changed, by the rule
The registration said only a deferral or cache that changes no turn's behaviour, and only for an item above 20% of its total. Imports:
keyring(48 ms) loads the credential vault the first turn needs;importlib.metadata(71 ms) is imported by pydantic-settings regardless, so a lazy__version__saves nothing. The shell's 200 ms port-file poll (~100 ms average) is a 7–10% item — noted for the Rust side, not touched here. The packaged sidecar was not measured; the port-file sequence is the same code, so the fix applies to it identically.Tests
test_the_app_socket_listens_before_the_port_is_announced: a connect to the returned socket succeeds within 400 ms with no server accepting (the kernel completes the handshake from the backlog). Sabotage:listen()removed → the test fails on Windows (2 s timeout; on POSIX it would be refused); restored. Full gate on a clean copy in WSL: ruff clean, mypy clean (360 files), 6530 passed, 18 skipped, 10 xfailed in 184 s.🤖 Generated with Claude Code