Skip to content

Add critical section guards to accessors#2215

Open
seberg wants to merge 2 commits into
NVIDIA:mainfrom
seberg:ft-fixes-criticial-sections
Open

Add critical section guards to accessors#2215
seberg wants to merge 2 commits into
NVIDIA:mainfrom
seberg:ft-fixes-criticial-sections

Conversation

@seberg

@seberg seberg commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Protect accessors with cython critical sections so free-threaded execution avoids races when reading cached state.

@cython.critical_section effectively takes a lock based on the first argument passed in. (Although that lock can be released to avoid deadlocks, it is as safe or safer as the GIL, but that doesn't make it 100% in all situations.)

At least one of these was noticed by free-threaded test runs. I am not certain that they will guarantee identity returns, but pretty sure this will make them thread-safe.
(The reason being that at least on non free-threaded builds they may release the GIL or, on free-threaded, do API calls that release the critical section temporarily.)

About the .pyi fixes: I think these are incorrect but cython is already used in other places and shouldn't be a runtime/typing requirement at all.

Towards gh-2194 which will add some testing although not strictly guaranteed for all paths covered here.

Protect accessors with cython critical sections so free-threaded execution
avoids races when reading cached state.

At least one of these was noticed by free-threaded test runs.  I am not
certain that they will guarantee identity returns, but pretty sure this
will make them thread-safe.
(The reason being that at least on non free-threaded builds they may
release the GIL or, on free-threaded, do API calls that release the
critical section temporarily.)

About the `.pyi` fixes: I think these are incorrect but `cython`
is already used in other places and shouldn't be a runtime/typing requirement
at all.
@copy-pr-bot

copy-pr-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Jun 15, 2026
@seberg seberg added the bug Something isn't working label Jun 15, 2026
@seberg seberg self-assigned this Jun 15, 2026
@seberg seberg added this to the cuda.core v1.1.0 milestone Jun 15, 2026
@seberg seberg added the P0 High priority - Must do! label Jun 15, 2026
@seberg

seberg commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 652a3e7

@github-actions

Copy link
Copy Markdown

@mdboom

mdboom commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

About the .pyi fixes: I think these are incorrect but cython is already used in other places and shouldn't be a runtime/typing requirement at all.

We probably need to submit a fix to stubgen-pyx to not do this. I think it's probably just being "literal" about what's there. I don't think it "breaks" our .pyi files, just unnecessary.

There are obviously a lot more properties/accessors in the codebase. Besides seeing multi-threaded test failures, are there other criteria we should follow about when to apply these decorators?

@seberg

seberg commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

We probably need to submit a fix to stubgen-pyx to not do this. I think it's probably just being "literal" about what's

Yeah, right now it seems you need cython and will just get Any (because Cython isn't fully typed I think). A simple "ignore these" feature would be good.

There are obviously a lot more properties/accessors in the codebase.

Whenever we mutate a cdef object (global or class instance) then there can be a race that breaks the INCREF/DECREF balancing.
That is even if we do not care about calls being strictly idempotent (once cached a cache value can't be replaced). And that incref/decref balancing issue is strictly a free-threading issue.

I would hope that Cython will provide things to make this easier in the future (maybe atomic object).

You are not wrong, in that I should make another pass and maybe TSAN testing could flush out some as well. In practice hitting these issues is very unlikely but if you do...

Comment thread cuda_core/cuda/core/_memory/_buffer.pyx Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cuda.core Everything related to the cuda.core module P0 High priority - Must do!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants