python / python/cpython

sys.monitoring `PY_YIELD` leaks internal `async_generator_wrapped_value` object

Open
#129,013 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Rather than the actual value, sys.monitoring's PY_YIELD event passes in to the callback the internal async_generator_wrapped_value.
I imagine this is a bug... it's inconsistent with what it does for non-async generators.

import sys
import types
import typing
import asyncio

async def gen():
    yield 42

async def main():
    async for _ in gen():
        pass

def handle_yield(code: types.CodeType, offset: int, value: object) -> typing.Any:
    if code.co_filename == __file__:
        print(f"yield name={code.co_qualname} {type(value)=}")

sysmon = sys.monitoring
sysmon.use_tool_id(4, "r")
sysmon.register_callback(4, sysmon.events.PY_YIELD, handle_yield)
sysmon.set_events(4, sysmon.events.PY_YIELD)

asyncio.run(main())

This prints out:

yield name=gen type(value)=<class 'async_generator_wrapped_value'>
CPython versions tested on:

3.12, 3.13, 3.14

Operating systems tested on:

macOS, Linux

Linked PRs
  • gh-129031

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided sys.monitoring PY_YIELD reproduction across the affected CPython versions and inspect the callback behavior for async generators. Review linked PR gh-129031 for the current implementation direction; done means the callback receives the yielded value rather than the internal async_generator_wrapped_value object, with coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.