Skip to content

Commit

Permalink
Merge pull request #222 from tornaria/clear-exc_value
Browse files Browse the repository at this point in the history
Use Py_CLEAR() to avoid a data race (#221)
  • Loading branch information
dimpase authored Jan 22, 2025
2 parents 068c923 + 2d37d97 commit 95ed336
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/cysignals/signals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ See ``tests.pyx`` for extensive tests.

from libc.signal cimport *
from libc.stdio cimport freopen, stdin
from cpython.ref cimport Py_XINCREF, Py_XDECREF
from cpython.ref cimport Py_XINCREF, Py_CLEAR
from cpython.exc cimport (PyErr_Occurred, PyErr_NormalizeException,
PyErr_Fetch, PyErr_Restore)
from cpython.version cimport PY_MAJOR_VERSION
Expand Down Expand Up @@ -204,7 +204,7 @@ cdef int sig_raise_exception "sig_raise_exception"(int sig, const char* msg) exc
PyErr_Fetch(&typ, &val, &tb)
PyErr_NormalizeException(&typ, &val, &tb)
Py_XINCREF(val)
Py_XDECREF(cysigs.exc_value)
Py_CLEAR(cysigs.exc_value)
cysigs.exc_value = val
PyErr_Restore(typ, val, tb)

Expand Down Expand Up @@ -362,8 +362,7 @@ cdef void verify_exc_value() noexcept:
"""
if cysigs.exc_value.ob_refcnt == 1:
# No other references => exception is certainly gone
Py_XDECREF(cysigs.exc_value)
cysigs.exc_value = NULL
Py_CLEAR(cysigs.exc_value)
return

if PyErr_Occurred() is not NULL:
Expand Down Expand Up @@ -394,8 +393,7 @@ cdef void verify_exc_value() noexcept:
pass
else:
if <PyObject*>handled is cysigs.exc_value:
Py_XDECREF(cysigs.exc_value)
cysigs.exc_value = NULL
Py_CLEAR(cysigs.exc_value)
return

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

0 comments on commit 95ed336

Please sign in to comment.