Skip to content
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

Eagerly close tempfile to fix #1082 #1087

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion src/tempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ impl NamedTempfile {
pub(super) fn file(&self) -> &File {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this method, to fn take_file() -> Option<File>, so we don't need the close() method

self.file.as_ref().unwrap()
}

pub(super) fn close(&mut self) {
self.file.take();
}
}

impl Drop for NamedTempfile {
fn drop(&mut self) {
// On Windows you have to close all handle to it before
// removing the file.
self.file.take();
self.close();
let _ = remove_file(&self.path);
}
}
5 changes: 4 additions & 1 deletion src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Tool {
.into(),
})?;

let tmp =
let mut tmp =
NamedTempfile::new(&out_dir, "detect_compiler_family.c").map_err(|err| Error {
kind: ErrorKind::IOError,
message: format!(
Expand All @@ -134,6 +134,9 @@ impl Tool {
})?;
tmp.file()
.write_all(include_bytes!("detect_compiler_family.c"))?;
// Close the file handle *now*, otherwise the compiler may fail to open it on Windows
// (#1082). The file stays on disk and its path remains valid until `tmp` is dropped.
tmp.close();
Copy link
Collaborator

@NobodyXu NobodyXu Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also do a tmp.file().flush()?;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would call the Write::flush method, which for File is a no-op on all mainstream platforms including all that cc supports right now: Windows, Unix, and wasi. If you intend the call as precaution against adding a BufWriter or something in the future, sure, I can add it. However, if you want to surface I/O errors that would otherwise be detected while closing the file and ignored by dropping, we'll need File::sync_all as discussed in File's documentation. Which option do you prefer?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for investigating this, I'd like file.flush()?; and file.sync_data()?; to be called.

I don't think we need file.sync_metadata()?;, since it's a tempfile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


let stdout = run_output(
Command::new(path).arg("-E").arg(tmp.path()),
Expand Down