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

feat: properly remove temporary files leftover after running tests #1762

Merged
merged 2 commits into from
Oct 2, 2024

Conversation

cryptonemo
Copy link
Collaborator

Take 2: It took a while to track down which tests were leaving around .tmp* directories and files in the TEMPDIR after the test suite was run, but it appears that this resolves them all across normal and 'ignored' tests.

In short, if there are any open files/dirs remaining when TempDir is scoped out, a deletion is silently omitted. This code more explicitly cleans up some temporary artifacts in order to ensure everything is removed and closed before that happens.

Comment on lines +187 to +189
if std::fs::remove_dir(cache_dir.path()).is_ok() && cache_dir.path().exists() {
let _ = cache_dir.close();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that I understand putting cache_dir.close() behind an if. fs::remove_dir will return Ok if cache_dir contains no files and is successfully removed (which would cause cache_dir.path().exists() to be false), or it will return an Err if cache_dir contains files.

Would something like the following also work without putting tempdir closing behind an if statement?

Suggested change
if std::fs::remove_dir(cache_dir.path()).is_ok() && cache_dir.path().exists() {
let _ = cache_dir.close();
}
let _ = std::fs::remove_dir_all(cache_dir.path());
let _ = cache_dir.close();

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pretty sure I tried that and it didn't work (would sometimes error on the second line).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, if you can find a different way to solve it, it would be appreciated. The goal is just for it to 1) not cause an error, and 2) be gone after the run. Took me a while to get that going and I agree it's weird (but works).

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, that is weird. If you've already tried alternatives to the if and the tests' temp-files weren't fully removed, then the PR looks good to me.

@cryptonemo cryptonemo merged commit 266acc3 into master Oct 2, 2024
10 checks passed
@cryptonemo cryptonemo deleted the cleanup-tmp-in-tests branch October 2, 2024 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants