Skip to content

Commit dbb539e

Browse files
authored
[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 e457d60)
1 parent e2d320b commit dbb539e

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
@@ -72,6 +72,9 @@ def test_instrumentation(self):
7272
break
7373

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

7679
self.after_test()
7780

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
@@ -1989,12 +1989,12 @@ _PyMonitoring_SetEvents(int tool_id, _PyMonitoringEventSet events)
19891989
if (existing_events == events) {
19901990
return 0;
19911991
}
1992-
set_events(&interp->monitors, tool_id, events);
19931992
uint32_t new_version = global_version(interp) + MONITORING_VERSION_INCREMENT;
19941993
if (new_version == 0) {
19951994
PyErr_Format(PyExc_OverflowError, "events set too many times");
19961995
return -1;
19971996
}
1997+
set_events(&interp->monitors, tool_id, events);
19981998
set_global_version(tstate, new_version);
19991999
#ifdef _Py_TIER2
20002000
_Py_Executors_InvalidateAll(interp, 1);

0 commit comments

Comments
 (0)