Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

refactor: implement retry logic for file replacement in embed_metadata

Tonho4e9d70407803
diff --git a/src-tauri/src/core/ffmpeg.rs b/src-tauri/src/core/ffmpeg.rs
index 5553d69c..4fede94c 100644
--- a/src-tauri/src/core/ffmpeg.rs
+++ b/src-tauri/src/core/ffmpeg.rs
@@ -513,9 +513,32 @@ pub async fn embed_metadata(
return Err(anyhow!("ffmpeg metadata failed: {}", stderr));
}
- tokio::fs::rename(&temp_output, file)
- .await
- .map_err(|e| anyhow!("Failed to replace file: {}", e))?;
+ let mut rename_ok = false;
+ for attempt in 0..3 {
+ match tokio::fs::rename(&temp_output, file).await {
+ Ok(()) => {
+ rename_ok = true;
+ break;
+ }
+ Err(e) if attempt < 2 => {
+ tracing::warn!(
+ "Failed to replace file (attempt {}): {}, retrying...",
+ attempt + 1,
+ e
+ );
+ tokio::time::sleep(std::time::Duration::from_millis(500 * (attempt as u64 + 1)))
+ .await;
+ }
+ Err(e) => {
+ let _ = tokio::fs::remove_file(&temp_output).await;
+ return Err(anyhow!("Failed to replace file after 3 attempts: {}", e));
+ }
+ }
+ }
+ if !rename_ok {
+ let _ = tokio::fs::remove_file(&temp_output).await;
+ return Err(anyhow!("Failed to replace file"));
+ }
Ok(())
}