Skip to content

Studio: exit the inference worker instead of waiting to be killed - #16338

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
YakshithK:fix/inference-worker-exit
Nov 1, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
YakshithK:fix/inference-worker-exit

Conversation

@YakshithK

Copy link
Copy Markdown
Contributor

Fixes #16330.

Problem

unsloth inference prints its answer, then 10-18 seconds later:

2026-10-31 16:15:08 [warning  ] Inference subprocess did not exit gracefully, terminating

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:

[worker] shutdown received
[worker] unloaded unsloth/Llama-3.2-3B-Instruct-unsloth-bnb-4bit in 0.337s
[worker] ack sent, total 0.337s, returning now

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 the shutdown_ack is actually written before the process goes,
  • flush 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_model has already killed the llama-server that the backend's own atexit handler would otherwise stop — so skipping atexit does 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:

answered warning wall clock
before yes 4 of 4 33-35 s
after yes 0 of 3 31-33 s

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.py and the other worker tests pass (96 total).

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.
Copilot AI balanced review requested due to automatic review settings October 31, 2026 22:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@danielhanchen danielhanchen self-assigned this Nov 1, 2026
@danielhanchen

Copy link
Copy Markdown
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 0058960a51 (this PR's 7dc9a1a47)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 0058960a51

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 0058960a51 (this PR's 7dc9a1a47).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI: unsloth inference logs a shutdown warning and waits 10-18s after a successful answer

3 participants