Skip to content

Commit e457d60

Browse files
authored
gh-120158: Fix inconsistent monitoring state when setting events too frequently (gh-141845)
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.
1 parent 614a28b commit e457d60

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
@@ -2021,12 +2021,12 @@ _PyMonitoring_SetEvents(int tool_id, _PyMonitoringEventSet events)
20212021
if (existing_events == events) {
20222022
return 0;
20232023
}
2024-
set_events(&interp->monitors, tool_id, events);
20252024
uint32_t new_version = global_version(interp) + MONITORING_VERSION_INCREMENT;
20262025
if (new_version == 0) {
20272026
PyErr_Format(PyExc_OverflowError, "events set too many times");
20282027
return -1;
20292028
}
2029+
set_events(&interp->monitors, tool_id, events);
20302030
set_global_version(tstate, new_version);
20312031
#ifdef _Py_TIER2
20322032
_Py_Executors_InvalidateAll(interp, 1);

0 commit comments

Comments
 (0)