Repository navigation
fix(llm): abandoned model-stream readers exit on every OS - #5138
Conversation
|
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 evidenceThe evidence stage ran the real CLI and a long-lived process against a fake OpenAI-compatible server (no inference on this runner):
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 🔍 Technical detailsIssues 🟢 🟢 Test server busy-loops if the client hangs up before finishing the request ( Checked and fine:
Strengths
|
On macOS, a model stream ended by a stall or Stop left its reader thread stuck in
recvwith 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
httpcore/_backends/sync.pyself._sock.recvaftershutdown(SHUT_RDWR)+close(). The same thread was still alive three tests later._AbortableBackend/_AbortableStream). It sets the pool's_network_backendand 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.connectto 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 orclose(). 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