Commit e5ef7e2
authored
Fix CUDA context leak in
## Summary
- **Fix**: `Device.get_primary_context()` calls
`self._dev.set_current()` on first invocation, which leaves the primary
context active on the thread. The caller `_activate_context_for` then
calls `push()`, saving that already-active context onto the stack. On
exit, `pop()` restores it — so the context remains current after the
`with` block (a leak). The fix pops the context left by `set_current()`
immediately after obtaining the handle, so `get_primary_context()`
upholds its documented contract: *"Note: it is not pushed to the CPU
thread."*
- **Tests**: Adds two regression tests in `test_context_stack.py` — one
verifying no context remains after `with cuda.gpus[0]: pass` on a clean
stack, and another verifying the previous context is properly restored.
Made with [Cursor](https://cursor.com)
---------
Co-authored-by: Michael Wang <isVoid@users.noreply.github.com>with cuda.gpus[N]: context manager (#855)1 parent 6803734 commit e5ef7e2
File tree
2 files changed
+52
-0
lines changed- numba_cuda/numba/cuda
- cudadrv
- tests/cudadrv
2 files changed
+52
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
534 | 534 | | |
535 | 535 | | |
536 | 536 | | |
| 537 | + | |
537 | 538 | | |
538 | 539 | | |
539 | 540 | | |
540 | 541 | | |
541 | 542 | | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
542 | 549 | | |
543 | 550 | | |
544 | 551 | | |
| |||
Lines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
80 | 125 | | |
81 | 126 | | |
82 | 127 | | |
| |||
0 commit comments