GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sys.monitoring `PY_UNWIND` not being delivered when a generator is closed/garbage collected · Issue #140373 · python/cpython

sys.monitoring PY_UNWIND not being delivered when a generator is closed/garbage collected #140373

New issue
Copy link
New issue
Copy link
Closed
Closed
sys.monitoring PY_UNWIND not being delivered when a generator is closed/garbage collected#140373
Copy link
Assignees
efimov-mikhail
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@jaltmayerpizzorno
jaltmayerpizzorno
opened on Oct 20, 2025
Issue body actions

Bug report

Bug description:

With Python 3.12, this code generates a PY_UNWIND event; with 3.13 and later, it no longer does.

import sys

def gen():
    yield 1
    yield 2

def callback(code, off, exc):
    if code is gen.__code__:
        print("got unwind!")

ID=3
sysmon = sys.monitoring
sysmon.use_tool_id(ID, "test")
sysmon.register_callback(ID, sysmon.events.PY_UNWIND, callback)
sysmon.set_events(ID, sysmon.events.PY_UNWIND)

g = gen()
next(g)
del g  # or g.close()

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

macOS, Linux

Linked PRs

  • gh-140373: Correctly emit PY_UNWIND event when generator is closed #140767
  • [3.14] gh-140373: Correctly emit PY_UNWIND event when generator is closed (GH-140767) #140816
  • [3.13] gh-140373: Correctly emit PY_UNWIND event when generator is closed (GH-140767) #140821
Reacted by Mikhail Efimov

Activity

  1. jaltmayerpizzorno
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 20, 2025
  2. efimov-mikhail commented on Oct 20, 2025

    @efimov-mikhail
    efimov-mikhail
    on Oct 20, 2025
    Member
    More actions

    Thanks for the report, confirmed on 3.13, 3.14 and main.

    Reacted by Juan Altmayer Pizzorno
  3. efimov-mikhail
    added
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    3.15bugs and security fixesbugs and security fixes
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Oct 20, 2025
  4. Zheaoli commented on Oct 20, 2025

    @Zheaoli
    Zheaoli
    on Oct 20, 2025
    Contributor
    More actions

    Seems related with https://github.com/python/cpython/pull/111069/files

  5. Zheaoli commented on Oct 20, 2025

    @Zheaoli
    Zheaoli
    on Oct 20, 2025
    Contributor
    More actions

    https://github.com/python/cpython/blob/main/Objects/genobject.c#L413-L420

    Seems we add a short path to just return instead of raise an exception when there is only one level generator

  6. markshannon commented on Oct 21, 2025

    @markshannon
    markshannon
    on Oct 21, 2025
    Member
    More actions

    What happening is that in #111069 we skipped the expensive raise then catch if the generator is not in a try block, as there would (or so we thought) be no observable effect.
    But, if there is no exception raised, there is no unwinding, and noPY_UNWIND event.

    This is fine for finalization, as the language is quite clear that finalization may never happen,
    but the docs explicitly state that an exception will be raised when generator.close() is called.

    If we are to fix this for generator.close() we might as well do it for finalization for consistency.

    My intended purpose of PY_UNWIND is that profilers and other tools can balance entries and exits to functions: for every PY_START or PY_RESUME there should be a PY_RETURN, PY_YIELD or PY_UNWIND, but it does seem that you ought to be able to use the event to detect generator close/finalization.

    So, do we add the additional check for instrumentation in generator.close() or do we update the docs to say that the exception may not be raised if the generator is not currently executing in a try block?

    @jaltmayerpizzorno how did you notice this, and is it breaking anything?

  7. jaltmayerpizzorno commented on Oct 21, 2025

    @jaltmayerpizzorno
    jaltmayerpizzorno
    on Oct 21, 2025
    Author
    More actions

    @jaltmayerpizzorno how did you notice this, and is it breaking anything?

    Hi @markshannon! RightTyper uses PY_START and PY_UNWIND; the PY_UNWIND is useful for detecting (in the absence of PY_RETURN) when a function doesn't return a value, or when a stack frame is done, so that it doesn't possibly take it for another call that happened to reuse the frame.

    To observe types, RightTyper executes the target program (using runpy). Once that execution ends, generators (but apparently not async generators or coroutines) may remain running. RightTyper used to close() them to have PY_UNWIND process normally. I've now changed that to a throw(GeneratorExit), which works around the issue for us... but I thought I'd raise the issue anyway (pun partly intended, sorry).

    I think generally it's useful to be able to tell when a call is done (by getting either PY_RETURN and PY_UNWIND), but you'd know best what it costs to support it.

  8. markshannon commented on Oct 22, 2025

    @markshannon
    markshannon
    on Oct 22, 2025
    Member
    More actions

    It sounds like it is being relied on, and it is in the spec. So, we'd better fix it.
    It's not a complicated fix and I expect the performance impact will not be measurable, and definitely not significant.

    Reacted by Juan Altmayer Pizzorno
  9. efimov-mikhail commented on Oct 22, 2025

    @efimov-mikhail
    efimov-mikhail
    on Oct 22, 2025
    Member
    More actions

    I'd like to prepare a fix for this.

    Reacted by Mark Shannon
  10. efimov-mikhail
    self-assigned this
    on Oct 24, 2025
  11. jaltmayerpizzorno commented on Oct 27, 2025

    @jaltmayerpizzorno
    jaltmayerpizzorno
    on Oct 27, 2025
    Author
    More actions

    It sounds like it is being relied on, and it is in the spec. So, we'd better fix it.

    In short: yes; thank you!

    Reacted by Mikhail Efimov
  12. markshannon
    added a commit that references this issue on Oct 31, 2025

    gh-140373: Correctly emit `PY_UNWIND` event when generator is closed (G…

    d17f28f
  13. miss-islington
    added a commit that references this issue on Oct 31, 2025

    pythongh-140373: Correctly emit `PY_UNWIND` event when generator is c…

    615943f
  14. efimov-mikhail
    added a commit that references this issue on Oct 31, 2025

    pythongh-140373: Correctly emit `PY_UNWIND` event when generator is c…

    1d92049
  15. efimov-mikhail commented on Nov 6, 2025

    @efimov-mikhail
    efimov-mikhail
    on Nov 6, 2025
    Member
    More actions

    cc @hugovk
    Could you please merge backports for this issue?
    The CI is green and changes are little.

  16. hugovk
    added a commit that references this issue on Nov 30, 2025

    [3.14] gh-140373: Correctly emit `PY_UNWIND` event when generator is …

    52731c4
  17. StanFromIreland
    added a commit that references this issue on Dec 6, 2025

    pythongh-140373: Correctly emit `PY_UNWIND` event when generator is c…

    cd956f5
  18. hugovk
    added a commit that references this issue on Dec 20, 2025

    [3.13] gh-140373: Correctly emit PY_UNWIND event when generator is cl…

    4504ff8
  19. hugovk
    closed this as completedon Dec 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @efimov-mikhail
    efimov-mikhail

Labels

3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.