Repository navigation
Serialize model cache downloads and removals - #2
Conversation
Reviewer's GuideAPIMake both public cache-removal operations acquire the per-model lock before deleting cached files, so they report contention without altering protected files.
ImplementationImplement persistent per-model OS locking, compatibility with legacy lock markers, safe lock-file opening, and lock acquisition before download setup.
UIReturn explicit download outcomes from the worker and handle completion, pause, cancellation, and failure in the TUI without temporary error files.
ConfigurationAdd the Unix rustix filesystem dependency needed to open cache-lock files safely.
TestsAdd a test-only cache-directory override and tests for removal contention, legacy-marker coordination, and rejection of invalid lock paths.
Lock files and vendored codeUpdate the dependency lockfile for the added Unix filesystem dependency.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
There was a problem hiding this comment.
Hey - I've found 5 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/model.rs" line_range="720" />
<code_context>
- Err(e) => Err(format!("cannot create download lock: {e}")),
+ .create(true)
+ .truncate(false)
+ .open(dir.join(format!(".{file}.cache.lock")))
+ .map_err(|e| format!("cannot open model cache lock: {e}"))?;
+ match lock.try_lock() {
</code_context>
<issue_to_address>
**Concurrent versions corrupt cached models**
When an older process and this version access the same model cache, `DownloadLock` opens `.{file}.lock` in the older version but `.{file}.cache.lock` here, so the locks do not contend and both processes can modify the same partial or finished model concurrently, leaving it corrupted or incomplete.
Use a lock path compatible with the older version for cache operations, and ensure this version uses that lock for downloads and removals.
</issue_to_address>
### Comment 2
<location path="src/model.rs" line_range="681" />
<code_context>
+ dir.join(info.file),
+ dir.join(format!("{}.part", info.file)),
+ dir.join(format!(".{}.error", info.file)),
+ ],
+ )
+}
</code_context>
<issue_to_address>
**Removed caches regain error notes**
When a download fails while removal runs or wins the lock race, the download caller writes the `.error` note after `download_from` releases `DownloadLock`, so `remove_cached_files` can delete the note before the UI reads it or finish first and have the caller recreate it; the UI shows a generic error or a removed cache retains stale error metadata.
Keep error-note creation and cache removal within the same per-model lock so the note cannot be lost or recreated outside the serialized operation.
Also at `src/model.rs:696`, `src/model.rs:722`.
</issue_to_address>
### Comment 3
<location path="src/model.rs" line_range="690-694" />
<code_context>
+ file: &str,
+ paths: impl IntoIterator<Item = PathBuf>,
+) -> Result<(), String> {
+ if !dir
+ .try_exists()
+ .map_err(|e| format!("cannot inspect model directory: {e}"))?
+ {
+ return Ok(());
+ }
+ let _lock = DownloadLock::acquire(dir, file)?;
</code_context>
<issue_to_address>
**Removal skips the shared lock**
When the cache directory does not yet exist and a download creates it while removal is running, `remove_cached_files` returns before acquiring `DownloadLock`, so a concurrent `download_from` can create the directory, acquire the lock, and leave a model in the cache after removal reports success.
Create the cache directory before acquiring the lock for removal, or otherwise synchronize the absent-directory case with downloads.
</issue_to_address>
### Comment 4
<location path="src/model.rs" line_range="720" />
<code_context>
- Err(e) => Err(format!("cannot create download lock: {e}")),
+ .create(true)
+ .truncate(false)
+ .open(dir.join(format!(".{file}.cache.lock")))
+ .map_err(|e| format!("cannot open model cache lock: {e}"))?;
+ match lock.try_lock() {
</code_context>
<issue_to_address>
**Model operations hang on FIFO lock**
When another local user who can write to the configured shared cache directory precreates the per-model lock path as a FIFO, `OpenOptions::open` blocks while opening the FIFO, before `try_lock` runs, so a removal call on the TUI thread hangs and downloads cannot acquire the lock.
Open the lock file in a way that cannot block on special files, and reject lock paths that are not regular files.
</issue_to_address>
### Comment 5
<location path="src/model.rs" line_range="1258" />
<code_context>
+ let lock = DownloadLock::acquire(&dir, file).unwrap();
+
+ for paths in [vec![partial.clone()], vec![model.clone(), partial.clone()]] {
+ let err = remove_cached_files(&dir, file, paths).unwrap_err();
+ assert!(err.contains("another scribe"));
+ }
</code_context>
<issue_to_address>
**Public removals go untested**
When either public removal function stops delegating to the helper, if `remove_partial` or `remove_model` stops delegating to `remove_cached_files`, this test still passes: at line 1258 it calls the helper directly, as it does for the later removal assertions, so broken public removals can leave cached files in place unnoticed.
Exercise both public removal functions using a test-configurable cache directory.
</issue_to_address>Sourcery assessment
Approval pending. 5 findings to address first.
Blocking findings: src/model.rs:720, src/model.rs:681, src/model.rs:694, src/model.rs:720, src/model.rs:1258
programmersd21
left a comment
There was a problem hiding this comment.
Thanks, Chris. I audited the patch against the existing cache/download flow. I agree with the five blocking findings already raised by Sourcery; they are real correctness gaps, not style concerns:
- The lock filename changes from
.{file}.lockto.{file}.cache.lock, so older Scribe processes do not contend with this version and can still modify the same cache concurrently. - The
.errornote is written outside the new lock lifetime. A concurrent removal can race with that write, deleting a note before it is read or allowing stale metadata to reappear after removal. remove_cached_filesreturns when the cache directory is absent without synchronizing with a concurrent download that may create it immediately afterward.- Opening the lock path can block if it is a FIFO or another special file. Validate that the lock path is a regular file using a nonblocking-safe open strategy before attempting the lock.
- The new test exercises
remove_cached_filesdirectly, not the publicremove_partialandremove_modelentry points. Add tests that call both public functions through a configurable cache directory.
Please address these together and add regression coverage for cross-version lock compatibility, error-note/removal ordering, the absent-directory race, special lock-file types, and both public removal APIs. I am holding approval until these are fixed and CI is green.
Keep the OS lock for current processes and guard legacy downloaders at the old lock path. Deliver download failures directly to the TUI and exercise both public removal methods.
programmersd21
left a comment
There was a problem hiding this comment.
The five earlier Sourcery findings are now marked addressed, and the new CI run has passed formatting, Clippy, and the Rust 1.90 MSRV check. The full test suite is still running, so this is not ready to merge yet.
I also found a separate regression in the latest src/main.rs patch: three user-facing strings that previously contained · now contain the mojibake text · (the pause status, pause toast, and cancel status). Please restore the intended UTF-8 separator in all three places. After that, let CI finish and rerun the final review before merging.
|
The latest CI run is fully green, and the five earlier locking findings are addressed. I found four remaining UTF-8 regressions in |
|
CI is now green on the current head, but this PR is not mergeable against the updated main after PR #1 was merged. The four mojibake separators are also still present in src/main.rs ( |
|
Ok I merged finally, everything is good to go! |
Summary
Use the same per-model file lock for downloads and both cache removal paths.
The OS releases the lock when the handle closes, so active downloads keep the
lock regardless of how long they take. Cache removal reports contention
without changing either the finished model or the partial download.
Validation
cargo fmt --checkcargo test --lockeda download, and removal after the lock is released.
Summary by Sourcery
Protect model cache downloads and removals with shared per-model locking.
Bug Fixes:
Enhancements:
Build:
Tests: