Skip to content

fix(llm): abandoned model-stream readers exit on every OS - #5138

Merged
kovtcharov-amd merged 5 commits into
mainfrom
fix/stall-reader-exits-macos
Oct 15, 2026
Merged

kovtcharov-amd merged 5 commits into
mainfrom
fix/stall-reader-exits-macos

Conversation

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

On macOS, a model stream ended by a stall or Stop left its reader thread stuck in recv with the connection open until the HTTP read timeout, which is the request budget plus 30 s (up to ~10 minutes), so every stall leaked a thread and a socket. That is what fails 6 macOS unit tests on main. Closing the socket from another thread doesn't wake that read on macOS. On every OS, a reader still waiting for headers (a local prefill, or a hosted reply that never starts) couldn't be reached at all. Now readers wait in 0.25 s slices and check an abandon flag between them, so they exit and close their own connection within a slice on any OS, whatever they were blocked on.

🔍 Technical details
  • A stack dump on the macOS runner showed the abandoned reader still in httpcore/_backends/sync.py self._sock.recv after shutdown(SHUT_RDWR) + close(). The same thread was still alive three tests later.
  • The fix wraps httpcore's network backend for the watchdog's client (_AbortableBackend / _AbortableStream). It sets the pool's _network_backend and raises loudly if that private attribute ever goes away. Nothing closes the socket from another thread now, so a freed descriptor can't be reused under a read in flight.
  • Not covered: a reader blocked in TCP connect to a blackholed remote host.

Test plan

  • python -m pytest tests/unit/test_stream_reader_abandon.py tests/unit/test_stream_stall_watchdog.py: the new file covers a reader blocked before headers, after headers, and mid-chunk, ended by a stall, by Stop, and by a consumer exception or close(). Each case asserts no thread is left, psutil sees no open socket, and the server sees the hang-up. It also checks that a slow live stream isn't cut and that the HTTP read timeout still fires. On main, the before-headers cases fail even on Windows.
  • python -m pytest tests/unit/test_broker_wiring.py tests/unit/test_lemonade_retry_fake_server.py tests/unit/test_llm_retry.py tests/unit/test_llm_retry_adversarial.py tests/unit/test_daemon_broker.py tests/unit/test_lemonade_chat_slots.py tests/unit/test_lemonade_client_http.py
  • Unit Tests (macOS, py3.12), (Linux, py3.12) and (Windows, py3.12) are green

@github-actions github-actions Bot added llm LLM backend changes tests Test changes performance Performance-critical changes labels Oct 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

When a model stream stalls or the user presses Stop, the background reader now actually stops and closes its connection within about a quarter second on every OS. Before, on macOS it held a thread and a socket for up to ~10 minutes. On any OS it couldn't be reached at all while still waiting for the first response headers. The approach fits the problem: each read waits in short slices and checks an "abandoned" flag, instead of closing a socket from another thread, which doesn't reliably wake the read. It fails loudly if a future httpx release hides the internal hook it relies on.

Nothing blocking. Two small optional cleanups are listed below.

Real-world evidence

The evidence stage ran the real CLI and a long-lived process against a fake OpenAI-compatible server (no inference on this runner):

Surface Result
CLI gaia llm, healthy stream ✅ printed violet-otter-92, exit 0, 1.68s
CLI gaia llm, stalled stream (idle limit 3s) ✅ actionable stall error after the partial frame; server saw the hang-up 3.3s after the stall began
Long-lived process, stalled stream ✅ LemonadeClientError after 3.04s; 0.11s later 0 gaia-model-stream threads and 0 open sockets, process still running
HTTP API / MCP / Agent UI N/A, not touched
RESULT LemonadeClientError after 3.04s; content so far='partial-'
AFTER 0.11s: reader threads=[] open sockets to server=0
process still alive; pid 2857
server log:  STALL_START 1792059016.65   CLIENT_HUNG_UP 1792059019.66

This backs the fix on Linux. The macOS behaviour the PR targets rests on the macOS unit-test lane, and real-inference validation is pending the strix-halo lane. The bundle also found that gaia llm follows the stall error with a misleading "Lemonade server is not installed" message and exits 0. That happens on the base revision too, so it's worth its own issue, not a blocker here.

🔍 Technical details

Issues

🟢 httpcore is now imported directly but isn't declared (src/gaia/llm/stream_watchdog.py:34). It comes in through httpx>=0.27,<1.0, which pins httpcore==1.*, so nothing breaks today. Because the module subclasses httpcore.NetworkStream/NetworkBackend and patches pool._network_backend, declaring httpcore>=1.0,<2.0 next to the httpx pins in setup.py would make that coupling explicit. The runtime guard in activity_http_client already covers the failure loudly.

🟢 Test server busy-loops if the client hangs up before finishing the request (tests/unit/test_stream_reader_abandon.py, SilentServer._serve). recv returns b"" on EOF, so while b"\r\n\r\n" not in request spins forever in a daemon thread and burns a core for the rest of the session. The happy path never hits it, but a regression that aborts mid-request would.

            while b"\r\n\r\n" not in request:
                data = conn.recv(65536)
                if not data:
                    self.client_hung_up.set()
                    return
                request += data

Checked and fine:

  • The client is built per attempt (lemonade_client.py _open_stream), so a sticky aborted flag can't poison later requests.
  • The worker's _AbandonedStream → httpx.ReadError → APIConnectionError lands in a queue nobody drains after the consumer has raised, so it never reaches the retrier.
  • The deadline math keeps the outer read timeout intact; test_the_read_timeout_still_fires_when_nothing_aborts pins this.
  • An explicit transport= disables env-proxy mounts, so no request skips the abortable backend.
  • Known gap, already named in the PR: TCP connect and the TLS handshake aren't sliced.

Strengths

  • The reader closes its own socket, so a descriptor can't be freed and reused under a read in flight. The old cross-thread shutdown/close had that race.
  • The tests go through a real socket and check three things: no leftover thread, psutil, and the server seeing the hang-up. They cover blocked-before-headers, after-headers, and mid-chunk across stall, Stop, consumer exception, and close(). They also check that a slow live stream isn't cut.
  • The private-attribute dependency fails loudly, with an actionable message, instead of silently going back to leaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm LLM backend changes performance Performance-critical changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant