Repository navigation
fix: clean up failed fallback downloads #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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", | ||
|
|
@@ -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}"))?; | ||
| { | ||
| 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}" | ||
| )); | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, Surface or log cleanup failures so leftover partial files are visible and can be addressed. Prompt for AI agentsIn `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); | ||
|
|
@@ -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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Fetch cleanup regressions go undetected When cleanup calls are removed from Exercise the relevant Prompt for AI agentsIn `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())); | ||
|
|
||
There was a problem hiding this comment.
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 nomatchkeyword, so Rust rejects the fallback branch and the project cannot build.Add
matchbefore the command-spawn expression.Prompt for AI agents