Skip to content
Merged
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
48 changes: 40 additions & 8 deletions src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async fn fetch_async(url: &str, cancel: &Arc<AtomicBool>) -> Result<FetchedAudio
std::process::id(),
unique_suffix()
));
let mut child = tokio::process::Command::new(&ytdlp)
let mut child = match tokio::process::Command::new(&ytdlp)
.args([
"--no-playlist",
"--no-warnings",
Expand All @@ -184,20 +184,30 @@ async fn fetch_async(url: &str, cancel: &Arc<AtomicBool>) -> Result<FetchedAudio
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.map_err(|e| format!("could not download audio: {e}"))?;
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Critical · Fallback branch fails to compile

The block following .spawn() has no match keyword, so Rust rejects the fallback branch and the project cannot build.

Add match before the command-spawn expression.

Prompt for AI agents
In `src/fetch.rs` at line 187:

**Fallback branch fails to compile**

The block following `.spawn()` has no `match` keyword, so Rust rejects the fallback branch and the project cannot build.

Add `match` before the command-spawn expression.

Ok(child) => child,
Err(e) => {
cleanup_fallback(&fallback, &direct);
return Err(format!("could not download audio: {e}"));
}
};
let status = tokio::select! {
s = child.wait() => s.map_err(|e| format!("could not download audio: {e}"))?,
result = child.wait() => match result {
Ok(status) => status,
Err(e) => {
stop_child(&mut child).await;
cleanup_fallback(&fallback, &direct);
return Err(format!("could not download audio: {e}"));
}
},
_ = cancelled(cancel) => {
stop_child(&mut child).await;
let _ = std::fs::remove_file(&fallback);
let _ = std::fs::remove_file(&direct);
cleanup_fallback(&fallback, &direct);
return Err("cancelled".into());
}
};
let _ = child.wait().await;
if !status.success() || !fallback.is_file() {
let _ = std::fs::remove_file(&fallback);
let _ = std::fs::remove_file(&direct);
cleanup_fallback(&fallback, &direct);
return Err(format!(
"no audio came out. the http path said: {crate_err}"
));
Expand Down Expand Up @@ -262,6 +272,11 @@ pub fn ffmpeg_path() -> Option<PathBuf> {
p.is_file().then_some(p)
}

fn cleanup_fallback(fallback: &Path, direct: &Path) {
let _ = std::fs::remove_file(fallback);
let _ = std::fs::remove_file(direct);
Comment on lines +276 to +277

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 · Failed cleanup stays invisible

When the filesystem refuses to remove either partial file, remove_file errors are discarded, so a filesystem or permission failure leaves partial downloads in the audio cache without any indication to the caller; repeated failures can consume disk space.

Surface or log cleanup failures so leftover partial files are visible and can be addressed.

Prompt for AI agents
In `src/fetch.rs` at lines 276-277:

**Failed cleanup stays invisible**

When the filesystem refuses to remove either partial file, `remove_file` errors are discarded, so a filesystem or permission failure leaves partial downloads in the audio cache without any indication to the caller; repeated failures can consume disk space.

Surface or log cleanup failures so leftover partial files are visible and can be addressed.

}

/// remove a partial audio file left behind by a cancelled/interrupted fetch.
pub fn cleanup_audio(path: &Path) {
let _ = std::fs::remove_file(path);
Expand Down Expand Up @@ -305,6 +320,23 @@ mod tests {
);
}

#[test]
fn failed_fallback_cleanup_removes_both_partial_files() {
let dir = std::env::temp_dir().join(format!("scribe-fallback-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let fallback = dir.join("fallback.webm");
let direct = dir.join("direct.m4a");
std::fs::write(&fallback, b"partial").unwrap();
std::fs::write(&direct, b"partial").unwrap();

cleanup_fallback(&fallback, &direct);

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 · Fetch cleanup regressions go undetected

When cleanup calls are removed from fetch_async failure paths, the direct-cleanup tests call cleanup_fallback rather than fetch_async, so they pass when spawn, wait-error, cancellation, unsuccessful-exit, or fallback-error paths stop cleaning up, leaving regressions undetected.

Exercise the relevant fetch_async failure paths and assert that both partial files are removed.

Prompt for AI agents
In `src/fetch.rs` at line 333:

**Fetch cleanup regressions go undetected**

When cleanup calls are removed from `fetch_async` failure paths, the direct-cleanup tests call `cleanup_fallback` rather than `fetch_async`, so they pass when spawn, wait-error, cancellation, unsuccessful-exit, or fallback-error paths stop cleaning up, leaving regressions undetected.

Exercise the relevant `fetch_async` failure paths and assert that both partial files are removed.


assert!(!fallback.exists());
assert!(!direct.exists());
std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn cleanup_removes_partial_audio() {
let dir = std::env::temp_dir().join(format!("scribe-fetch-{}", std::process::id()));
Expand Down