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 — PyFunction_GetAnnotations is probably causing ref leaks · Issue #97943 · python/cpython

PyFunction_GetAnnotations is probably causing ref leaks #97943

New issue
Copy link
New issue
Copy link
Closed
Closed
PyFunction_GetAnnotations is probably causing ref leaks#97943
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@larryhastings
larryhastings
opened on Oct 5, 2022
Issue body actions

In issue #30409 PyFunction_GetAnnotations was fixed so it wouldn't return a tuple. That's good.

But this change changed the return value. PyFunction_GetAnnotations now returns a new reference to the annotations dict. But according to the documentation, it should return a borrowed reference.

https://docs.python.org/3/c-api/function.html#c.PyFunction_GetAnnotations

If users only read the documentation, and assume that it's correct, they'll leak a reference to the annotation dict.

We could fix this by changing the documentation. However, all the other PyFunction_Get* functions also return borrowed references. So I think the correct fix is to remove the incref.

Activity

  1. larryhastings
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 5, 2022
  2. larryhastings commented on Oct 5, 2022

    @larryhastings
    larryhastings
    on Oct 5, 2022
    ContributorAuthor
    More actions

    IDK how to tag people, so I'll just do it in a comment:

    @pablogsal @methane

  3. larryhastings
    changed the title [-]PyFunction_GetAnnotations should be causing ref leaks[/-] [+]PyFunction_GetAnnotations is probably causing ref leaks[/+] on Oct 5, 2022
  4. larryhastings commented on Oct 5, 2022

    @larryhastings
    larryhastings
    on Oct 5, 2022
    ContributorAuthor
    More actions

    Also, I consider this enough of a bug fix that it should be backported to versions still getting bug fixes.

  5. larryhastings
    added a commit that references this issue on Oct 6, 2022

    pythongh-97943: PyFunction_GetAnnotations should return a borrowed re…

    d46f37e
  6. ericvsmith commented on Oct 6, 2022

    @ericvsmith
    ericvsmith
    on Oct 6, 2022
    Member
    More actions

    I agree that changing back to a borrowed reference to the current version and backporting that behavior it is correct. Many more people would read the documentation and assume it's correct than would read the source code and know that it's a new reference that they need to manage.

  7. larryhastings commented on Oct 6, 2022

    @larryhastings
    larryhastings
    on Oct 6, 2022
    ContributorAuthor
    More actions

    I didn't do a full forensic analysis, but I think what happened here is:

    • func_get_annotations (the getter/setter) returns a fresh reference.
    • func_get_annotations was changed to call this code that says "oh! if it's a tuple, turn it into a dict." this function returns a new reference.
    • PyFunction_GetAnnotations wasn't changed, it was returning the dict.
    • the bug fix was, "oh! just have it call the same function to turn it from a tuple into a dict." but this function returns a new reference, and PyFunction_GetAnnotations shouldn't return a new reference.
  8. pablogsal
    added a commit that references this issue on Oct 6, 2022

    gh-97943: PyFunction_GetAnnotations should return a borrowed referenc…

    6bfb0be
  9. miss-islington
    added 4 commits that reference this issue on Oct 6, 2022

    pythongh-97943: PyFunction_GetAnnotations should return a borrowed re…

    dd4bbfb

    pythongh-97943: PyFunction_GetAnnotations should return a borrowed re…

    e9126fd

    pythongh-97943: PyFunction_GetAnnotations should return a borrowed re…

    787cc60

    gh-97943: PyFunction_GetAnnotations should return a borrowed referenc…

    33cf0a6
  10. carljm
    added a commit that references this issue on Oct 6, 2022

    Merge branch 'main' into dictwatch

    e5d2169
  11. ambv
    added a commit that references this issue on Oct 7, 2022

    [3.10] gh-97943: PyFunction_GetAnnotations should return a borrowed r…

    e0e303a
  12. carljm
    added a commit that references this issue on Oct 8, 2022

    Merge branch 'main' into typewatch

    415ed49
  13. mpage
    added a commit that references this issue on Oct 11, 2022

    pythongh-97943: PyFunction_GetAnnotations should return a borrowed re…

    149ed7f
  14. hauntsaninja commented on Oct 28, 2022

    @hauntsaninja
    hauntsaninja
    on Oct 28, 2022
    Contributor
    More actions

    Thanks for spotting, looks like it's been fixed and backported

  15. hauntsaninja
    closed this as completedon Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    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 01402503 — 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.