From a4cc0af4fbb74402249540694be6a269ec6f8379 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Tue, 16 Jun 2026 10:46:26 +0800 Subject: [PATCH 1/3] gh-151524: Avoid using instrumentation callback result after Py_DECREF --- Python/instrumentation.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 0af2070b5cd983a..733aa59594e7ddb 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -989,8 +989,9 @@ call_one_instrument( if (res == NULL) { return -1; } + int is_disable = (res == &_PyInstrumentation_DISABLE); Py_DECREF(res); - return (res == &_PyInstrumentation_DISABLE); + return is_disable; } static const int8_t MOST_SIGNIFICANT_BITS[16] = { From 137a1bf5aad9c18cec7dbccf7223444fcfd9b0fe Mon Sep 17 00:00:00 2001 From: lipengyu Date: Tue, 16 Jun 2026 13:54:29 +0800 Subject: [PATCH 2/3] add NEWS --- .../2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst new file mode 100644 index 000000000000000..3f780bbeeb50714 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst @@ -0,0 +1,2 @@ +Avoid comparing the result of a ``sys.monitoring`` callback after +decrementing its reference count. From 67821dc92853684d462d15dcda0aaab1fc07be7f Mon Sep 17 00:00:00 2001 From: lipengyu Date: Tue, 16 Jun 2026 18:56:11 +0800 Subject: [PATCH 3/3] update --- .../2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst | 2 -- Python/instrumentation.c | 7 +++++-- 2 files changed, 5 insertions(+), 4 deletions(-) delete mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst deleted file mode 100644 index 3f780bbeeb50714..000000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-16-13-50-13.gh-issue-151377.mCqrGg.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid comparing the result of a ``sys.monitoring`` callback after -decrementing its reference count. diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 733aa59594e7ddb..806d3fbf5d6b192 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -989,9 +989,12 @@ call_one_instrument( if (res == NULL) { return -1; } - int is_disable = (res == &_PyInstrumentation_DISABLE); + if (res == &_PyInstrumentation_DISABLE) { + assert(_Py_IsImmortal(res)); + return 1; + } Py_DECREF(res); - return is_disable; + return 0; } static const int8_t MOST_SIGNIFICANT_BITS[16] = {