Commit 5e9a5ea
cuda.core: fix C++ teardown leak when buffer has no attached stream (#2001)
Issue: the C++ ``shared_ptr`` deleter for a buffer's device-pointer
handle invokes ``MemoryResource.deallocate`` via ``_mr_dealloc_callback``.
The handle's deallocation stream is set separately via
``set_deallocation_stream``; if it was never set (e.g. buffers minted via
``Buffer.from_handle(ptr, size, mr=mr)`` from DLPack import, IPC import,
or third-party adapters), the callback would pass ``stream=None`` to
``mr.deallocate``. After the strict-stream changes for #2001, the
stream-ordered MR overrides reject ``stream=None`` via ``Stream_accept``
and raise ``TypeError``. The ``noexcept`` callback catches the exception,
prints a warning to stderr, and returns -- silently **leaking** the
underlying CUDA allocation (and any associated IPC handles).
Fix: when ``h_stream`` is empty in ``_mr_dealloc_callback``, fall back
to ``default_stream()`` instead of ``None``. The C++ teardown path is
the unique legitimate "no-stream-context" caller (no Python frame from
which to obtain a stream), so this is the one place where an implicit
default-stream fallback is necessary; everywhere else the policy
remains "stream is required and must be passed explicitly".
Add ``test_mr_dealloc_callback_falls_back_to_default_stream`` covering
the regression: a strict stream-ordered mock MR is used to back a
``Buffer.from_handle`` (no attached stream), and the test asserts that
``deallocate`` is invoked with the default stream rather than failing
with ``TypeError`` and leaking.
Co-authored-by: Cursor <cursoragent@cursor.com>1 parent 91807ee commit 5e9a5ea
2 files changed
Lines changed: 66 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
53 | 67 | | |
54 | | - | |
55 | | - | |
56 | | - | |
| 68 | + | |
57 | 69 | | |
58 | 70 | | |
59 | 71 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
521 | 521 | | |
522 | 522 | | |
523 | 523 | | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
524 | 574 | | |
525 | 575 | | |
526 | 576 | | |
| |||
0 commit comments