Skip to content

Commit bc94ed4

Browse files
committed
test(cuda.core): escalate the baseline-release warning and tidy gate wording
- pytest.ini: escalate BaselineMemoryResourceReleasedWarning to an error alongside UnreleasedMemoryResourceWarning, matching tests/AGENTS.md. - conftest.py: print labelled counts (owned_pools=..., va_reservations=...) in both warnings; replace the stale GraphMemoryResource example in the leftover-check comment. - helpers/memory.py: describe both warnings in terms of owned memory pools and VA reservations. - test_resource_live_count.py: release the VA test buffer with a context manager so a failed assertion does not leak the reservation. - test_init_cuda_collect_gate.py: assert on the warning class only.
1 parent 4b4e769 commit bc94ed4

5 files changed

Lines changed: 29 additions & 21 deletions

File tree

‎cuda_core/pytest.ini‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ addopts = --showlocals --durations=20
77
pythonpath = tests
88
norecursedirs = cython
99
filterwarnings =
10-
# A test left a memory resource alive; see "Release resources at test boundaries" in tests/AGENTS.md.
10+
# A test left an owned memory pool or VA reservation alive, or released one
11+
# that existed before it started; see "Release resources at test boundaries"
12+
# in tests/AGENTS.md.
1113
# conftest.py makes helpers importable, which happens after pytest first
12-
# applies these filters (and warns); the filter is applied again per test.
13-
# The message regex below is pinned to pytest's wording (as of pytest 9.x);
14+
# applies these filters (and warns); the filters are applied again per test.
15+
# The message regexes below are pinned to pytest's wording (as of pytest 9.x);
1416
# if a pytest upgrade rewords "Failed to import filter module" this stops
1517
# suppressing the config warning (noisy, not fatal) and must be updated.
1618
ignore:Failed to import filter module.*UnreleasedMemoryResourceWarning:pytest.PytestConfigWarning
19+
ignore:Failed to import filter module.*BaselineMemoryResourceReleasedWarning:pytest.PytestConfigWarning
1720
error::helpers.memory.UnreleasedMemoryResourceWarning
21+
error::helpers.memory.BaselineMemoryResourceReleasedWarning
1822
markers =
1923
samples: tests for cuda.core standalone samples
2024
# Keep this authorship marker registry in sync across all pytest config roots.

‎cuda_core/tests/conftest.py‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ def _live_resource_counts():
150150
return (_live_owned_mempool_count(), _live_va_reservation_count())
151151

152152

153+
def _format_counts(counts):
154+
owned_pools, va_reservations = counts
155+
return f"owned_pools={owned_pools}, va_reservations={va_reservations}"
156+
157+
153158
# Counts after the last init_cuda test; None until one has run.
154159
_mr_expected_live = None
155160
# Counts at the start of the current item, before its fixtures set up.
@@ -159,9 +164,9 @@ def _live_resource_counts():
159164

