Skip to content

fix: temporary file leak in atomic_write_file #17505

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

Merged
merged 1 commit into from
Jul 21, 2025
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
19 changes: 15 additions & 4 deletions crates/fs-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,21 @@ where
let mut file =
File::create(&tmp_path).map_err(|err| FsPathError::create_file(err, &tmp_path))?;

write_fn(&mut file).map_err(|err| FsPathError::Write {
source: Error::other(err.into()),
path: tmp_path.clone(),
})?;
// Execute the write function and handle errors properly
// If write_fn fails, we need to clean up the temporary file before returning
match write_fn(&mut file) {
Ok(()) => {
// Success - continue with the atomic operation
}
Err(err) => {
// Clean up the temporary file before returning the error
let _ = fs::remove_file(&tmp_path);
return Err(FsPathError::Write {
source: Error::other(err.into()),
path: tmp_path.clone(),
});
}
}

// fsync() file
file.sync_all().map_err(|err| FsPathError::fsync(err, &tmp_path))?;
Expand Down