Skip to content

tests/semantic: stop leaking TemporaryDirectory when another module borrows the helpers - #3486

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
joelh12:tests/semantic-tempdir-leaks
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
joelh12:tests/semantic-tempdir-leaks

Conversation

@joelh12

@joelh12 joelh12 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #3381 (comment) (not on the list; one-line claim)

What and why

python -m unittest discover -s tests/semantic on main (f847748c) prints three ResourceWarning: 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:

Owner Helper Borrowers
test_salu64_bitwise_data translate_ps() / run_ps_wave() test_interp_merge_check, test_mask_src_sgpr_const, test_saveexec_variants
test_buffer_load_format load_format() test_norm_decode

In discovery order this leaks in two ways:

  • A borrower that runs before the owner creates the folder; the owner's own setUpModule() then replaces it without cleaning it up (the warning in the middle of the run).
  • A borrower that runs after the owner's 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():

  • return when _TMP_DIR already exists;
  • after creating the folder, atexit.register(tearDownModule). It has to be registered after the TemporaryDirectory: atexit runs last in, first out, and tempfile's own exit finalizer (which prints the warning) is registered when the first TemporaryDirectory is 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.
  • Every tests/semantic/test_*.py run on its own: before, the four borrowers each leak one folder; after, none do.
  • The four test_salu64_bitwise_data modules together, and test_buffer_load_format + test_norm_decode together: 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 in test_buffer_load_format, which this fix also covers.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of 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

…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>
@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

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:

py -3.14 -I -c "import sys,atexit;sys.path.insert(0,'tests/semantic');import test_buffer_load_format as m;m.setUpModule();m.tearDownModule();n=atexit._ncallbacks();[(m.setUpModule(),m.tearDownModule()) for _ in range(128)];print(atexit._ncallbacks()-n)"

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).

@favabean13

favabean13 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Independent CPU review of 327746c (Linux, Python 3.13, no GPU, no game files):

  • Reproduced the leak on main (f847748): python -W always::ResourceWarning -m unittest test_buffer_load_format test_norm_decode test_salu64_bitwise_data test_interp_merge_check test_mask_src_sgpr_const test_saveexec_variants prints two "Implicitly cleaning up " warnings. On this head: none, all 71 tests OK, and no leftover /tmp/tmp* directories.
  • Also clean with -W error::ResourceWarning (importers run before and after the owning modules), under pytest (55 passed, 1366 subtests), and standalone with python -I tests/semantic/test_salu64_bitwise_data.py.
  • Full discover -s tests/semantic with ResourceWarnings shown: zero "Implicitly cleaning up" lines; only failure is test_fma_khr.test_spirv_tools_accept_it, which fails on main too.
  • Small nit, not blocking: atexit.register(tearDownModule) runs again each time a helper recreates the folder after teardown, so it can be registered more than once. That's harmless because tearDownModule does nothing once _TMP_DIR is None.

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>
@joelh12

joelh12 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @shdann and @favabean13.

@shdann, reproduced: 128 extra callbacks after 128 setup/teardown cycles in both modules at 327746cc. Fixed in 6b7833b9: a module-level _EXIT_CLEANUP flag registers tearDownModule() only on the first folder the module creates. That 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 the folder after teardown is still covered.

Rechecked at 6b7833b9 (macOS 26.5.2, Python 3.12.16):

  • Your repro: 0 extra callbacks after 128 cycles, for both modules.
  • Each borrower alone, and the borrowers with their owner in discovery order: 0 ResourceWarnings.
  • python -W always::ResourceWarning -m unittest discover -s tests/semantic: 0 warnings, 7322 tests OK (1 expected failure).
  • pytest on the four affected modules: 55 passed, 1366 subtests.

@favabean13, this also covers the nit you raised. Thanks for the independent run.

AI-assisted (Claude Code).

@Maraco91

Copy link
Copy Markdown

Tested 327746c on Windows 11, Python 3.12.6: pass

python -m unittest discover -s tests/semantic, the whole suite, on main (f847748) and on this head:

tests ResourceWarning: Implicitly cleaning up <TemporaryDirectory ...>
main 7,322 OK (4 skipped, 1 expected failure) 1
this PR 7,322 OK (4 skipped, 1 expected failure) 0
main, python -W default -m unittest ... 7,322 OK 3
this PR, python -W default -m unittest ... 7,322 OK 0

On Windows with Python 3.12 the plain command shows only the mid-run warning on main; the two at exit appear once warnings are forced on with -W default, which gives the three the description reports. Either way they are all gone with this change, and the test count and results are unchanged.

No blocker found.

AI-assisted (Claude Code); no game files or GPU.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants