Skip to content

gh-94510: Raise on re-entrant calls to sys.setprofile and sys.settrace #94511

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

Merged
merged 3 commits into from
Jul 5, 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
39 changes: 39 additions & 0 deletions Lib/test/test_sys_setprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pprint
import sys
import unittest
from test import support


class TestGetProfile(unittest.TestCase):
Expand Down Expand Up @@ -415,5 +416,43 @@ def show_events(callable):
pprint.pprint(capture_events(callable))


class TestEdgeCases(unittest.TestCase):

def setUp(self):
self.addCleanup(sys.setprofile, sys.getprofile())
sys.setprofile(None)

def test_reentrancy(self):
def foo(*args):
...

def bar(*args):
...

class A:
def __call__(self, *args):
pass

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)


def test_same_object(self):
def foo(*args):
...

sys.setprofile(foo)
del foo
sys.setprofile(sys.getprofile())


if __name__ == "__main__":
unittest.main()
39 changes: 39 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from test import support
import unittest
from unittest.mock import MagicMock
import sys
import difflib
import gc
Expand Down Expand Up @@ -2684,5 +2685,43 @@ def f():
self.assertEqual(counts, {'call': 1, 'line': 2000, 'return': 1})


class TestEdgeCases(unittest.TestCase):

def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())
sys.settrace(None)

def test_reentrancy(self):
def foo(*args):
...

def bar(*args):
...

class A:
def __call__(self, *args):
pass

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)


def test_same_object(self):
def foo(*args):
...

sys.settrace(foo)
del foo
sys.settrace(sys.gettrace())


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Re-entrant calls to :func:`sys.setprofile` and :func:`sys.settrace` now
raise :exc:`RuntimeError`. Patch by Pablo Galindo.
2 changes: 1 addition & 1 deletion Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ profiler_dealloc(ProfilerObject *op)
if (op->flags & POF_ENABLED) {
PyThreadState *tstate = _PyThreadState_GET();
if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) {
PyErr_WriteUnraisable((PyObject *)op);
_PyErr_WriteUnraisableMsg("When destroying _lsprof profiler", NULL);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestigly, I catched this bug with the change: the object here is dead so is not correct to pass it to the unraisable hook.

}
}

Expand Down
26 changes: 24 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -6973,10 +6973,20 @@ _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;
}

Expand All @@ -6994,6 +7004,7 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)

/* Flag that tracing or profiling is turned on */
_PyThreadState_UpdateTracingState(tstate);
reentrant = 0;
return 0;
}

Expand All @@ -7014,10 +7025,21 @@ _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;
}

Expand All @@ -7027,15 +7049,15 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
tstate->c_traceobj = NULL;
/* Must make sure that profiling is not ignored if 'traceobj' is freed */
_PyThreadState_UpdateTracingState(tstate);
Py_XDECREF(traceobj);

Py_XINCREF(arg);
Py_XDECREF(traceobj);
Copy link
Member Author

@pablogsal pablogsal Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reversed this order to avoid dec-refing too much traceobj if traceobj and arg are the same objects and we held the last ref.

tstate->c_traceobj = arg;
tstate->c_tracefunc = func;

/* Flag that tracing or profiling is turned on */
_PyThreadState_UpdateTracingState(tstate);

reentrant = 0;
return 0;
}

Expand Down