|
33 | 33 | UnreleasedMemoryResourceWarning, |
34 | 34 | ) |
35 | 35 |
|
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 |
37 | 38 |
|
38 | 39 |
|
39 | 40 | class _ScriptedCounts: |
@@ -250,3 +251,58 @@ def test_teardown_warns_on_baseline_release(request, monkeypatch): |
250 | 251 | pytest.raises(StopIteration), |
251 | 252 | ): |
252 | 253 | 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