Skip to content

Commit 995ec72

Browse files
fix: clean up failed fallback downloads (#13)
* fix: clean up failed fallback downloader processes * fix: match fallback process spawn result
1 parent b2ddf6c commit 995ec72

1 file changed

Lines changed: 40 additions & 8 deletions

File tree

‎src/fetch.rs‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ async fn fetch_async(url: &str, cancel: &Arc<AtomicBool>) -> Result<FetchedAudio
170170
std::process::id(),
171171
unique_suffix()
172172
));
173-
let mut child = tokio::process::Command::new(&ytdlp)
173+
let mut child = match tokio::process::Command::new(&ytdlp)
174174
.args([
175175
"--no-playlist",
176176
"--no-warnings",
@@ -184,20 +184,30 @@ async fn fetch_async(url: &str, cancel: &Arc<AtomicBool>) -> Result<FetchedAudio
184184
.stdout(std::process::Stdio::null())
185185
.stderr(std::process::Stdio::null())
186186
.spawn()
187-
.map_err(|e| format!("could not download audio: {e}"))?;
187+
{
188+
Ok(child) => child,
189+
Err(e) => {
190+
cleanup_fallback(&fallback, &direct);
191+
return Err(format!("could not download audio: {e}"));
192+
}
193+
};
188194
let status = tokio::select! {
189-
s = child.wait() => s.map_err(|e| format!("could not download audio: {e}"))?,
195+
result = child.wait() => match result {
196+
Ok(status) => status,
197+
Err(e) => {
198+
stop_child(&mut child).await;
199+
cleanup_fallback(&fallback, &direct);
200+
return Err(format!("could not download audio: {e}"));
201+
}
202+
},
190203
_ = cancelled(cancel) => {
191204
stop_child(&mut child).await;
192-
let _ = std::fs::remove_file(&fallback);
193-
let _ = std::fs::remove_file(&direct);
205+
cleanup_fallback(&fallback, &direct);
194206
return Err("cancelled".into());
195207
}
196208
};
197-
let _ = child.wait().await;
198209
if !status.success() || !fallback.is_file() {
199-
let _ = std::fs::remove_file(&fallback);
200-
let _ = std::fs::remove_file(&direct);
210+
cleanup_fallback(&fallback, &direct);
201211
return Err(format!(
202212
"no audio came out. the http path said: {crate_err}"
203213
));
@@ -262,6 +272,11 @@ pub fn ffmpeg_path() -> Option<PathBuf> {
262272
p.is_file().then_some(p)
263273
}
264274

275+
fn cleanup_fallback(fallback: &Path, direct: &Path) {
276+
let _ = std::fs::remove_file(fallback);
277+
let _ = std::fs::remove_file(direct);
278+
}
279+
265280
/// remove a partial audio file left behind by a cancelled/interrupted fetch.
266281
pub fn cleanup_audio(path: &Path) {
267282
let _ = std::fs::remove_file(path);
@@ -305,6 +320,23 @@ mod tests {
305320
);
306321
}
307322

323+
#[test]
324+
fn failed_fallback_cleanup_removes_both_partial_files() {
325+
let dir = std::env::temp_dir().join(format!("scribe-fallback-{}", std::process::id()));
326+
let _ = std::fs::remove_dir_all(&dir);
327+
std::fs::create_dir_all(&dir).unwrap();
328+
let fallback = dir.join("fallback.webm");
329+
let direct = dir.join("direct.m4a");
330+
std::fs::write(&fallback, b"partial").unwrap();
331+
std::fs::write(&direct, b"partial").unwrap();
332+
333+
cleanup_fallback(&fallback, &direct);
334+
335+
assert!(!fallback.exists());
336+
assert!(!direct.exists());
337+
std::fs::remove_dir_all(&dir).unwrap();
338+
}
339+
308340
#[test]
309341
fn cleanup_removes_partial_audio() {
310342
let dir = std::env::temp_dir().join(format!("scribe-fetch-{}", std::process::id()));

0 commit comments

Comments
 (0)