test: fix flaky tests_temp teardown cleanup#28
Merged
Conversation
The shared _rmtree() helper failed intermittently in CI (StreamingFormReaderTest), with "IsADirectoryError: Is a directory: tests_temp". Two bugs: 1. The on-error handler always called os.remove(path). shutil.rmtree also invokes the handler for directories (when os.rmdir fails), where os.remove raises IsADirectoryError. Fixed by re-running the failed operation func(path) (os.unlink for files, os.rmdir for directories) — the documented pattern. 2. No tolerance for the underlying race: upload threads in some tests are still writing into the temp folder during teardown, so a file appears between rmtree's scandir and rmdir and leaves the directory non-empty. Added a retry loop (up to ~1s) that lets those threads finish before giving up. Adds regression tests covering a plain tree, a read-only file (exercises the handler), and a concurrent writer (reproduces the race). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Summary
Fixes the recurring CI flake in
StreamingFormReaderTest(it broke PRs #21, #26 and #27, each needing a re-run). The failure was always at teardown:Root cause — two bugs in
test_utils._rmtreeHandler mishandled directories. The
on_rm_errorhandler always calledos.remove(path). Butshutil.rmtreealso invokes the handler for directories (whenos.rmdirfails), andos.removeon a directory raisesIsADirectoryError. Fixed by re-running the operation that actually failed —func(path)(os.unlinkfor files,os.rmdirfor directories) — which is the pattern from the Python docs.No tolerance for the race. Some tests upload files via background threads that are still writing into
tests_tempwhen teardown starts; a file appearing between rmtree'sscandirandrmdirleaves the directory non-empty. Added a retry loop (up to ~1s) so those threads can finish before giving up.Testing
tests/test_utils_rmtree_test.py): plain tree, read-only file (exercises the handler), and a concurrent writer (reproduces the race). 8/8 stable.onerrorDeprecationWarning on Python 3.14 (onerrorkept for 3.10–3.11 compatibility;onexcis 3.12+).🤖 Generated with Claude Code