Skip to content

Commit c313200

Browse files
downloader: Manually drop file handle before deleting on error or cancel
1 parent 9f25867 commit c313200

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/downloader.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
209209
loop {
210210
tokio::select! {
211211
_ = &mut req.cancel => {
212+
drop(file); // Manually drop the file handle to ensure that deletion doesn't fail
212213
tokio::fs::remove_file(&req.destination).await?;
213214
return Err(Error::Io(std::io::Error::new(
214215
std::io::ErrorKind::Interrupted,
@@ -223,7 +224,11 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
223224
update_progress(bytes_downloaded, total_bytes);
224225
}
225226
Ok(None) => break,
226-
Err(e) => return Err(Error::Reqwest(e)),
227+
Err(e) => {
228+
drop(file); // Manually drop the file handle to ensure that deletion doesn't fail
229+
tokio::fs::remove_file(&req.destination).await?;
230+
return Err(Error::Reqwest(e))
231+
},
227232
}
228233

229234
}

0 commit comments

Comments
 (0)