Repository navigation
tests/semantic: stop leaking TemporaryDirectory when another module borrows the helpers - #3486
Conversation
…orrows the helpers test_salu64_bitwise_data and test_buffer_load_format create their folder lazily: translate_ps() / load_format() call setUpModule() when _TMP_DIR is None. Four modules import those helpers (test_interp_merge_check, test_mask_src_sgpr_const and test_saveexec_variants from the first, test_norm_decode from the second), so in discovery order: - a borrower that runs first creates the folder, and the owner's own setUpModule() then replaces it without cleaning it up; - a borrower that runs after the owner's tearDownModule() creates it again, and nothing removes it. `python -m unittest discover -s tests/semantic` printed three "ResourceWarning: Implicitly cleaning up <TemporaryDirectory ...>" lines for this. setUpModule() now keeps an existing folder, and registers tearDownModule() with atexit right after creating one. It is registered after the TemporaryDirectory, so it runs before tempfile's own exit finalizer (atexit is last in, first out); registering it at import time ran too late and the warning stayed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The new exit handler accumulates when a long-lived unittest process runs either fixture repeatedly. On Windows CPython 3.14.7 at 327746c, 128 setup/teardown cycles added 128 atexit callbacks for each changed module; on the parent the same cycles added zero. Both temp directories were empty after the probe. The retained object here is the cleanup registration. The two affected suites passed 34 and 11 tests. Repro from the checkout, using CPython's private callback counter for observation only: Replacing the import with test_salu64_bitwise_data gives the same result. Please register the fallback once per module after the first TemporaryDirectory is constructed, or unregister it during explicit teardown and register once for the next live fixture. That keeps cleanup for the late borrower while bounding retained callbacks. AI-assisted (Codex). |
|
Independent CPU review of 327746c (Linux, Python 3.13, no GPU, no game files):
LGTM. AI-assisted (Claude Code). |
Review (@shdann): setUpModule() registered tearDownModule() with atexit every time it created the folder, so a process that cycles setup and teardown kept one more callback per cycle (128 after 128 cycles). A module-level flag now registers it on the first creation only. That first registration still follows the module's first TemporaryDirectory, so it runs before tempfile's exit finalizer, and tearDownModule() cleans whichever folder is live at exit, so a borrower that recreates it after teardown is still covered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @shdann and @favabean13. @shdann, reproduced: 128 extra callbacks after 128 setup/teardown cycles in both modules at Rechecked at
@favabean13, this also covers the nit you raised. Thanks for the independent run. AI-assisted (Claude Code). |
|
Tested 327746c on Windows 11, Python 3.12.6: pass
On Windows with Python 3.12 the plain command shows only the mid-run warning on No blocker found. AI-assisted (Claude Code); no game files or GPU. |
Claimed in the coordination issue as: #3381 (comment) (not on the list; one-line claim)
What and why
python -m unittest discover -s tests/semanticonmain(f847748c) prints threeResourceWarning: Implicitly cleaning up <TemporaryDirectory ...>lines. They come from two modules whose helpers create the module's folder lazily (if _TMP_DIR is None: setUpModule()), and four modules that import those helpers:test_salu64_bitwise_datatranslate_ps()/run_ps_wave()test_interp_merge_check,test_mask_src_sgpr_const,test_saveexec_variantstest_buffer_load_formatload_format()test_norm_decodeIn discovery order this leaks in two ways:
setUpModule()then replaces it without cleaning it up (the warning in the middle of the run).tearDownModule()creates the folder again, and unittest never tears the owner down a second time (the two warnings at exit).The borrowers were all added on 2026-11-22, which is when the warnings started.
The fix, in each owner's
setUpModule():_TMP_DIRalready exists;atexit.register(tearDownModule). It has to be registered after theTemporaryDirectory: atexit runs last in, first out, andtempfile's own exit finalizer (which prints the warning) is registered when the firstTemporaryDirectoryis created. Registering at import time ran too late and the warning stayed.tearDownModule()was already idempotent, so the normal unittest path is unchanged. Test code only: no translator or runtime change.Tested
macOS 26.5.2 (Apple M4 Pro), Python 3.12.16, llvm-mc 23.1.6, SPIRV-Tools v2026.4. No GPU needed.
python -W always::ResourceWarning -m unittest discover -s tests/semantic: 3 warnings before, 0 after; 7322 tests OK (1 expected failure) both times.tests/semantic/test_*.pyrun on its own: before, the four borrowers each leak one folder; after, none do.test_salu64_bitwise_datamodules together, andtest_buffer_load_format+test_norm_decodetogether: 0 warnings.python -m compileall -q tests/semantic: OK.Open PRs touching these files (#293, #1371, #2437, #3018) don't change this code. #1371 adds another lazy
setUpModule()call intest_buffer_load_format, which this fix also covers.Checklist
docs/design.md, with how it was confirmed. (n/a: no translator change)AI-assisted (Claude Code): the diagnosis, the fix and the test runs above were done with Claude Code on this machine.
🤖 Generated with Claude Code