Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-98257: Make _PyEval_SetTrace() reentrant #98258

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Lib/test/test_sys_setprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,8 @@ def __del__(self):
sys.setprofile(bar)

sys.setprofile(A())
with support.catch_unraisable_exception() as cm:
sys.setprofile(foo)
self.assertEqual(cm.unraisable.object, A.__del__)
self.assertIsInstance(cm.unraisable.exc_value, RuntimeError)

self.assertEqual(sys.getprofile(), foo)
sys.setprofile(foo)
self.assertEqual(sys.getprofile(), bar)


def test_same_object(self):
Expand Down
8 changes: 2 additions & 6 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2798,12 +2798,8 @@ def __del__(self):
sys.settrace(bar)

sys.settrace(A())
with support.catch_unraisable_exception() as cm:
sys.settrace(foo)
self.assertEqual(cm.unraisable.object, A.__del__)
self.assertIsInstance(cm.unraisable.exc_value, RuntimeError)

self.assertEqual(sys.gettrace(), foo)
sys.settrace(foo)
self.assertEqual(sys.gettrace(), bar)


def test_same_object(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make :func:`sys.setprofile` and :func:`sys.settrace` functions reentrant. They
can no long fail with: ``RuntimeError("Cannot install a trace function while
another trace function is being installed")``. Patch by Victor Stinner.
57 changes: 13 additions & 44 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -6343,38 +6343,23 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
/* The caller must hold the GIL */
assert(PyGILState_Check());

static int reentrant = 0;
if (reentrant) {
_PyErr_SetString(tstate, PyExc_RuntimeError, "Cannot install a profile function "
"while another profile function is being installed");
reentrant = 0;
return -1;
}
reentrant = 1;

/* Call _PySys_Audit() in the context of the current thread state,
even if tstate is not the current thread state. */
PyThreadState *current_tstate = _PyThreadState_GET();
if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
reentrant = 0;
return -1;
}

PyObject *profileobj = tstate->c_profileobj;

tstate->c_profilefunc = NULL;
tstate->c_profileobj = NULL;
/* Must make sure that tracing is not ignored if 'profileobj' is freed */
_PyThreadState_UpdateTracingState(tstate);
Py_XDECREF(profileobj);

Py_XINCREF(arg);
tstate->c_profileobj = arg;
tstate->c_profilefunc = func;

PyObject *old_profileobj = tstate->c_profileobj;
tstate->c_profileobj = Py_XNewRef(arg);
/* Flag that tracing or profiling is turned on */
_PyThreadState_UpdateTracingState(tstate);
reentrant = 0;

// gh-98257: Only call Py_XDECREF() once the new profile function is fully
// set, so it's safe to call sys.setprofile() again (reentrant call).
Py_XDECREF(old_profileobj);

return 0;
}

Expand Down Expand Up @@ -6416,39 +6401,23 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
/* The caller must hold the GIL */
assert(PyGILState_Check());

static int reentrant = 0;

if (reentrant) {
_PyErr_SetString(tstate, PyExc_RuntimeError, "Cannot install a trace function "
"while another trace function is being installed");
reentrant = 0;
return -1;
}
reentrant = 1;

/* Call _PySys_Audit() in the context of the current thread state,
even if tstate is not the current thread state. */
PyThreadState *current_tstate = _PyThreadState_GET();
if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
reentrant = 0;
return -1;
}

PyObject *traceobj = tstate->c_traceobj;

tstate->c_tracefunc = NULL;
tstate->c_traceobj = NULL;
/* Must make sure that profiling is not ignored if 'traceobj' is freed */
_PyThreadState_UpdateTracingState(tstate);
Py_XINCREF(arg);
Py_XDECREF(traceobj);
tstate->c_traceobj = arg;
tstate->c_tracefunc = func;

PyObject *old_traceobj = tstate->c_traceobj;
tstate->c_traceobj = Py_XNewRef(arg);
/* Flag that tracing or profiling is turned on */
_PyThreadState_UpdateTracingState(tstate);

reentrant = 0;
// gh-98257: Only call Py_XDECREF() once the new trace function is fully
// set, so it's safe to call sys.settrace() again (reentrant call).
Py_XDECREF(old_traceobj);

return 0;
}

Expand Down