Repository navigation
Shield TemporaryDirectory.cleanup() from cancellation - #1316
Merged
agronholm merged 11 commits intoOct 17, 2026
Merged
Conversation
TemporaryDirectory.cleanup() ran the synchronous cleanup via to_thread.run_sync, which performs a cancellation checkpoint on entry. If the host task was already cancelled when cleanup() was called (as it normally is, from a finally block), that checkpoint aborted the cleanup before the worker thread ran, leaking the temporary directory. agronholm#1304 fixed this same failure in __aexit__ by wrapping the call in a shielded CancelScope, but left cleanup() with the identical bug a few lines below. Apply the same fix to cleanup(), and add a regression test mirroring the one agronholm#1304 added for __aexit__.
Yasser-Ameur
marked this pull request as ready for review
September 9, 2026 00:06
3 tasks
agronholm
reviewed
Sep 13, 2026
…-shield # Conflicts: # docs/versionhistory.rst
TemporaryDirectory.cleanup() shields the worker-thread cleanup call from cancellation so the directory still gets removed, but returned without ever raising the cancellation back to the caller. Add a checkpoint_if_cancelled() after the shielded block, same as the other shield+checkpoint pairing in this file, so a pending cancellation is still delivered once the directory is gone. Extend the regression test to assert both.
Contributor
Author
|
Good catch, you're right. I've added |
agronholm
reviewed
Sep 29, 2026
agronholm
reviewed
Sep 29, 2026
agronholm
reviewed
Oct 2, 2026
2 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NOTE Erasing or replacing the contents of this template will result in your pull
request being summarily closed without consideration!
Changes
Fixes #.
TemporaryDirectory.cleanup()runs its cleanup throughto_thread.run_sync, whichcheckpoints on entry. When the host task is already cancelled, and it usually is,
because
cleanup()gets awaited from afinally:block during unwind, the checkpointaborts before the worker thread runs and the directory is left on disk.
#1304 fixed exactly this for
__aexit__with a shieldedCancelScope.cleanup(), afew lines below, kept the unshielded call. This gives it the same shield. The other
to_thread.run_syncteardown in the module,AsyncFile.aclose(), was already shieldedby #1314, so this was the last one.
test_cleanup_method_with_cancellationmirrors the test #1304 added for__aexit__.It fails on the unmodified tree on all four backend variants and passes with the fix.
Checklist
If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):
tests/) which would fail without your patchdocs/), in case of behavior changes or newfeatures
docs/versionhistory.rst).