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

Fix spyx_tmp() cleanup. #39201

Merged
merged 3 commits into from
Jan 18, 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
17 changes: 11 additions & 6 deletions src/sage/misc/temporary_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
# as the parent for all temporary files & directories created by them.
# This lets us clean up after those two functions when sage exits normally
# using an atexit hook
TMP_DIR_FILENAME_BASE = tempfile.TemporaryDirectory()
# Note that `TemporaryDirectory()` will cleanup on program exit;
# we keep the atexit hook to be redundant, in case that fails.
TMP_DIR_FILENAME_BASE = tempfile.TemporaryDirectory(prefix='sage_')
atexit.register(lambda: TMP_DIR_FILENAME_BASE.cleanup())


Expand Down Expand Up @@ -533,14 +535,17 @@ def spyx_tmp() -> str:
We cache the result of this function "by hand" so that the same
temporary directory will always be returned. A function is used to
delay creating a directory until (if) it is needed. The temporary
directory is removed when sage terminates by way of an atexit
hook.
directory is automatically removed when sage terminates.
"""
global _spyx_tmp
if _spyx_tmp:
return _spyx_tmp

d = tempfile.TemporaryDirectory()
_spyx_tmp = os.path.join(d.name, 'spyx')
atexit.register(lambda: d.cleanup())
# We don't use `tempfile.TemporaryDirectory()` here because it
# is not clear when it will or will not be cleaned. Sometimes it
# might not be cleaned up at all, and starting in python 3.13 it
# might be cleaned up on child exit, breaking parallel testing.
# For some reason this doesn't affect the `TemporaryDirectory`
Copy link
Contributor

@antonio-rojas antonio-rojas Dec 26, 2024

Choose a reason for hiding this comment

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

Using tempfile.TemporaryDirectory() works just fine for me, so this comment may not be completely correct (however, it is not cleaned up on exit of the parent process)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm.. so you mean TemporaryDirectory() is not cleaning up on program exit in general, or only in sage.misc.temporary_file.spyx_tmp()? What about sage.misc.temporary_file.TMP_DIR_FILENAME_BASE?

Copy link
Contributor

Choose a reason for hiding this comment

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

Only on spyx_tmp(). The TMP_DIR_FILENAME_BASE one is cleaned up correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, there seems to be something fragile about using TemporaryDirectory() on a local variable that is meant to survive its function. There are no clear rules about when this object will be destroyed (apparently the lambda: d.cleanup() grabs a reference that stays in the atexit hook, but I'm not sure if the lambda takes a strong reference).

I guess the "right" way to acomplish what was intended here might be to store d in the global variable, and not just d.name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I expanded the comment to mention that several different behaviors can happen.

@antonio-rojas can you have a look and maybe approve this? We are shipping this in the void linux package for sagemath 10.5.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, I'm shipping this too and have run the test suite in parallel a few times.

# stored in the global `TMP_DIR_FILENAME_BASE`.
_spyx_tmp = tmp_dir(name='spyx_')
return _spyx_tmp
Loading