Repository navigation
Studio: install the pinned Diffusers build before the backend imports what it replaces - #11635
Conversation
… what it replaces
…repair The startup repair killed the installer at 120s and then imported whatever was on disk, which could be a uv install stopped part way through replacing diffusers or huggingface_hub. Split the repair in two. --prefetch-diffusers-main fetches and builds the pinned build into uv's cache with --target pointing at a scratch directory, so stopping it at the deadline leaves site-packages untouched and startup continues as before. The install that follows runs from the cache (about a second measured on a cold cache, against about 10s for the fetch). If that install itself is ever stopped while this process holds the lock, startup now exits with a message instead of importing, and nothing is recorded so the next start finishes it. Also skip the in-process repair when huggingface_hub or diffusers is already imported (a notebook kernel calling run_server), since replacing files under loaded modules is the failure this repair exists to avoid. The timeout tests now let the fake installer spawn its child before the tree kill runs: the kill names descendants before signalling, so a child forked in between is missed, which made the peer test flaky on macOS.
The Popen patch is global, and on macOS the tree kill lists descendants with ps through it, so the recorded launches picked up those calls too.
The prefetch takes no lock, so an update or a sibling's repair can begin replacing packages while it runs. Its timeout now checks the pass lock like the install's does, instead of recording a failure and importing.
On macOS a pid's identity was "lstart comm". A framework python (python.org, the GitHub runners, Homebrew) re-execs into Python.app right after spawn, so the comm adopt_pid read no longer matches later: terminate_pid took the installer for a recycled pid, forgot it and signalled nothing, and only the Popen kill of the installer remained, orphaning uv. Compare lstart alone, as Linux already compares its start time alone for the same reason. Measured on macos-15 runners: the four repair timeout tests failed in 15 of 15 loops before and 0 of 15 after.
|
Review record for #11635, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#65. 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
|
|
Review record, round 1 (supplement). These two findings were raised on the mirror (danielhanchen/unsloth-staging-review#65) against the original head and were removed from it when settled threads were pruned, so they are missing from the record above. Quoted as posted by chatgpt-codex-connector[bot].
Accepted on the mirror. Fixed in 1e9f46e: the slow fetch and build moved to
Accepted on the mirror. Fixed in 1e9f46e: the in-process repair is skipped with a message when either package is already in |
…ai#11635's prefetch The ARM64 wheelhouse test counted _effective_requirements(req) and expected four. unslothai#11635's Diffusers prefetch is a fifth, and it has to filter the same way, since it builds the cache entry the install then reads. Name each caller and the file it passes instead, so a new caller fails with its own name.
…ponsive classes in the tests (unslothai#11680) * Let a scratch --target prefetch through the torch-probe installer check unslothai#11635's _prefetch_diffusers_main builds the pinned Diffusers into a scratch --target directory to warm uv's cache, so it installs nothing into the environment and cannot change which torch is there. The entry-point check counted any caller of _build_uv_cmd as an installer and failed main. A function whose every build command carries --target is now read as a scratch prefetch. Anything else that builds a command still has to drop the memoized torch classification. * Follow unslothai#11660's phone gutters and lg chat split in the desktop contract unslothai#11660 gave the Images, Audio and image-train rails a 20px gutter on phones (max-sm:px-5, max-sm:pl-5) and moved the chat split from md to lg, so tablets stack. Four contracts still pinned the old class strings. * Exempt only a --target into a directory the function just made with mkdtemp * Only a name bound once can prove its --target is the mkdtemp scratch * Hold the prefetch to a scratch target by running it, not by reading its source The AST classifier kept meeting spellings it could not see through: a rebound name, a second --target. Exempt _prefetch_diffusers_main by name instead, and prove the exemption by running it with the subprocess stubbed, through both the git and the archive source: exactly one --target, naming a directory it made under the temp root, gone afterwards. * Name the callers of the shared requirements filter, including unslothai#11635's prefetch The ARM64 wheelhouse test counted _effective_requirements(req) and expected four. unslothai#11635's Diffusers prefetch is a fifth, and it has to filter the same way, since it builds the cache entry the install then reads. Name each caller and the file it passes instead, so a new caller fails with its own name.
… what it replaces (unslothai#11635) * Studio: install the pinned Diffusers build before the backend imports what it replaces * Skip the startup repair when the installer would do nothing * Never import a half-replaced environment after a timed-out Diffusers repair The startup repair killed the installer at 120s and then imported whatever was on disk, which could be a uv install stopped part way through replacing diffusers or huggingface_hub. Split the repair in two. --prefetch-diffusers-main fetches and builds the pinned build into uv's cache with --target pointing at a scratch directory, so stopping it at the deadline leaves site-packages untouched and startup continues as before. The install that follows runs from the cache (about a second measured on a cold cache, against about 10s for the fetch). If that install itself is ever stopped while this process holds the lock, startup now exits with a message instead of importing, and nothing is recorded so the next start finishes it. Also skip the in-process repair when huggingface_hub or diffusers is already imported (a notebook kernel calling run_server), since replacing files under loaded modules is the failure this repair exists to avoid. The timeout tests now let the fake installer spawn its child before the tree kill runs: the kill names descendants before signalling, so a child forked in between is missed, which made the peer test flaky on macOS. * Count only installer launches in the repair timeout tests The Popen patch is global, and on macOS the tree kill lists descendants with ps through it, so the recorded launches picked up those calls too. * Stop startup when a peer's pass is running at a prefetch timeout The prefetch takes no lock, so an update or a sibling's repair can begin replacing packages while it runs. Its timeout now checks the pass lock like the install's does, instead of recording a failure and importing. * Match a macOS child by start time alone, so a timed-out repair is killed On macOS a pid's identity was "lstart comm". A framework python (python.org, the GitHub runners, Homebrew) re-execs into Python.app right after spawn, so the comm adopt_pid read no longer matches later: terminate_pid took the installer for a recycled pid, forgot it and signalled nothing, and only the Popen kill of the installer remained, orphaning uv. Compare lstart alone, as Linux already compares its start time alone for the same reason. Measured on macos-15 runners: the four repair timeout tests failed in 15 of 15 loops before and 0 of 15 after. --------- Co-authored-by: Daniel Han <[email protected]>
…ponsive classes in the tests (unslothai#11680) * Let a scratch --target prefetch through the torch-probe installer check unslothai#11635's _prefetch_diffusers_main builds the pinned Diffusers into a scratch --target directory to warm uv's cache, so it installs nothing into the environment and cannot change which torch is there. The entry-point check counted any caller of _build_uv_cmd as an installer and failed main. A function whose every build command carries --target is now read as a scratch prefetch. Anything else that builds a command still has to drop the memoized torch classification. * Follow unslothai#11660's phone gutters and lg chat split in the desktop contract unslothai#11660 gave the Images, Audio and image-train rails a 20px gutter on phones (max-sm:px-5, max-sm:pl-5) and moved the chat split from md to lg, so tablets stack. Four contracts still pinned the old class strings. * Exempt only a --target into a directory the function just made with mkdtemp * Only a name bound once can prove its --target is the mkdtemp scratch * Hold the prefetch to a scratch target by running it, not by reading its source The AST classifier kept meeting spellings it could not see through: a rebound name, a second --target. Exempt _prefetch_diffusers_main by name instead, and prove the exemption by running it with the subprocess stubbed, through both the git and the archive source: exactly one --target, naming a directory it made under the temp root, gone afterwards. * Name the callers of the shared requirements filter, including unslothai#11635's prefetch The ARM64 wheelhouse test counted _effective_requirements(req) and expected four. unslothai#11635's Diffusers prefetch is a fifth, and it has to filter the same way, since it builds the cache entry the install then reads. Name each caller and the file it passes instead, so a new caller fails with its own name.
After an update using an older installer, the first Qwen-Image-2.1 load can fail inside
huggingface_hub. Reported on Reddit on v0.1.814-beta:Restarting Studio clears it.
Cause
#11579 repairs the pinned Diffusers build in a background thread after the app imports
huggingface_hub. The build requireshuggingface-hub>=1.31.0, so repairing an older installation can replace hub files while its old modules remain insys.modules. Later imports mix versions and fail on names such ashttpxorflag_as_download_call.Fix
Run the repair synchronously before
from main import appinrun_server(), the shared entry point for direct, CLI, desktop and Colab starts.Healthy starts skip installation after checking metadata. Repairs print progress:
Split the repair in two, within 120 s overall (the desktop's startup deadline is 300 s):
--prefetch-diffusers-maindownloads and builds the pinned build into uv's cache with--target <scratch dir> --no-deps, so it never writes site-packages. On timeout its process tree is stopped. If a peer holds the install lock, startup exits with code 1 and asks the user to retry. Otherwisediffusers_main_repair: failedis recorded and startup continues; an explicitunsloth studio updateclears the key and retries.--repair-diffusers-mainthen installs from the cache (at least 30 s budget). If that install is stopped, the environment may be half replaced, so startup exits with code 1 instead of importing (same for a peer holding the lock), and nothing is recorded so the next start finishes it.Skip the repair when
huggingface_hubordiffusersis already imported in the process (for example a notebook kernel callingrun_server()), with a message to rununsloth studio updateand restart.Remove the background thread and its load guards, prewarm skip and restart message.
Keep
install_python_stack.py --repair-diffusers-mainunchanged, including its lock that serializes concurrent repairs.On macOS, match a tracked child by start time alone in
process_lifetime._same_identity, as Linux already does. A framework Python re-execs intoPython.appright after spawn, so the recordedcommstopped matching andterminate_pidsignalled nothing, leaving the installer's uv running after a timeout.Before and after
Tested via
run.pyin a copied Studio venv on CUDA/Linux, starting with PyPI Diffusers 0.40.0 and huggingface_hub 1.30.0, then loadingunsloth/Qwen-Image-2.1-GGUFQ4_K_M.POST /images/loadflag_as_download_callimportcannot import nameerrors in the logThe next start skipped installation and loaded the same model. Installation took 0.17 s with uv's cache and 8 s with an empty cache on this connection.
With a second process holding the real install lock for 200 s, the backend exited with code 1 and the retry message after 120 s.
Tests
test_diffusers_repair.py: 20 passed, covering import ordering, clean imports in a fresh interpreter, timeout cleanup, peer contention, failure recording and console messages.test_studio_pid_files.py: added coverage that the realrun_server()removes its startup marker on failure.tests/studio/install/test_diffusers_pin.py: 37 passed.git diff --checkpassed.Scope
maindirectly get no startup repair.Timeout safety (added during review)
Cold uv cache, release Diffusers 0.36.0 and hub 1.30.0 in a scratch venv on Linux:
--prefetch-diffusers-maindiffusers 0.36.0 hub 1.30.0, directory mtime unchanged)--repair-diffusers-mainafter itdiffusers 0.41.0.dev0 hub 1.32.0macos-15 runners, the four repair timeout tests looped 15 times: 15 of 15 loops had failures before the
_same_identitychange (the installer's child outlived the kill), 0 of 15 after.Staging CI (ubuntu-latest, macos-15, windows-latest) on the PR's own test files: Linux green. The remaining macOS and Windows failures are pre-existing on
mainand unchanged or reduced by this PR (base vs head, 3 runs each):maintest_a_real_unreaped_child_is_not_a_live_sibling5/5; 3 POSIX-only tests intest_orphaned_children.py3/3test_macos_style_orphans_are_recorded_and_reaped2/3,test_a_survivor_keeps_its_record_through_a_clean_shutdown1/3Upstream CI runs these suites on Linux only.