Skip to content

Commit 1f7e2d1

Browse files
committed
maybe leak
1 parent 9dd1405 commit 1f7e2d1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

ddtrace/internal/_threads/lock.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ Lock_init(Lock* self, PyObject* args, PyObject* kwargs)
4343
return 0;
4444
}
4545

46+
// ----------------------------------------------------------------------------
47+
static inline void
48+
_Lock_maybe_leak(Lock* self)
49+
{
50+
// This function is used to ensure that the mutex is not leaked if it is
51+
// still locked when the lock object is deallocated.
52+
if (self->_locked) {
53+
self->_mutex.release(); // DEV: This releases the unique_ptr, not the mutex!
54+
}
55+
}
56+
4657
// ----------------------------------------------------------------------------
4758
static void
4859
Lock_dealloc(Lock* self)
@@ -56,6 +67,8 @@ Lock_dealloc(Lock* self)
5667
lock_set.erase(self);
5768
}
5869

70+
_Lock_maybe_leak(self);
71+
5972
self->_mutex = nullptr;
6073

6174
Py_TYPE(self)->tp_free((PyObject*)self);
@@ -153,6 +166,7 @@ Lock_exit(Lock* self, PyObject* args, PyObject* kwargs)
153166
static inline void
154167
Lock_reset(Lock* self)
155168
{
169+
_Lock_maybe_leak(self);
156170
self->_mutex = std::make_unique<std::timed_mutex>();
157171
self->_locked = 0;
158172
}
@@ -220,6 +234,17 @@ RLock_init(RLock* self, PyObject* args, PyObject* kwargs)
220234
return 0;
221235
}
222236

237+
// ----------------------------------------------------------------------------
238+
static inline void
239+
_RLock_maybe_leak(RLock* self)
240+
{
241+
// This function is used to ensure that the mutex is not leaked if it is
242+
// still locked when the re-entrant lock object is deallocated.
243+
if (self->_locked) {
244+
self->_mutex.release(); // DEV: This releases the unique_ptr, not the mutex!
245+
}
246+
}
247+
223248
// ----------------------------------------------------------------------------
224249
static void
225250
RLock_dealloc(RLock* self)
@@ -232,6 +257,7 @@ RLock_dealloc(RLock* self)
232257
rlock_set.erase(self);
233258
}
234259

260+
_RLock_maybe_leak(self);
235261
self->_mutex = nullptr;
236262

237263
Py_TYPE(self)->tp_free((PyObject*)self);
@@ -329,6 +355,7 @@ RLock_exit(RLock* self, PyObject* args, PyObject* kwargs)
329355
static inline void
330356
RLock_reset(RLock* self)
331357
{
358+
_RLock_maybe_leak(self);
332359
self->_mutex = std::make_unique<std::recursive_timed_mutex>();
333360
self->_locked = 0;
334361
}

0 commit comments

Comments
 (0)