Skip to content

MNT: Add linter for thread-unsafe C API uses #26159

Closed

Description

@ngoldbaum

As noted in PEP 703 C API functions that return borrowed references are problematic in the nogil build.

We need to audit our usages and replace any that upgrade a borrowed reference to an owned reference with alternate C API functions that return new references.

Problematic Functions:

Activity

  1. added
    39 - free-threadingPRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)
    on Apr 10, 2024
  2. arunppsg commented on Jun 16, 2024

    @arunppsg
    Contributor

    @ngoldbaum Can I work on replacing usage of PyList_GET_ITEM? I intend to replace it with PyList_GetItemRef as it returns a strong reference. Is there anything else I should look for?

  3. ngoldbaum commented on Jun 16, 2024

    @ngoldbaum
    MemberAuthor

    So, careful, we don’t want to wholesale replace API usages. Sometimes we intentionally create borrowed references.

    Any usages where in the same region of code we first create a borrowed reference and then soon after upgrade it to an owned reference by calling Py_INCREF can be replaced by GetItemRef. Replacing those usages (and deleting the subsequent INCREF) shouldn’t introduce new reference cou ting bugs unless we’re only doing an INCREF conditionally.

    Basically, be careful, because reference counting bugs can be hard to track down and spot, and we don’t want to introduce any new ones in a refactor.

  4. added
    sprintableIssue fits the time-frame and setting of a sprint
    on Oct 24, 2024
  5. changed the title [-]MNT: Replace C API usages that return borrowed references[/-] [+]MNT: Add linter for thread-unsafe C API uses[/+] on Oct 24, 2024
  6. ngoldbaum commented on Oct 24, 2024

    @ngoldbaum
    MemberAuthor

    I think the remaining issue on this task is to add a linter to NumPy's CI to check PRs for new uses of possibly problematic C API items. We should double-check and explicitly whitelist all the remaining ones, then make any new uses get whitelisted as they come in.

    I added the sprintable label to this since this should be doable for anyone who understands how to work with github actions and parse git diffs.

  7. removed their assignment
    on Oct 24, 2024
  8. lvllvl commented on Apr 3, 2025

    @lvllvl
    Contributor

    Hi @ngoldbaum, do you mind checking out my PR when you get a chance? I was trying to add a workflow linter for the problematic functions.

    Here's a link to the PR: #28634

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

    03 - Maintenance39 - free-threadingPRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)sprintableIssue fits the time-frame and setting of a sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions