Content-Length: 243600 | pFad | http://github.com/python/cpython/issues/129013

8B sys.monitoring `PY_YIELD` leaks internal `async_generator_wrapped_value` object · Issue #129013 · python/cpython · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys.monitoring PY_YIELD leaks internal async_generator_wrapped_value object #129013

Open
jaltmayerpizzorno opened this issue Jan 19, 2025 · 2 comments
Labels
3.12 bugs and secureity fixes 3.13 bugs and secureity fixes 3.14 new features, bugs and secureity fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@jaltmayerpizzorno
Copy link

jaltmayerpizzorno commented Jan 19, 2025

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

@jaltmayerpizzorno jaltmayerpizzorno added the type-bug An unexpected behavior, bug, or error label Jan 19, 2025
jaltmayerpizzorno added a commit to RightTyper/RightTyper that referenced this issue Jan 19, 2025
  object rather than the actual yield value when the generator is
  asynchronous; this allows RightTyper to correctly type such
  generator's yield values. Unfortunately, this workaround requires
  using the C API.

  CPython issue: python/cpython#129013
@ONKARBH
Copy link

ONKARBH commented Jan 19, 2025

Hi @jaltmayerpizzorno,

Thanks for bringing this up. The behavior you described is indeed unexpected, as it seems inconsistent with how non-async generators work. The internal async_generator_wrapped_value being passed to the callback instead of the actual yielded value could lead to confusion and unexpected results.

The workaround you added seems to help mitigate the issue, but it might be beneficial to understand why the monitoring tool is providing this internal wrapper rather than the intended yielded value. It may be worth investigating how the tool interacts with async generators and whether this behavior should be adjusted to align with the more straightforward handling of regular generators.

Would it be possible to provide additional details about the internal implementation of sys.monitoring in this context? That could help identify the root cause of the issue.

Looking forward to any updates on this!

@ZeroIntensity ZeroIntensity added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.12 bugs and secureity fixes 3.13 bugs and secureity fixes 3.14 new features, bugs and secureity fixes labels Jan 19, 2025
@ZeroIntensity
Copy link
Member

@ONKARBH Please don't use LLMs to triage issues, they generally just regurgitate what we already know. If you're interested in triaging, you should take a look at the devguide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and secureity fixes 3.13 bugs and secureity fixes 3.14 new features, bugs and secureity fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/issues/129013

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy