Repository navigation
Studio: stop a quit during a model load from orphaning llama-server - #10592
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
378a864 to
0807658
Compare
0807658 to
407a681
Compare
|
@codex security review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
0e716ce to
e09be1d
Compare
e09be1d to
55e1ee6
Compare
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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() |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
55e1ee6 to
373f30c
Compare
373f30c to
4a7f7e9
Compare
4a7f7e9 to
c7e37cf
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
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.
c7e37cf to
5966c56
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
The bug
Quitting Studio while a model is loading can leave a
llama-serverchild 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:
_graceful_shutdown, but the backstop sweep is step 7;hub/utils/llm_assist.py,utils/datasets/llm_assist.py) builds its ownLlamaCppBackend, which no teardown step touches at all;A load crossing any of those gaps starts a server after
terminate_allhas taken its snapshot, so nothing reaps it.The fix
One flag, set before any subsystem is torn down and read at every spawn.
mark_process_shutting_down/is_process_shutting_downinprocess_lifetime, latched at step 0a_spawn_lockheld across the check, thePopenand the pid record, plus a recheck aftermark_process_shutting_downdoes not take that lock, so a child born in the gap is reaped instead of surviving a 600s health waitactive_model_namewithout testing livenessbegin_process_lifecycleclears the latchstudio/backend/colab.pycallsrun_serveragain in the same interpreter; a sticky flag would leave that second session refusing every spawnScope
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
maintoday. 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.pyandutils/torch_device_probe.py. That is a residual, stated rather than hidden:test_every_long_lived_spawner_consults_the_shutdown_latchderives 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 onstudio/teardown-restart-overlap-archiveif 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
studio/backendsuite on this head against a freshly-run current-mainbaseline: 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.torch,transformers,datasets,safetensors,requests,pymupdfortyper, 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.pyandtests/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.