https://github.com/python/cpython/commit/dbb539efc31220313589ba7dd5c2674eba64c4e3
commit: dbb539efc31220313589ba7dd5c2674eba64c4e3
branch: 3.13
author: Sam Gross <[email protected]>
committer: colesbury <[email protected]>
date: 2025-11-23T15:30:37Z
summary:
[3.13] gh-120158: Fix inconsistent monitoring state when setting events too
frequently (gh-141845) (gh-141880)
If we overflowed the global version counter (i.e., after 2*24 calls to
`_PyMonitoring_SetEvents`), we bailed out after setting global monitoring
events but before instrumenting code objects, which led to assertion errors
later on.
Also add a `time.sleep()` to `test_free_threading.test_monitoring` to avoid
overflowing the global version counter.
(cherry picked from commit e457d60daafe66534283e0f79c81517634408e57)
files:
A Misc/NEWS.d/next/Core and
Builtins/2025-11-22-10-43-26.gh-issue-120158.41_rXd.rst
M Lib/test/test_free_threading/test_monitoring.py
M Python/instrumentation.c
diff --git a/Lib/test/test_free_threading/test_monitoring.py
b/Lib/test/test_free_threading/test_monitoring.py
index 35aaea76a1912c..1375e8e0c36448 100644
--- a/Lib/test/test_free_threading/test_monitoring.py
+++ b/Lib/test/test_free_threading/test_monitoring.py
@@ -72,6 +72,9 @@ def test_instrumentation(self):
break
self.during_threads()
+ # Sleep to avoid setting monitoring events too rapidly and
+ # overflowing the global version counter
+ time.sleep(0.0001)
self.after_test()
diff --git a/Misc/NEWS.d/next/Core and
Builtins/2025-11-22-10-43-26.gh-issue-120158.41_rXd.rst b/Misc/NEWS.d/next/Core
and Builtins/2025-11-22-10-43-26.gh-issue-120158.41_rXd.rst
new file mode 100644
index 00000000000000..b3b5f252ac07cb
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and
Builtins/2025-11-22-10-43-26.gh-issue-120158.41_rXd.rst
@@ -0,0 +1,2 @@
+Fix inconsistent state when enabling or disabling monitoring events too many
+times.
diff --git a/Python/instrumentation.c b/Python/instrumentation.c
index 5f10cdfc7de989..21d9692b586163 100644
--- a/Python/instrumentation.c
+++ b/Python/instrumentation.c
@@ -1989,12 +1989,12 @@ _PyMonitoring_SetEvents(int tool_id,
_PyMonitoringEventSet events)
if (existing_events == events) {
return 0;
}
- set_events(&interp->monitors, tool_id, events);
uint32_t new_version = global_version(interp) +
MONITORING_VERSION_INCREMENT;
if (new_version == 0) {
PyErr_Format(PyExc_OverflowError, "events set too many times");
return -1;
}
+ set_events(&interp->monitors, tool_id, events);
set_global_version(tstate, new_version);
#ifdef _Py_TIER2
_Py_Executors_InvalidateAll(interp, 1);
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]