Skip to content

Studio: stop a quit during a model load from orphaning llama-server - #10592

Merged
danielhanchen merged 1 commit into
mainfrom
studio/quit-during-load-orphans-llama-server
Sep 9, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio/quit-during-load-orphans-llama-server

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

The bug

Quitting Studio while a model is loading can leave a llama-server child running after the app is gone.

Every subsystem already refuses to respawn its own child once its teardown step has run, but that state lives on the object being torn down, and a load in flight can reach a spawner that nothing marked:

  • the inference orchestrator is stopped at step 2 of _graceful_shutdown, but the backstop sweep is step 7;
  • a helper or advisor load (hub/utils/llm_assist.py, utils/datasets/llm_assist.py) builds its own LlamaCppBackend, which no teardown step touches at all;
  • a request sitting in the lifecycle gate or in preflight is not yet holding anything a per-object flag can see.

A load crossing any of those gaps starts a server after terminate_all has taken its snapshot, so nothing reaps it.

The fix

One flag, set before any subsystem is torn down and read at every spawn.

Change Why
mark_process_shutting_down / is_process_shutting_down in process_lifetime, latched at step 0a The answer stops depending on which object a spawn belongs to
_spawn_lock held across the check, the Popen and the pid record, plus a recheck after mark_process_shutting_down does not take that lock, so a child born in the gap is reaped instead of surviving a 600s health wait
Same check-and-reap on the inference worker spawn Same gap, same remedy
Shutdown cancels in-flight loads before the kill A load in the gate or in preflight would otherwise spawn after step 5 had run
The llama-server teardown is marked as a teardown, not a crash An in-flight health wait ends cleanly instead of reporting a startup failure
Publish paths refuse to advertise a model whose worker shutdown has killed The already-loaded fast path trusts active_model_name without testing liveness
begin_process_lifecycle clears the latch studio/backend/colab.py calls run_server again in the same interpreter; a sticky flag would leave that second session refusing every spawn

Scope

This fixes the orphan on quit. It deliberately does not try to make an embedded restart safe against a shutdown still running underneath it. An old session's work can still cross into a new one, exactly as it can on main today. That is a separate concern and is left for a follow-up rather than widened into this change.

It also does not gate every process in the backend that adopts a child. Ten modules are gated, the ten a quit during a model load can actually reach: the llama-server and inference-worker spawns, the three STT sidecars, the embed server, both sd-cpp paths, the export worker and both training worker spawns. Nine more adopt children and are left ungated: cloudflare_tunnel.py, core/data_recipe/jobs/manager.py, core/inference/diffusion_transformer_quant.py, core/inference/stt_download_worker.py, core/inference/tools.py, core/training/diffusion_training_service.py, utils/prebuilt/update_flow.py and utils/torch_device_probe.py. That is a residual, stated rather than hidden: test_every_long_lived_spawner_consults_the_shutdown_latch derives the adopter set from the tree and fails if a module is in neither list, so the next one that appears forces the decision instead of arriving as a bug report.

An earlier revision of this PR did attempt it, growing to ~3,500 lines across 15 files (lifecycle generations, an admission ContextVar, an ASGI stamp middleware, a lifecycle transition lock, generation-scoped sweeps). That work is preserved on studio/teardown-restart-overlap-archive if it is ever wanted, but it is a redesign of shutdown/restart lifecycle rather than a bug fix, and it does not belong in this PR.

Verification

  • Full studio/backend suite on this head against a freshly-run current-main baseline: 570 failed / 38,180 passed vs 570 failed / 38,172 passed, with zero unique failures in either direction. The 570 are identical on both trees; the extra passes are this branch's new tests.
  • Coverage limit, stated plainly: the environment used for that sweep cannot install torch, transformers, datasets, safetensors, requests, pymupdf or typer, so about two dozen modules do not import on either tree and their tests do not run. Valid as a differential, not as full coverage.
  • tests/test_orchestrator_unload_cancel.py and tests/test_llama_cpp_wait_for_health.py: 172 passing, covering the spawn-during-shutdown races, the teardown-vs-crash distinction, the post-spawn reap, and the load cancellation at shutdown.

Supersedes #10430, which is closed. That PR carried a long review history against a much larger earlier revision of this change (lifecycle generations, an admission ContextVar, an ASGI stamp middleware, a lifecycle transition lock). This one is the same fix at its final, narrowed scope, as a single commit, so the diff and the discussion match what is actually being proposed.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-09T15:38:46.900250Z 5966c56 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen
danielhanchen force-pushed the studio/quit-during-load-orphans-llama-server branch from 378a864 to 0807658 Compare September 9, 2026 07:20
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen
danielhanchen force-pushed the studio/quit-during-load-orphans-llama-server branch from 0807658 to 407a681 Compare September 9, 2026 07:33
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 407a681f98

ℹ️ 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 force-pushed the studio/quit-during-load-orphans-llama-server branch 2 times, most recently from 0e716ce to e09be1d Compare September 9, 2026 08:16
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen
danielhanchen force-pushed the studio/quit-during-load-orphans-llama-server branch from e09be1d to 55e1ee6 Compare September 9, 2026 08:35
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 55e1ee60e3

ℹ️ 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".

reuse the interpreter, and a latch that never cleared would refuse every spawn of
the second session.
"""
_shutdown_latch.clear()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve old-operation staleness when reopening the lifecycle

When an embedded host starts a second server while a helper/advisor, RAG, STT, or native-media operation from the previous server is still resolving or downloading, this unconditional clear makes every subsequent is_process_shutting_down() check return false. Those operations are not all represented in the chat load-attempt maps, and helper-owned LlamaCppBackend instances never receive the singleton's _shutting_down flag, so old work can resume at its guarded spawn and launch a model into the new session. Track a lifecycle generation captured when each operation begins, or drain/cancel all prior-lifecycle operations before clearing the shared latch.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not taking this one, and it is a scope decision rather than a disagreement about the window.

The remedy you name, a lifecycle generation captured per operation, is the design this PR deliberately does not carry. The description says so directly: it does not try to make an embedded restart safe against a shutdown still running underneath it, and an old session's work can still cross into a new one, exactly as it can on main today.

On the specific claim: main has no latch at all, so every is_process_shutting_down() check returns false there unconditionally. begin_process_lifecycle() clearing the latch restores that behaviour for the new server, it does not create a regression against main. The gap you describe is real, it is just not one this change widens.

Leaving it as a documented residual.

@danielhanchen
danielhanchen force-pushed the studio/quit-during-load-orphans-llama-server branch from 55e1ee6 to 373f30c Compare September 9, 2026 10:05
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen
danielhanchen force-pushed the studio/quit-during-load-orphans-llama-server branch from 4a7f7e9 to c7e37cf Compare September 9, 2026 11:10
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c7e37cfb5a

ℹ️ 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

Copy link
Copy Markdown
Member Author

Confirmed main has no shutdown latch in studio/backend/utils/process_lifetime.py, so a spawn crossing the teardown window still escapes the sweep in core/inference/llama_cpp.py. Will get this reviewed.

Quitting Studio while a model was loading could leave a llama-server child
running after the app was gone. Each subsystem already refused to respawn its
own child once its teardown step had run, but that state lives on the object
being torn down, and a load in flight can reach a spawner that nothing marked:
the orchestrator is stopped at step 2 and the backstop sweep runs at step 7, and
a helper or advisor load owns a LlamaCppBackend that no step touches at all. A
load crossing one of those gaps started a server after the sweep had already
taken its snapshot, so nothing reaped it.

One flag, set before any subsystem is torn down and read at every spawn:

- process_lifetime gains mark_process_shutting_down / is_process_shutting_down,
  latched at step 0a of _graceful_shutdown. Every spawn path consults it, so the
  answer no longer depends on which object a spawn happens to belong to.
- The llama-server spawn takes _spawn_lock across the check, the Popen and the
  pid record, and rechecks afterwards: mark_process_shutting_down does not take
  that lock, so a child born in the gap is reaped rather than left behind for the
  length of a 600s health wait. The inference worker spawn does the same.
- Shutdown cancels in-flight loads before the kill. A request sitting in the
  lifecycle gate or in preflight holds nothing the backend's own flag can see,
  and would otherwise spawn after step 5 had run.
- The llama-server teardown is marked as a teardown rather than a crash, so an
  in-flight health wait ends instead of reporting a startup failure, and the
  publish paths refuse to advertise a model whose worker shutdown has killed.
- begin_process_lifecycle clears the latch, because studio/backend/colab.py calls
  run_server again in the same interpreter and a sticky flag would leave that
  second session refusing every spawn.

Scope: this fixes the orphan on quit. It does not try to make an embedded
restart safe against a shutdown that is still running underneath it -- an old
session's work can still cross into a new one, exactly as it can on main today.
That is a separate concern and is left for a follow-up rather than widened into
this change.
@danielhanchen
danielhanchen force-pushed the studio/quit-during-load-orphans-llama-server branch from c7e37cf to 5966c56 Compare September 9, 2026 13:42
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 5966c56b46

ℹ️ 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 fcaad20 into main Sep 9, 2026
5 of 55 checks passed
@danielhanchen
danielhanchen deleted the studio/quit-during-load-orphans-llama-server branch September 9, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant