Skip to content

BUG: avoid thread-unsafe refcount check in temp elision#29649

Merged
charris merged 1 commit into
numpy:mainfrom
ngoldbaum:fix-unique-reference
Sep 1, 2025
Merged

BUG: avoid thread-unsafe refcount check in temp elision#29649
charris merged 1 commit into
numpy:mainfrom
ngoldbaum:fix-unique-reference

Conversation

@ngoldbaum
Copy link
Copy Markdown
Member

Checking Py_REFCNT == 1 is thread-unsafe on the free-threaded build, since refcounts are stored and loaded in free-threading using atomic operations.

Python 3.14 added PyUnstable_Object_IsUniquelyReferenced to replace static refcount checks. I added a backport to pythoncapi-compat in python/pythoncapi-compat#149, which allows me to use the function unconditionally and get correct behavior on 3.13t as well. This relies on some private implementation details on 3.13t, but all that is done in the compat header.

@ngoldbaum ngoldbaum added 00 - Bug 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703) 09 - Backport-Candidate PRs tagged should be backported labels Sep 1, 2025
@charris charris merged commit 4e05a13 into numpy:main Sep 1, 2025
77 checks passed
@charris
Copy link
Copy Markdown
Member

charris commented Sep 1, 2025

Thanks Nathan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants