Skip to content

studio: close the delete-vs-load races around the H3 companion repos - #8657

Merged
danielhanchen merged 5 commits into
mainfrom
fix/h3-claim-companion-repos-before-install
Aug 13, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fix/h3-claim-companion-repos-before-install

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 13, 2026 •

Copy link
Copy Markdown
Member

Summary

_loading.asset_repos is what stops the delete-cached guard admitting a delete of the H3 companion repos while a load is pulling from them. Its own comment says so.

#8560 moved the sd-cli preflight ahead of the four-file download, which is right. But the claim stayed below it, so the window the guard is blind to became the whole prebuilt install rather than the microseconds it used to be:

video.py:1528   binary = ensure_h3_sd_cpp_binary(...)     # may install; minutes
video.py:1595   self._loading.asset_repos = (H3_GGUF_REPO, H3_COMPONENT_REPO)

A delete admitted inside that window is not revoked by claiming the repos afterwards, so it can then remove cached files underneath the load that follows. Review found two narrower versions of the same check-then-act above and below that one, and this PR closes all three.

Changes

1. The claim moves above the preflight (video.py). expected_bytes stays where it is, since the sizes are not known any earlier.

2. begin_load publishes the claim with the loading state. Moving it to the top of the worker still left a gap: begin_load returns as soon as the thread is scheduled, so a delete arriving before that thread runs reads loading_repo_ids() with only repo_id and base_repo. The companion tuple now goes into the _VideoLoadingState constructor, inside the same locked section that publishes _loading. The worker-side claim stays as a no-op repeat for load_pipeline, which never goes through begin_load.

3. The delete guard is re-read after the scope is reserved (deletion.py). The guard ran before begin_delete, with the cache-key case resolution between them, and begin_delete could not see these loads either: video and image loads download directly rather than through a registry claim, so nothing marks them active. The guard is now re-derived once the reservation succeeds, which is the same reserve-then-re-derive the only_if_orphan branch below it already does for its own question.

Validation

  • test_h3_native_load_claims_the_companion_repos_before_the_preflight asserts the claim is visible from inside the preflight. Fails on main with assert [()] == [('unsloth/MiniMax-H3-GGUF', 'Comfy-Org/MiniMax-H3')].
  • test_begin_load_publishes_the_h3_companion_claim_with_the_loading_state stubs the load thread so it never starts and asserts both companions are already claimed; test_begin_load_claims_no_companion_repos_for_a_non_h3_family pins the other side. The first fails without change 2.
  • test_delete_cached_rechecks_the_load_guard_after_reserving_the_scope reports idle on the first guard read and loading on the second, and expects the delete refused. Fails without change 3.
  • test_video_backend.py, test_sd_cpp_backend.py, test_sd_cpp_engine.py, test_diffusion_engine_router.py, test_cached_gguf_routes.py, test_video_routes.py: all pass. Ruff clean.

Not in this PR

The remaining interval between the second guard read and the unlink needs load admission to take the claim_repository_owner reservation that begin_delete already refuses against. That changes download admission for the whole repo for the duration of a load and applies to chat and Images equally, so it belongs in its own change rather than here.

Supersedes #8570, which I closed: #8560 landed the preflight hoist, the cancel check and the unmanaged re-vet, and did the last of those better than my version (dropping the ownership test outright, and using a verdict form for the accelerator probe that does not fold "could not tell" into True). This ordering fix is the one thing that was left.

asset_repos is what stops the delete-cached guard admitting a delete of the H3 companion repos
while a load is pulling from them. #8560 moved the sd-cli preflight ahead of the four-file
download, which is right, but the claim stayed below it, so the window the guard is blind to is
now the whole prebuilt install: minutes, not microseconds. A delete admitted inside that window
is not revoked by claiming the repos afterwards, and it can then remove cached files underneath
the load that follows.

The base_repo and asset_repos claim moves above the preflight. expected_bytes stays where it is,
since the sizes are not known any earlier.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 9d2272f1d8

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

@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: ff1c46bb80

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

# registry claim, so nothing marks them active. Without this the claim is checked and then
# deleted around, which is the check-then-act the reservation exists to close.
try:
blocks_detail = await asyncio.to_thread(_load_state_blocks_delete)

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 Make load admission share the delete reservation

When an Images or Video load starts after this second read returns but before or during _delete_cached_model_blocking, the new load publishes its claim too late and deletion can still unlink files it is downloading or assembling. Fresh evidence in this revision is that the new check at line 830 is followed by the destructive dispatch at line 842 without any lock or reservation that VideoBackend.begin_load() must acquire; begin_delete() only excludes registry-backed downloads, while these loads bypass that registry. The added test covers a load appearing between the first and second reads, but not this remaining post-check interval, so admission needs a shared reservation rather than another point-in-time read.

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 here. The remaining window is real but it is the point-in-time guard design that every backend already shares, and it is not what this PR set out to change: the guard was minutes wide when a load could start during the sd-cli preflight, is now the recheck-to-unlink interval, and each narrowing has been inside the scope the previous one opened. The shared reservation you are describing does already exist, claim_repository_owner, and begin_delete refuses against it, so the correct closure is for load admission to take it rather than for the guard to be read a third time. That changes download admission for the whole repo for the duration of a load, applies to chat and Images equally rather than to the H3 path this PR touches, and needs its own tests, so it belongs in its own change rather than tacked onto this one.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: ff1c46bb80

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

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 9b5c0aab7d

ℹ️ 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 changed the title studio: claim the H3 companion repos before the preflight, not after it studio: close the delete-vs-load races around the H3 companion repos Aug 13, 2026
@danielhanchen
danielhanchen merged commit b4702b2 into main Aug 13, 2026
42 of 44 checks passed
@danielhanchen
danielhanchen deleted the fix/h3-claim-companion-repos-before-install branch August 13, 2026 11:41
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.

1 participant