Repository navigation
Studio: exit the inference worker instead of waiting to be killed - #16338
Merged
danielhanchen merged 3 commits intoNov 1, 2026
Merged
Conversation
The worker answered shutdown in ~0.35s, unload included, but the parent still found it alive after join(10) and logged 'Inference subprocess did not exit gracefully, terminating' 10-18s after a correct answer, then killed it. The time goes into teardown after the command loop returns: a watchdog thread that should have reported the stuck stack 6s later never ran, so the main thread sits in a native call holding the GIL. The models are already unloaded by then, and for GGUF unload_model has already killed the server its own atexit handler would stop, so the shutdown branch ends the process itself after flushing the response queue.
This was referenced Nov 1, 2026
Member
|
Review record for #16338, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#665. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1: reviewed mirror
|
danielhanchen
added a commit
that referenced
this pull request
Nov 3, 2026
…he stop (#16525) ## Summary `studio/backend/tests/test_inference_worker_shutdown_exit.py::test_shutdown_exit_still_relays_the_mirrored_stderr_tail` (added in #16338) fails intermittently in `(Python 3.13, g-r)`. In the last 30 PRs it hit #16495, #16496, #16498, #16500 and #16509: ``` assert "the last line before shutdown" in result.stderr E assert 'the last line before shutdown' in '' ``` That is a real product bug, not a flaky test. The worker can drop its last stderr lines when it exits, which are the lines the parent reports when a worker dies. **Cause.** `_tail_sink_to_stderr`, the mirror pump thread, decided it was done from two separate steps: a read that came back empty, then a check that `stop` was set. If the pump is descheduled between those two steps, the worker writes its last lines to the sink and calls `stop_worker_stderr_mirror()` in the gap. The pump then sees "empty read, and stop is set", exits, and never relays them. I instrumented the stop sequence in a child that runs the test's exact script, 900 times on 24 threads. On a failing run: - the sink held all 1720 bytes at stop time; - the pump made exactly one read, which returned empty and was logged after the stop; - it emitted 0 bytes, and both threads ended cleanly within 4 ms. So nothing timed out and nothing was cut off; the lines were simply never read. ## Change `studio/backend/utils/worker_stderr.py`, `_tail_sink_to_stderr`: the pump records `stop.is_set()` before each read and only finishes on an empty read that began after the stop was set. Bytes written before `stop.set()` are therefore always read. Nothing else changes: the poll interval, compaction, the relay, the join timeouts, and what a live worker mirrors. `studio/backend/tests/test_inference_worker_shutdown_exit.py`: new `test_the_pump_reads_once_more_after_an_empty_read_that_raced_the_stop`. It drives the pump with a reader whose first read returns empty and sets `stop` in that same call, so the race is forced deterministically rather than left to scheduling. ## Testing | check | main | this branch | |---|---|---| | instrumented child, 300 / 600 / 900 parallel runs | 1 lost the whole tail in each batch | 0 of 900 | | new deterministic test | fails (`b'' == b'the last line before shutdown\n'`) | passes | | `test_shutdown_exit_still_relays_the_mirrored_stderr_tail`, `--count 40 -n 16` | 1 failed of 40 | 240 of 240 passed (`--count 80 -n 16`, three times) | - `test_inference_worker_shutdown_exit.py` and `test_inference_worker_stderr_tail.py`: 77 passed.
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.
Fixes #16330.
Problem
unsloth inferenceprints its answer, then 10-18 seconds later:Exit code is 0, so the run succeeded; the delay and the warning are the teardown.
What is actually happening
Instrumenting the worker showed it is not slow to obey:
It unloads and acknowledges the shutdown in 0.34s, and then returns from its command loop — yet the parent's
join(timeout=10)still times out. So the time is not spent unloading; it is spent after the loop returns, in interpreter shutdown.A watchdog thread started just before the
return, which should have dumped every stack 6 seconds later, never ran at all. That means the main thread is inside a native call holding the GIL, not waiting on anything Python-level; the queue feeder thread is a daemon and cannot be the blocker either.Reproduced 7 of 8 runs on Windows 11 / RTX 5060 (torch 2.13.0+cu130), with one clean exit, so it is a race in teardown rather than a fixed delay.
Change
The shutdown branch now ends the process itself once the models are unloaded and the ack is sent, instead of returning and letting interpreter shutdown decide when to finish:
resp_queue.close()/join_thread()so theshutdown_ackis actually written before the process goes,stdout/stderr,os._exit(0).There is nothing left for interpreter shutdown to do at that point. The models are already unloaded, and for GGUF
unload_modelhas already killed thellama-serverthat the backend's ownatexithandler would otherwise stop — so skippingatexitdoes not orphan a server. The desktop app reaches this branch on every model switch as well, and it only ever benefits from the worker really dying instead of being terminated 10 seconds later.Verification
Same command, three runs with the change:
Answers are unchanged; the run is ~2 s shorter because the parent no longer waits out the join timeout before terminating.
Tests
studio/backend/tests/test_inference_worker_shutdown_exit.py(2 tests): the helper exits with 0 after closing the queue, and still exits when the queue is already closed.test_gpu_arbiter.pyand the other worker tests pass (96 total).