Skip to content

Commit 6df4ed1

Browse files
Andy-Jostclaude
andcommitted
test(cuda.core): freeze the collected heap once so the per-test gc.collect() stops walking it
#2484 added `gc.collect()` plus `cuCtxSynchronize()` to every `init_cuda` teardown so that each test's garbage is drained before the context is popped (#2381). That collect walks the whole heap on every teardown, including the roughly 4,600 collected test items and the imported modules, which never become garbage during a session. In a single measurement run on one L40S GPU (not a CI runner) the collect costs 59 ms median per teardown; the sync costs 0.03 ms. CI step timestamps of run 30938115056 and run 30953896252 show the "Run cuda.core tests" step going from 144 s to 237 s at that commit on the same L4 row. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 5078fe6 commit 6df4ed1

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎cuda_core/tests/AGENTS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ The `init_cuda` fixture in `conftest.py` runs `gc.collect()` followed
6464
by `cuCtxSynchronize()` before popping the context. Tests should not rely on
6565
that as a substitute for cleaning up explicitly: prefer context managers for
6666
resources whose lifetime fits a single scope, and keep pool lifetimes inside
67-
the test that creates them.
67+
the test that creates them. The collected items and imported modules are
68+
frozen after collection, so the per-test collect covers only objects created
69+
during the run.
6870

6971
## Shared test support
7072

‎cuda_core/tests/conftest.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@
4646
from cuda.core._utils.cuda_utils import handle_return
4747

4848

49+
def pytest_collection_finish(session):
50+
# The per-test gc.collect() in init_cuda (below) must not re-traverse the
51+
# collected items and the imported modules on every teardown: freeze them
52+
# once. Objects created after this point are collected as before (#2381).
53+
gc.collect()
54+
gc.freeze()
55+
56+
4957
def pytest_configure(config):
5058
# When using `parallel-threads` set up mini-plugin to ensure each thread has a CUDA context
5159
parallel_threads = getattr(config.option, "parallel_threads", 0)

0 commit comments

Comments
 (0)