Skip to content

Commit 33f68da

Browse files
safe_close()
1 parent 28db9df commit 33f68da

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

cuda_core/cuda/core/experimental/_event.pyx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,23 @@ cdef class Event:
109109
self._ctx_handle = ctx_handle
110110
return self
111111

112-
cpdef close(self, is_shutting_down=is_shutting_down):
112+
cpdef safe_close(self, is_shutting_down=is_shutting_down):
113113
"""Destroy the event."""
114114
if self._handle is not None:
115-
if not is_shutting_down()
115+
if not is_shutting_down():
116116
err, = driver.cuEventDestroy(self._handle)
117117
self._handle = None
118118
raise_if_driver_error(err)
119119

120+
cpdef close(self):
121+
"""Destroy the event."""
122+
if self._handle is not None:
123+
err, = driver.cuEventDestroy(self._handle)
124+
self._handle = None
125+
raise_if_driver_error(err)
126+
120127
def __del__(self):
121-
self.close()
128+
self.safe_close()
122129

123130
def __isub__(self, other):
124131
return NotImplemented

cuda_core/cuda/core/experimental/_memory.pyx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,17 @@ cdef class Buffer:
5757
return self
5858

5959
def __del__(self):
60-
self.close()
60+
self.safe_close()
6161

62-
cpdef close(self, stream: Stream = None, is_shutting_down=is_shutting_down):
62+
cpdef safe_close(self, stream: Stream = None, is_shutting_down=is_shutting_down):
63+
if self._ptr and self._mr is not None:
64+
if not is_shutting_down():
65+
self._mr.deallocate(self._ptr, self._size, stream)
66+
self._ptr = 0
67+
self._mr = None
68+
self._ptr_obj = None
69+
70+
cpdef close(self, stream: Stream = None):
6371
"""Deallocate this buffer asynchronously on the given stream.
6472
6573
This buffer is released back to their memory resource
@@ -72,8 +80,7 @@ cdef class Buffer:
7280
the behavior depends on the underlying memory resource.
7381
"""
7482
if self._ptr and self._mr is not None:
75-
if not is_shutting_down():
76-
self._mr.deallocate(self._ptr, self._size, stream)
83+
self._mr.deallocate(self._ptr, self._size, stream)
7784
self._ptr = 0
7885
self._mr = None
7986
self._ptr_obj = None

cuda_core/cuda/core/experimental/_stream.pyx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,16 @@ cdef class Stream:
186186
return self
187187

188188
def __del__(self):
189-
self.close()
189+
self.safe_close()
190+
191+
cpdef safe_close(self, is_shutting_down=is_shutting_down):
192+
if self._owner is None:
193+
if self._handle and not self._builtin:
194+
if not is_shutting_down():
195+
handle_return(driver.cuStreamDestroy(self._handle))
196+
else:
197+
self._owner = None
198+
self._handle = None
190199

191200
cpdef close(self, is_shutting_down=is_shutting_down):
192201
"""Destroy the stream.
@@ -197,8 +206,7 @@ cdef class Stream:
197206
"""
198207
if self._owner is None:
199208
if self._handle and not self._builtin:
200-
if not is_shutting_down():
201-
handle_return(driver.cuStreamDestroy(self._handle))
209+
handle_return(driver.cuStreamDestroy(self._handle))
202210
else:
203211
self._owner = None
204212
self._handle = None

0 commit comments

Comments
 (0)