Skip to content

Commit 587c111

Browse files
committed
fix(cuda.core): synchronize the bound context during test teardown
Use device.sync() because the test wrapper may pop the current context before init_cuda teardown. This avoids spurious INVALID_CONTEXT errors while preserving synchronization error reporting and context cleanup. Add regression coverage for a missing current context and cleanup after synchronization failures. Related to NVIDIA#3426.
1 parent 730fcbe commit 587c111

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎cuda_core/tests/conftest.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,10 @@ def init_cuda(request):
298298
# With pytest-run-parallel this runs after worker join. This also runs
299299
# when setup raises, so an escalated leftover warning cannot leave the
300300
# context current.
301+
# The test wrapper may already have popped the calling thread's
302+
# context; synchronize the fixture device's bound context explicitly.
301303
try:
302-
handle_return(driver.cuCtxSynchronize())
304+
device.sync()
303305
finally:
304306
_ = _device_unset_current()
305307
# After the cleanup, so an escalated warning cannot skip it.

‎cuda_core/tests/memory/test_init_cuda_collect_gate.py‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
UnreleasedMemoryResourceWarning,
3434
)
3535

36-
from cuda.core._utils.cuda_utils import driver, handle_return
36+
from cuda.core import Device
37+
from cuda.core._utils.cuda_utils import CUDAError, driver, handle_return
3738

3839

3940
class _ScriptedCounts:
@@ -250,3 +251,58 @@ def test_teardown_warns_on_baseline_release(request, monkeypatch):
250251
pytest.raises(StopIteration),
251252
):
252253
next(gen) # teardown: live_after == (0, 0) -> baseline-release warning
254+
255+
256+
@_THREAD_UNSAFE_MARKER
257+
@pytest.mark.agent_authored(model="gpt-6")
258+
@pytest.mark.parametrize("clear_current", [False, True])
259+
def test_teardown_synchronizes_fixture_context(request, monkeypatch, clear_current):
260+
"""Teardown works even after the test wrapper has popped the current context."""
261+
func = _init_cuda_func(request)
262+
_patch_conftest_global(monkeypatch, func, "_mr_expected_live", None)
263+
_patch_conftest_global(monkeypatch, func, "_live_resource_counts", _ScriptedCounts((0, 0)))
264+
265+
saved = _pop_context_stack()
266+
gen = func(_FakeRequest())
267+
try:
268+
next(gen)
269+
if clear_current:
270+
handle_return(driver.cuCtxPopCurrent())
271+
assert current_context_handle() == 0
272+
_finish_teardown(gen)
273+
assert current_context_handle() == 0
274+
finally:
275+
gen.close()
276+
_push_context_stack(saved)
277+
278+
279+
@_THREAD_UNSAFE_MARKER
280+
@pytest.mark.agent_authored(model="gpt-6")
281+
def test_teardown_sync_failure_still_pops_context(request, monkeypatch):
282+
"""A synchronization error propagates without leaving the context current."""
283+
func = _init_cuda_func(request)
284+
_patch_conftest_global(monkeypatch, func, "_mr_expected_live", None)
285+
_patch_conftest_global(monkeypatch, func, "_live_resource_counts", _ScriptedCounts((0, 0)))
286+
failure = CUDAError("injected synchronization failure")
287+
288+
class FailingSyncDevice:
289+
device_id = 0
290+
291+
def set_current(self):
292+
Device(0).set_current()
293+
294+
def sync(self):
295+
raise failure
296+
297+
_patch_conftest_global(monkeypatch, func, "Device", lambda _: FailingSyncDevice())
298+
saved = _pop_context_stack()
299+
gen = func(_FakeRequest())
300+
try:
301+
next(gen)
302+
with pytest.raises(CUDAError) as excinfo:
303+
next(gen)
304+
assert excinfo.value is failure
305+
assert current_context_handle() == 0
306+
finally:
307+
gen.close()
308+
_push_context_stack(saved)

0 commit comments

Comments
 (0)