From a98c52a7ca9c5f24bdc85f658ad9273501bbc570 Mon Sep 17 00:00:00 2001 From: zibuyu Date: Tue, 1 Dec 2026 22:44:22 +0800 Subject: [PATCH] gh-161928: Fix spurious async for exit events in sys.monitoring --- Lib/test/test_monitoring.py | 17 +++++++++++++++++ ...26-12-01-21-37-55.gh-issue-161928.25919f.rst | 3 +++ Modules/_testinternalcapi/test_cases.c.h | 7 ++++++- Python/bytecodes.c | 7 +++++-- Python/generated_cases.c.h | 7 ++++++- 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-12-01-21-37-55.gh-issue-161928.25919f.rst diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 24cc51f4b4053a..cbdf8ccae9791c 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -2089,6 +2089,23 @@ async def foo(): ('branch right', 'foo', 1, 3), ('branch left', 'func', 12, 12)]) + def test_async_for_exception(self): + async def items(): + yield 1 + raise ValueError + + async def consume(): + async for item in items(): + pass + + def run(): + try: + consume().send(None) + except ValueError: + pass + + self.check_events(run, expected=[], recorders=(BranchRightRecorder,)) + def test_match(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-01-21-37-55.gh-issue-161928.25919f.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-01-21-37-55.gh-issue-161928.25919f.rst new file mode 100644 index 00000000000000..3fcae572079719 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-01-21-37-55.gh-issue-161928.25919f.rst @@ -0,0 +1,3 @@ +Fix :mod:`sys.monitoring` reporting a loop-exit branch when an +:keyword:`async for` iterator raises an exception other than +:exc:`StopAsyncIteration`. diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 8bb3f857437792..9de8a450bfc8c5 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -7558,12 +7558,17 @@ frame->instr_ptr = next_instr; next_instr += 1; INSTRUCTION_STATS(INSTRUMENTED_END_ASYNC_FOR); + _PyStackRef *exc_slot; _PyStackRef awaitable_st; _PyStackRef exc_st; // _MONITOR_END_ASYNC_FOR { + exc_slot = &stack_pointer[-1]; assert((next_instr-oparg)->op.code == END_SEND || (next_instr-oparg)->op.code >= MIN_INSTRUMENTED_OPCODE); - INSTRUMENTED_JUMP(next_instr-oparg, this_instr+1, PY_MONITORING_EVENT_BRANCH_RIGHT); + PyObject *exc = PyStackRef_AsPyObjectBorrow(exc_slot[0]); + if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { + INSTRUMENTED_JUMP(next_instr-oparg, this_instr+1, PY_MONITORING_EVENT_BRANCH_RIGHT); + } } // _END_ASYNC_FOR { diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c6f1f61f3e08fe..19b5fe0d7e04cf 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1953,9 +1953,12 @@ dummy_func( } } - tier1 op(_MONITOR_END_ASYNC_FOR, ( -- )) { + tier1 op(_MONITOR_END_ASYNC_FOR, (exc_slot[1] -- exc_slot[1])) { assert((next_instr-oparg)->op.code == END_SEND || (next_instr-oparg)->op.code >= MIN_INSTRUMENTED_OPCODE); - INSTRUMENTED_JUMP(next_instr-oparg, this_instr+1, PY_MONITORING_EVENT_BRANCH_RIGHT); + PyObject *exc = PyStackRef_AsPyObjectBorrow(exc_slot[0]); + if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { + INSTRUMENTED_JUMP(next_instr-oparg, this_instr+1, PY_MONITORING_EVENT_BRANCH_RIGHT); + } } macro(INSTRUMENTED_END_ASYNC_FOR) = diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3af7863e80d2c6..47e087da8c4494 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -7558,12 +7558,17 @@ frame->instr_ptr = next_instr; next_instr += 1; INSTRUCTION_STATS(INSTRUMENTED_END_ASYNC_FOR); + _PyStackRef *exc_slot; _PyStackRef awaitable_st; _PyStackRef exc_st; // _MONITOR_END_ASYNC_FOR { + exc_slot = &stack_pointer[-1]; assert((next_instr-oparg)->op.code == END_SEND || (next_instr-oparg)->op.code >= MIN_INSTRUMENTED_OPCODE); - INSTRUMENTED_JUMP(next_instr-oparg, this_instr+1, PY_MONITORING_EVENT_BRANCH_RIGHT); + PyObject *exc = PyStackRef_AsPyObjectBorrow(exc_slot[0]); + if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { + INSTRUMENTED_JUMP(next_instr-oparg, this_instr+1, PY_MONITORING_EVENT_BRANCH_RIGHT); + } } // _END_ASYNC_FOR {