Skip to content

PyFunction_GetAnnotations is probably causing ref leaks #97943

Description

@larryhastings

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 commented on Oct 5, 2022

    @larryhastings
    ContributorAuthor

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

    @pablogsal @methane

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

    @larryhastings
    ContributorAuthor

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

  4. added a commit that references this issue on Oct 6, 2022
  5. ericvsmith commented on Oct 6, 2022

    @ericvsmith
    Member

    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.

  6. larryhastings commented on Oct 6, 2022

    @larryhastings
    ContributorAuthor

    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.
  7. added a commit that references this issue on Oct 6, 2022
  8. added 4 commits that reference this issue on Oct 6, 2022
  9. added a commit that references this issue on Oct 6, 2022
  10. added a commit that references this issue on Oct 7, 2022
  11. added a commit that references this issue on Oct 8, 2022
  12. added a commit that references this issue on Oct 11, 2022
  13. hauntsaninja commented on Oct 28, 2022

    @hauntsaninja
    Contributor

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

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 error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions