Skip to content

Commit 1a798d3

Browse files
[3.14] gh-120158: Fix inconsistent monitoring state when setting events too frequently (gh-141845) (gh-141879)
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 e457d60) Co-authored-by: Sam Gross <colesbury@gmail.com>
1 parent 59ab1ed commit 1a798d3

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

Lib/test/test_free_threading/test_monitoring.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def test_instrumentation(self):
7373
break
7474

7575
self.during_threads()
76+
# Sleep to avoid setting monitoring events too rapidly and
77+
# overflowing the global version counter
78+
time.sleep(0.0001)
7679

7780
self.after_test()
7881

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix inconsistent state when enabling or disabling monitoring events too many
2+
times.

Python/instrumentation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2019,12 +2019,12 @@ _PyMonitoring_SetEvents(int tool_id, _PyMonitoringEventSet events)
20192019
if (existing_events == events) {
20202020
return 0;
20212021
}
2022-
set_events(&interp->monitors, tool_id, events);
20232022
uint32_t new_version = global_version(interp) + MONITORING_VERSION_INCREMENT;
20242023
if (new_version == 0) {
20252024
PyErr_Format(PyExc_OverflowError, "events set too many times");
20262025
return -1;
20272026
}
2027+
set_events(&interp->monitors, tool_id, events);
20282028
set_global_version(tstate, new_version);
20292029
#ifdef _Py_TIER2
20302030
_Py_Executors_InvalidateAll(interp, 1);

0 commit comments

Comments
 (0)