160165
@pytest.hookimpl(wrapper=True)
161166
def pytest_runtest_protocol(item, nextitem):
162-
# Leftover check: compare with the count after the previous init_cuda test,
163-
# not the session start, because the count legitimately rises over a run
164-
# (interned GraphMemoryResource, pytest-retained class instances).
167+
# Leftover check: compare with the counts after the previous init_cuda test,
168+
# not the session start, because they legitimately rise over a run (pools
169+
# owned by module- or class-scoped fixtures).
165170
global _mr_expected_live, _mr_live_at_item_start, _mr_last_init_cuda_nodeid
166171
_mr_live_at_item_start = _live_resource_counts()
167172
try:
@@ -245,10 +250,9 @@ def init_cuda(request):
245250
seed = getattr(request.config.option, "randomly_seed", None)
246251
seed_msg = f" (pytest-randomly seed: {seed})" if seed is not None else ""
247252
warnings.warn(
248-
f"{excess} (owned_pools, va_reservations) memory resource(s) were left alive by "
249-
f"tests that ran since {_mr_last_init_cuda_nodeid} ({held} still alive after a "
250-
f"collect). A test or fixture that constructs a memory resource must request "
251-
f"init_cuda.{seed_msg}",
253+
f"Owned resources were left alive by tests that ran since {_mr_last_init_cuda_nodeid} "
254+
f"({_format_counts(excess)}; still alive after a collect: {_format_counts(held)}). "
255+
f"A test or fixture that constructs a memory resource must request init_cuda.{seed_msg}",
252256
UnreleasedMemoryResourceWarning,
253257
stacklevel=2,
254258
)
@@ -284,10 +288,10 @@ def init_cuda(request):
284288
if any(now < snap for now, snap in zip(live_after, live_at_setup)):
285289
released = tuple(s - n for s, n in zip(live_at_setup, live_after))
286290
warnings.warn(
287-
f"{request.node.nodeid} released {released} (owned_pools, va_reservations) memory "
288-
f"resource(s) that existed before it started (counts {live_after}, snapshot "
289-
f"{live_at_setup}). A leak in this test could have been masked by that release. "
290-
"Find what held the released resource.",
291+
f"{request.node.nodeid} released owned resources that existed before it started "
292+
f"({_format_counts(released)}; counts {_format_counts(live_after)}, snapshot "
293+
f"{_format_counts(live_at_setup)}). A leak in this test could have been masked by "
294+
"that release. Find what held the released resource.",
291295
BaselineMemoryResourceReleasedWarning,
292296
stacklevel=2,
293297
)

‎cuda_core/tests/helpers/memory.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515

1616
class UnreleasedMemoryResourceWarning(Warning):
17-
"""A test or fixture left a ``MemoryResource`` alive without ``init_cuda``."""
17+
"""A test or fixture left an owned memory pool or VA reservation alive without ``init_cuda``."""
1818

1919

2020
class BaselineMemoryResourceReleasedWarning(Warning):
21-
"""An ``init_cuda`` test released a resource that existed before it started."""
21+
"""An ``init_cuda`` test released an owned memory pool or VA reservation that existed before it started."""
2222

2323

2424
def skip_if_pinned_memory_unsupported(device):

‎cuda_core/tests/memory/test_init_cuda_collect_gate.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def test_setup_warns_when_resources_left_alive_since_last_init_cuda(request, mon
179179
_patch_conftest_global(monkeypatch, func, "_live_resource_counts", _ScriptedCounts((1, 0)))
180180

181181
gen = func(_FakeRequest())
182-
with pytest.warns(UnreleasedMemoryResourceWarning, match="left alive"):
182+
with pytest.warns(UnreleasedMemoryResourceWarning):
183183
next(gen) # setup emits the leftover warning
184184
_finish_teardown(gen) # live_after == live_at_setup -> no baseline warning
185185

@@ -196,7 +196,7 @@ def test_teardown_warns_on_baseline_release(request, monkeypatch):
196196
gen = func(_FakeRequest())
197197
next(gen) # setup: live_at_setup == (1, 0)
198198
with (
199-
pytest.warns(BaselineMemoryResourceReleasedWarning, match="existed before it started"),
199+
pytest.warns(BaselineMemoryResourceReleasedWarning),
200200
pytest.raises(StopIteration),
201201
):
202202
next(gen) # teardown: live_after == (0, 0) -> baseline-release warning

‎cuda_core/tests/memory/test_resource_live_count.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ def test_va_reservation_is_counted_until_freed(init_cuda):
104104
buf = vmm.allocate(4096)
105105
except NotImplementedError:
106106
pytest.skip("VMM handle type not implemented on this platform")
107-
# An allocation reserves at least one address range.
108-
assert _live_va_reservation_count() >= before + 1
109-
buf.close()
107+
with buf:
108+
# An allocation reserves at least one address range.
109+
assert _live_va_reservation_count() >= before + 1
110110
assert _live_va_reservation_count() == before
111111

112112

0 commit comments

Comments
 (0)