Skip to content

gh-162027: Fix branch monitoring events for async comprehensions and loops - #162033

Closed
itxtarun2727 wants to merge 1 commit into
python:mainfrom
itxtarun2727:gh-162027-async-monitoring-branches
Closed

itxtarun2727 wants to merge 1 commit into
python:mainfrom
itxtarun2727:gh-162027-async-monitoring-branches

Conversation

@itxtarun2727

@itxtarun2727 itxtarun2727 commented Nov 15, 2026 •

Copy link
Copy Markdown

Summary & Problem Description

In sys.monitoring, branch events for asynchronous loops were erroneous in two scenarios:

  1. Async Comprehensions: [x async for x in agen()] never emitted BRANCH_LEFT and calling co_branches() aborted with an assertion failure in instrumentation.c:3183 because NOT_TAKEN was not generated after END_SEND.
  2. Multiple async for loops: When basic blocks were reordered (e.g. moving cold CLEANUP_THROW exception blocks), the distance between SEND and END_SEND differed from the hardcoded END_SEND_OFFSET 6 in assemble.c. This caused END_ASYNC_FOR's oparg to point to CLEANUP_THROW instead of END_SEND, resulting in mismatched source offsets and debug assertion failures.

Solution

  • In Python/codegen.c, updated codegen_add_yield_from to export the exit label pointing directly to END_SEND.
  • Targeted END_ASYNC_FOR directly to end_send in both codegen_async_for() and codegen_async_comprehension_generator().
  • Added missing ADDOP(c, loc, NOT_TAKEN) to codegen_async_comprehension_generator().
  • In Python/assemble.c, removed #define END_SEND_OFFSET 6 and calculated the backwards jump offset directly from END_SEND.
  • Added regression tests in Lib/test/test_monitoring.py.

Verification

  • Tested with repro script from issue: both cases emit proper BRANCH_LEFT/BRANCH_RIGHT events matching co_branches().
  • PCbuild\amd64\python_d.exe -m test test_monitoring -v (104/104 tests passed).
  • PCbuild\amd64\python_d.exe -m test test_monitoring -R : (0 reference leaks).
  • pre-commit run passed cleanly.

Fixes #162027

Copilot AI balanced review requested due to automatic review settings November 15, 2026 08:17
@python-cla-bot

python-cla-bot Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@sobolevn sobolevn added the AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. label Nov 15, 2026
@bedevere-app

bedevere-app Bot commented Nov 15, 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.

We believe this contribution does not meet our guidelines for using AI tools, so we're closing it. If you think we've misunderstood your contribution, please let us know. You're welcome to explain your approach or ask questions about the guidelines.

Please read our guidelines for using AI tools.

@bedevere-app bedevere-app Bot closed this Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sys.monitoring: wrong branch events for async comprehensions and for a second async for loop

3 participants