Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,29 @@ fn change_setting(app: &mut App, forward: bool) {
fn finish_model_download(app: &mut App, outcome: DownloadOutcome) {
match outcome {
DownloadOutcome::Ready => {
if app.download_discard {
let result = app
.download_models
.iter()
.find(|model| model.file == app.download_file)
.ok_or_else(|| "downloaded model is no longer in the catalog".to_string())
.and_then(model::remove_model);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Selected model points to a removed file

When the worker commits the file while discard is pending and Enter is processed before completion is handled, start_download can select the committed file before checking whether a downloader is active, then finish_model_download removes it without clearing app.model_name; the next transcription uses a missing model file and fails.

Prevent the committed file from being selected while discard is pending, or clear the selection when removing it.

Prompt for AI agents
In `src/main.rs` at line 897:

**Selected model points to a removed file**

When the worker commits the file while discard is pending and Enter is processed before completion is handled, `start_download` can select the committed file before checking whether a downloader is active, then `finish_model_download` removes it without clearing `app.model_name`; the next transcription uses a missing model file and fails.

Prevent the committed file from being selected while discard is pending, or clear the selection when removing it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Completed model survives cancellation

When another process acquires the model’s cache lock after the downloader returns and before cleanup acquires it, model::remove_model cannot acquire the cache lock, so the completed file remains and the UI reports failure instead of honoring the discard request.

Keep the download lock held through cancellation cleanup, or retry cleanup after a transient lock conflict.

Prompt for AI agents
In `src/main.rs` at line 897:

**Completed model survives cancellation**

When another process acquires the model’s cache lock after the downloader returns and before cleanup acquires it, `model::remove_model` cannot acquire the cache lock, so the completed file remains and the UI reports failure instead of honoring the discard request.

Keep the download lock held through cancellation cleanup, or retry cleanup after a transient lock conflict.

app.download_discard = false;
app.download_paused = false;
match result {
Ok(()) => {
app.download = app::DownloadState::Idle;
app.download_info = "download canceled · completed file removed".into();
app.toast("download canceled", ToastKind::Warn);
}
Err(err) => {
app.download = app::DownloadState::Failed;
app.download_info = err.clone();
app.set_message(err);
}
}
return;
}
app.download = app::DownloadState::Done;
app.download_paused = false;
app.download_frac = 1.0;
Expand Down Expand Up @@ -1081,6 +1104,33 @@ mod tests {
assert_eq!(failed.unwrap_err(), "decoder failed");
}

#[test]
fn cancel_after_download_commit_removes_the_completed_model() {
let dir = std::env::temp_dir().join(format!("scribe-discard-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
crate::model::set_test_cache_dir(Some(dir.clone()));

let info = crate::model::find("tiny").unwrap();
let path = dir.join(info.file);
std::fs::write(&path, b"completed model").unwrap();

let mut app = App::new(&crate::cli::Args::default());
app.download_models = crate::model::CATALOG.to_vec();
app.download_file = info.file.to_string();
app.download_discard = true;
app.download_paused = true;

finish_model_download(&mut app, DownloadOutcome::Ready);

assert!(!path.exists());
assert!(!app.download_discard);
assert!(!app.download_paused);
assert_eq!(app.download, app::DownloadState::Idle);
crate::model::set_test_cache_dir(None);
std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn terminal_cleanup_continues_after_a_failed_step() {
use std::cell::RefCell;
Expand Down
5 changes: 5 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ thread_local! {
static TEST_CACHE_DIR: std::cell::RefCell<Option<PathBuf>> = const { std::cell::RefCell::new(None) };
}

#[cfg(test)]
pub(crate) fn set_test_cache_dir(dir: Option<PathBuf>) {
TEST_CACHE_DIR.with(|cache| *cache.borrow_mut() = dir);
}

/// What a `--model`/saved value can refer to. Keeping the three forms distinct
/// is what makes resolution unambiguous: a catalog name is never also treated
/// as a path just because a file with that name happens to exist in `$PWD`.
Expand Down