fix(mm): close TOCTOU race in _restore_incomplete_installs#9142
Open
fix(mm): close TOCTOU race in _restore_incomplete_installs#9142
Conversation
The restore path snapshotted active sources under the lock, then released the lock and iterated tmpdirs against the stale snapshot. A foreground import_model call landing during iteration could append a job the loop never saw, leading to a duplicate enqueue and a FileNotFoundError when the second download tried to rename the .downloading file the first had already moved. Move the membership check inside the lock immediately before the append so check-and-append is atomic. Fixes #9141. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_restore_incomplete_installssnapshotted active sources under the lock, then iterated tmpdirs against the stale snapshot. A foregroundimport_modelcall landing during iteration could be missed, the same source enqueued twice, and the second download would fail withFileNotFoundErrorwhen trying to rename the.downloadingfile the first download had already moved._install_jobs.append(job)so check-and-append is atomic. Duplicate-tmpdir suppression within a single restore pass stays outside the lock since it doesn't race againstimport_model.Fixes #9141.
Test plan
pytest tests/app/services/model_install/test_model_install.py::test_simple_download— ran 10× consecutively, all pass (was flaky ~30–50% onmain)pytest tests/app/services/model_install/— 31/31 pass🤖 Generated with Claude Code