From e457d60daafe66534283e0f79c81517634408e57 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Sun, 23 Nov 2025 10:07:17 -0500 Subject: [PATCH 1/2] 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. --- Lib/test/test_free_threading/test_monitoring.py | 3 +++ .../2025-11-22-10-43-26.gh-issue-120158.41_rXd.rst | 2 ++ Python/instrumentation.c | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-11-22-10-43-26.gh-issue-120158.41_rXd.rst diff --git a/Lib/test/test_free_threading/test_monitoring.py b/Lib/test/test_free_threading/test_monitoring.py index 407bf7cbdee917..4fbd3f3415cb32 100644 --- a/Lib/test/test_free_threading/test_monitoring.py +++ b/Lib/test/test_free_threading/test_monitoring.py @@ -73,6 +73,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 72b7433022fdea..9e750433cffa89 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2021,12 +2021,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); From ecb901dd87f68195e74ce9facc4f72dd71c1a044 Mon Sep 17 00:00:00 2001 From: yihong Date: Mon, 24 Nov 2025 00:33:05 +0800 Subject: [PATCH 2/2] Drop three unused imports (#141875) --- Lib/_pyrepl/simple_interact.py | 1 - Lib/asyncio/tools.py | 2 +- Lib/shelve.py | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index ff1bdab9fea078..3b0debf2ba037b 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -31,7 +31,6 @@ import sys import code import warnings -import errno from .readline import _get_reader, multiline_input, append_history_file diff --git a/Lib/asyncio/tools.py b/Lib/asyncio/tools.py index f39e11fdd513b4..1d463ea09ba5b8 100644 --- a/Lib/asyncio/tools.py +++ b/Lib/asyncio/tools.py @@ -1,6 +1,6 @@ """Tools to analyze tasks running in asyncio programs.""" -from collections import defaultdict, namedtuple +from collections import defaultdict from itertools import count from enum import Enum import sys diff --git a/Lib/shelve.py b/Lib/shelve.py index 1010be1e09d702..9f6296667fdb6b 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -57,7 +57,6 @@ """ from pickle import DEFAULT_PROTOCOL, dumps, loads -from io import BytesIO import collections.abc