Skip to content

Commit f9754da

Browse files
committed
fix(debug): lock cb handle counters
1 parent 68af9b3 commit f9754da

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

uvloop/cbhandles.pyx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
cdef inline void _debug_cb_handles_inc(Loop loop):
2+
if _debug_cb_handles_lock != NULL:
3+
PyThread_acquire_lock(_debug_cb_handles_lock, 1)
4+
loop._debug_cb_handles_total += 1
5+
loop._debug_cb_handles_count += 1
6+
if _debug_cb_handles_lock != NULL:
7+
PyThread_release_lock(_debug_cb_handles_lock)
8+
9+
10+
cdef inline void _debug_cb_handles_dec(Loop loop):
11+
if _debug_cb_handles_lock != NULL:
12+
PyThread_acquire_lock(_debug_cb_handles_lock, 1)
13+
loop._debug_cb_handles_count -= 1
14+
if _debug_cb_handles_lock != NULL:
15+
PyThread_release_lock(_debug_cb_handles_lock)
16+
17+
118
@cython.no_gc_clear
219
@cython.freelist(DEFAULT_FREELIST_SIZE)
320
cdef class Handle:
@@ -9,8 +26,7 @@ cdef class Handle:
926
cdef inline _set_loop(self, Loop loop):
1027
self.loop = loop
1128
if UVLOOP_DEBUG:
12-
loop._debug_cb_handles_total += 1
13-
loop._debug_cb_handles_count += 1
29+
_debug_cb_handles_inc(loop)
1430
if loop._debug:
1531
self._source_traceback = extract_stack()
1632

@@ -21,7 +37,7 @@ cdef class Handle:
2137

2238
def __dealloc__(self):
2339
if UVLOOP_DEBUG and self.loop is not None:
24-
self.loop._debug_cb_handles_count -= 1
40+
_debug_cb_handles_dec(self.loop)
2541
if self.loop is None:
2642
raise RuntimeError('Handle.loop is None in Handle.__dealloc__')
2743

uvloop/loop.pyx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ from libc cimport errno
3131
from cpython cimport PyObject
3232
from cpython cimport PyErr_CheckSignals, PyErr_Occurred
3333
from cpython cimport PyThread_get_thread_ident
34+
from cpython.pythread cimport (
35+
PyThread_type_lock,
36+
PyThread_allocate_lock,
37+
PyThread_acquire_lock,
38+
PyThread_release_lock,
39+
)
3440
from cpython cimport Py_INCREF, Py_DECREF, Py_XDECREF, Py_XINCREF
3541
from cpython cimport (
3642
PyObject_GetBuffer, PyBuffer_Release, PyBUF_SIMPLE,
@@ -52,6 +58,7 @@ cdef:
5258
int PY311 = PY_VERSION_HEX >= 0x030b0000
5359
int PY313 = PY_VERSION_HEX >= 0x030d0000
5460
uint64_t MAX_SLEEP = 3600 * 24 * 365 * 100
61+
PyThread_type_lock _debug_cb_handles_lock = PyThread_allocate_lock()
5562

5663

5764
cdef _is_sock_stream(sock_type):

0 commit comments

Comments
 (0)