Skip to content

gh-161928: Fix spurious async for exit events in sys.monitoring - #162928

Open
LUX-ETVERITAS wants to merge 2 commits into
python:mainfrom
LUX-ETVERITAS:gh-161928
Open

LUX-ETVERITAS wants to merge 2 commits into
python:mainfrom
LUX-ETVERITAS:gh-161928

Conversation

@LUX-ETVERITAS

@LUX-ETVERITAS LUX-ETVERITAS commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #161928.

When an async iterator raises an exception such as ValueError, sys.monitoring reports a BRANCH_RIGHT event pointing past the loop even though execution never reaches that point. Emit the loop-exit event only when the exception matches StopAsyncIteration. This preserves the order of the callback and stack cleanup on normal exhaustion.

The regression test records BRANCH_RIGHT events for an async generator that yields once and then raises ValueError. It catches the extra event on main and passes with the fix. The adjacent test covers normal exhaustion.

Verification included test_monitoring, a reference-leak check for its async-for tests, and make patchcheck. After rerunning the failures from the full local suite, only test_socket.test_host_resolution_bad_address still failed. The system Python also resolves the malformed hostname used by that test.

Codex helped write the patch and run the tests.

@python-cla-bot

python-cla-bot Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@picnixz picnixz added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Dec 1, 2026
@picnixz

picnixz commented Dec 1, 2026

Copy link
Copy Markdown
Member

How much of the fix do you yourself understand and would explain to someone who doesn't know the bytecode? Do you understand the rationale why codex did some changes?

@bedevere-app

bedevere-app Bot commented Dec 1, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sys.monitoring: async for emits BRANCH_RIGHT when __anext__ raises an exception other than StopAsyncIteration

2 participants