gh-81905: Fix race condition when tarfile is used from multiple processes #98632
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.
This removes a race condition that can happen if the
tarfile
module is used at the same time from two different processes with overlapping parent paths of the archive that don't exist yet.This bug has been discussed on the Python bugtracker:
https://bugs.python.org/issue37724 -- which is exactly this bug in the multiprocessing context
https://bugs.python.org/issue23649 -- in which this behavior was discussed in the multithreading context -- I agree that tarfile should not be considered thread safe, but I do think this race-condition should be fixed for multi-process safety
While it is possible to work around by calling
os.makedirs(..., exists_ok=True)
in advance, I believe removing this footgun altogether is valuable. Especially since the code arguably gets cleaner.I hope this is useful. Let me know your thoughts and if I need to take any additional steps to get this merged. It doesn't seem to warrant an update to the
NEWS
file, but let me know if you feel otherwise.