Skip to content

gh-137400: Fix thread-safety issues when profiling all threads #137518

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix another race with tracing
  • Loading branch information
colesbury committed Aug 7, 2025
commit e8ab41dee5abb10a1b6bb783bb97a7003ee1bd74
32 changes: 32 additions & 0 deletions Lib/test/test_free_threading/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,38 @@ def noop():

self.observe_threads(noop, buf)

def test_trace_concurrent(self):
# Test calling a function concurrently from a tracing and a non-tracing
Copy link
Member

Choose a reason for hiding this comment

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

What was the behavior of this test when this was not fixed? Will it crash because the code object is partially instrumented, or will we miss some trace func calls because the code object is not instrumented at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mostly it showed up as data races reported by TSan. I don't think I saw any crashes for this one.

The test above (SetProfileAllThreadsMultiThreaded) would crash before this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For clarification, this test is related to the change to _MAYBE_INSTRUMENT with int check_instrumentation = (tstate->tracing == 0);

# thread
b = threading.Barrier(2)

def func():
for _ in range(100):
pass

def noop():
pass

def bg_thread():
b.wait()
func() # this may instrument `func`

def tracefunc(frame, event, arg):
# These calls run under tracing can race with the background thread
for _ in range(10):
func()
return tracefunc

t = Thread(target=bg_thread)
t.start()
try:
sys.settrace(tracefunc)
b.wait()
noop()
finally:
sys.settrace(None)
t.join()


if __name__ == "__main__":
unittest.main()
10 changes: 9 additions & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,15 @@ dummy_func(
}

tier1 op(_MAYBE_INSTRUMENT, (--)) {
if (tstate->tracing == 0) {
#ifdef Py_GIL_DISABLED
// For thread-safety, we need to check instrumentation version
// even when tracing. Otherwise, another thread may concurrently
// re-write the bytecode while we are executing this function.
int check_instrumentation = 1;
#else
int check_instrumentation = (tstate->tracing == 0);
#endif
if (check_instrumentation) {
uintptr_t global_version = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) & ~_PY_EVAL_EVENTS_MASK;
uintptr_t code_version = FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(_PyFrame_GetCode(frame)->_co_instrumentation_version);
if (code_version != global_version) {
Expand Down
16 changes: 14 additions & 2 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading