Skip to content

fix(studio): release the inference subprocess's VRAM when its last model unloads - #8221

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
LeoBorcherding:fix/chat-subprocess-vram-leak
Aug 9, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
LeoBorcherding:fix/chat-subprocess-vram-leak

Conversation

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Fixes #8220.

The transformers inference subprocess stays alive after its last model unloads, holding several GB of VRAM that nothing reclaims. Measured on a 16 GB card: 3.89 GB stranded, card at 16.12 GB against 16.0 GB dedicated.

unload_model already calls clear_gpu_cache() in the child and it runs, so this is not a missing empty_cache(). The allocator can only return blocks nothing still references, and the accelerator context outlives every model regardless. The surviving reference is unidentified; this change fixes the layer that holds independent of it.

gpu_arbiter evicts between owners, and both the transformers orchestrator and the llama.cpp GGUF backend are owner CHAT, so switching between them is an intra-owner handoff no evictor covers. Training start and diffusion/video acquisition both already tear the subprocess down; this adds the third case.

The stranded VRAM is also invisible to the GGUF context fit, which probes torch.cuda.mem_get_info in the parent process, so the fit budgets against a card it reads as empty (GPUs free: [(0, 15939)] with 3.89 GB resident elsewhere). That probe is a separate issue and is not touched here.

Change

After a successful unload, if no models remain resident and no load is in flight, shut the worker down. The next load respawns it.

A teardown failure is logged, never surfaced: the unload has already succeeded at that point, and letting the exception reach the outer handler would report a successful unload as failed. (My first draft had exactly that bug; it broke four existing tests in test_orchestrator_unload_cancel.py, which is how it surfaced.)

Guarded on loading_models so a load already reusing the worker isn't killed.

Verification

tests/test_orchestrator_idle_subprocess_teardown.py   3 passed  (new)
tests/test_orchestrator_unload_cancel.py             60 passed
-k "orchestrator or arbiter"                        108 passed, 4 skipped

New tests cover the last-model teardown, no teardown while another model is resident, and no teardown while a load is in flight.

test_inference_orchestrator_crash_message.py::test_subprocess_crash_message_includes_signal_and_oom_hint fails on this branch and on a clean tree (verified by stashing), so it is pre-existing and unrelated. test_mcp_server.py and test_rag_ocr_fallback.py fail collection on missing optional deps, also pre-existing.

…del unloads

The child's unload frees the model and calls empty_cache, but the caching
allocator can only return blocks nothing still references, and the accelerator
context is never returned while the process lives. An idle worker therefore
kept its high-water mark.

gpu_arbiter evicts between owners, and the transformers orchestrator and the
llama.cpp GGUF backend are both CHAT, so switching between them is an
intra-owner handoff that no evictor covers. The stranded VRAM is also invisible
to the GGUF context fit, which probes its own process, so the fit budgets
against a card it reads as empty.

Shut the worker down once nothing is left to serve. A teardown failure is
logged, never surfaced as a failed unload.
@danielhanchen

Copy link
Copy Markdown
Member

Reviewed and pushed a small follow-up to this branch.

The change itself is correct and I could not find a way to break it:

  • No deadlock. The teardown runs while unload_model holds _gen_lock with _unload_pending set, and _shutdown_subprocess calls _stop_dispatcher, which takes _dispatcher_lifecycle_lock. That is the documented _gen_lock -> _dispatcher_lifecycle_lock order, and _wait_dispatcher_idle has already stopped the dispatcher by then. The finally is None safe against the teardown nulling _drain_event.
  • It costs nothing. load_model already kills and respawns the worker on every load, so the next load pays no extra spawn.
  • The transformers sidecar swap path stays safe. It requires unload_model to return True and then rechecks is_worker_alive(), and _shutdown_subprocess keeps the handle when the worker outlives SIGKILL, so a survivor still raises there.

What I pushed:

  1. Two tests that drive the real _shutdown_subprocess against a live spawned worker instead of stubbing it. The three existing tests stub the teardown, so they cannot catch a deadlock or a raise on that path. The first asserts the worker is dead, _proc is None, _unload_pending is cleared and _gen_lock is free afterwards. The second asserts a raising teardown still returns True. The first one fails on a clean tree, which pins [Bug] Studio: the inference subprocess keeps its VRAM after its last model is unloaded, and the GGUF context fit can't see it #8220 directly.
  2. Trimmed the comment block and the log line, same intent, fewer lines.

Test runs on the branch:

tests/test_orchestrator_idle_subprocess_teardown.py   5 passed
tests/test_orchestrator_unload_cancel.py             60 passed
-k "orchestrator or arbiter"                        111 passed, 2 skipped
full studio/backend suite                       18845 passed, 78 failed

Every one of those 78 also fails on clean main here (video backend, RAG with the sqlite extension and pymupdf missing, web, desktop auth, mcp). I diffed the node ids against a main run, so nothing fails because of this PR.

Agreed the VRAM probe and _CTX_FIT_VRAM_FRACTION = 0.97 are separate issues, please do file them.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 4bfeb3b1a6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit cdeea9c into unslothai:main Aug 9, 2026
35 of 40 checks 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.

[Bug] Studio: the inference subprocess keeps its VRAM after its last model is unloaded, and the GGUF context fit can't see it

2 participants