Skip to content
Closed
Prev Previous commit
Next Next commit
Make Linker teardown order explicit
  • Loading branch information
isVoid committed May 27, 2026
commit 0ca1affc46c99f7e83e1309ca6711ec5ea41f8f1
12 changes: 6 additions & 6 deletions cuda_core/cuda/core/_linker.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ cdef class Linker:
cdef:
NvJitLinkHandle _nvjitlink_handle
CuLinkHandle _culink_handle
# _drv_jit_keys/_drv_jit_values are the C arrays handed to cuLinkCreate.
# The driver retains a reference to the optionValues array for the life
# of the CUlinkState (it writes back log-size outputs into its slots),
# so these must live past cuLinkCreate and outlive cuLinkDestroy.
# Declared after _culink_handle so their C++ destructors run AFTER
# cuLinkDestroy executes during tp_dealloc.
# WARNING: the driver backend passes raw pointers from _drv_jit_values
# and _drv_log_bufs to cuLinkCreate. cuLinkDestroy may still dereference
# them, so _close_noexcept must reset _culink_handle before releasing
# these retainers. Do not bypass or weaken that teardown order.
vector[cydriver.CUjit_option] _drv_jit_keys
vector[void*] _drv_jit_values
bint _use_nvjitlink
Expand All @@ -27,3 +25,5 @@ cdef class Linker:
str _error_log # decoded log; None until link() or pre-link get_*_log()
object _options # LinkerOptions
object __weakref__

cdef void _close_noexcept(self) noexcept
48 changes: 27 additions & 21 deletions cuda_core/cuda/core/_linker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,37 @@ cdef class Linker:

def close(self):
"""Destroy this linker."""
# link() caches decoded logs into _info_log/_error_log on the success
# path only. A failed link leaves them as None with _drv_log_bufs as the
# only source, so cache them here before the raw buffers are released.
if not self._use_nvjitlink and self._drv_log_bufs is not None:
if self._info_log is None:
self._info_log = self.get_info_log()
if self._error_log is None:
self._error_log = self.get_error_log()
self._close_noexcept()

cdef void _close_noexcept(self) noexcept:
cdef vector[cydriver.CUjit_option] empty_keys
cdef vector[void*] empty_values

if self._use_nvjitlink:
self._nvjitlink_handle.reset()
else:
# link() caches decoded logs into _info_log/_error_log on the
# success path only. A failed link leaves them as None with
# _drv_log_bufs as the only source, so cache them here before
# the raw buffers are released.
if self._drv_log_bufs is not None:
if self._info_log is None:
self._info_log = self.get_info_log()
if self._error_log is None:
self._error_log = self.get_error_log()
# .reset() drops the last shared_ptr to the CUlinkState and runs
# cuLinkDestroy synchronously via the custom deleter. The driver
# is no longer looking at our option arrays or log buffers, so
# release the host-side retainers now rather than waiting for
# tp_dealloc. Swap with empty vectors to actually free the
# backing allocation; vector.clear() only sets size to 0 and
# retains capacity.
self._culink_handle.reset()
self._drv_jit_keys.swap(empty_keys)
self._drv_jit_values.swap(empty_values)
self._drv_log_bufs = None
return

# .reset() drops the last shared_ptr to the CUlinkState and runs
# cuLinkDestroy synchronously via the custom deleter. The driver may
# still look at our option arrays or log buffers during cuLinkDestroy,
# so release the host-side retainers only after the handle is reset.
self._culink_handle.reset()
# Swap with empty vectors to actually free the backing allocation;
# vector.clear() only sets size to 0 and retains capacity.
self._drv_jit_keys.swap(empty_keys)
self._drv_jit_values.swap(empty_values)
self._drv_log_bufs = None

def __dealloc__(self):
self._close_noexcept()

@property
def handle(self) -> LinkerHandleT:
Expand Down
10 changes: 5 additions & 5 deletions cuda_core/tests/test_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

import gc
import inspect

import pytest
Expand Down Expand Up @@ -261,10 +262,9 @@ def test_driver_linker_lifetime_no_heap_corruption(monkeypatch, compile_ptx_func
Two prior bugs corrupted the heap during driver-linker teardown: the log
buffer bytearrays were cleared before cuLinkDestroy ran, and the
optionValues array was a stack-local vector destroyed when Linker_init
returned. Both manifested in the NEXT CUDA operation after the Linker
was destroyed, not at destruction itself. This test forces the driver
backend, links, closes + drops the Linker, and then performs a full
compile + link cycle that would previously segfault.
returned. Both manifested in the NEXT CUDA operation after the Linker was
destroyed, not at destruction itself. This test forces the driver backend,
destroys one Linker via __dealloc__, and explicitly closes another.
"""
if not _can_load_generated_ptx():
pytest.skip("PTX version too new for current driver")
Expand All @@ -273,8 +273,8 @@ def test_driver_linker_lifetime_no_heap_corruption(monkeypatch, compile_ptx_func
linker = Linker(*compile_ptx_functions, options=LinkerOptions(arch=ARCH))
assert linker.backend == "driver"
linker.link("cubin")
linker.close()
del linker
gc.collect()

obj_a = Program(kernel_a, "c++", ProgramOptions(relocatable_device_code=True)).compile("ptx")
obj_b = Program(device_function_b, "c++", ProgramOptions(relocatable_device_code=True)).compile("ptx")
Expand Down