Skip to content

Commit 95ed336

Browse files
authored
Merge pull request #222 from tornaria/clear-exc_value
Use Py_CLEAR() to avoid a data race (#221)
2 parents 068c923 + 2d37d97 commit 95ed336

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/cysignals/signals.pyx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ See ``tests.pyx`` for extensive tests.
2525

2626
from libc.signal cimport *
2727
from libc.stdio cimport freopen, stdin
28-
from cpython.ref cimport Py_XINCREF, Py_XDECREF
28+
from cpython.ref cimport Py_XINCREF, Py_CLEAR
2929
from cpython.exc cimport (PyErr_Occurred, PyErr_NormalizeException,
3030
PyErr_Fetch, PyErr_Restore)
3131
from cpython.version cimport PY_MAJOR_VERSION
@@ -204,7 +204,7 @@ cdef int sig_raise_exception "sig_raise_exception"(int sig, const char* msg) exc
204204
PyErr_Fetch(&typ, &val, &tb)
205205
PyErr_NormalizeException(&typ, &val, &tb)
206206
Py_XINCREF(val)
207-
Py_XDECREF(cysigs.exc_value)
207+
Py_CLEAR(cysigs.exc_value)
208208
cysigs.exc_value = val
209209
PyErr_Restore(typ, val, tb)
210210

@@ -362,8 +362,7 @@ cdef void verify_exc_value() noexcept:
362362
"""
363363
if cysigs.exc_value.ob_refcnt == 1:
364364
# No other references => exception is certainly gone
365-
Py_XDECREF(cysigs.exc_value)
366-
cysigs.exc_value = NULL
365+
Py_CLEAR(cysigs.exc_value)
367366
return
368367

369368
if PyErr_Occurred() is not NULL:
@@ -394,8 +393,7 @@ cdef void verify_exc_value() noexcept:
394393
pass
395394
else:
396395
if <PyObject*>handled is cysigs.exc_value:
397-
Py_XDECREF(cysigs.exc_value)
398-
cysigs.exc_value = NULL
396+
Py_CLEAR(cysigs.exc_value)
399397
return
400398

401399
# To be safe, we run the garbage collector because it may clear
@@ -411,5 +409,4 @@ cdef void verify_exc_value() noexcept:
411409
# called again during garbage collection it might have already been set
412410
# to NULL; see https://github.com/sagemath/cysignals/issues/126
413411
if cysigs.exc_value != NULL and cysigs.exc_value.ob_refcnt == 1:
414-
Py_XDECREF(cysigs.exc_value)
415-
cysigs.exc_value = NULL
412+
Py_CLEAR(cysigs.exc_value)

0 commit comments

Comments
 (0)