-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GILProtected possibly allowing concurrent access #218
Comments
Hi, the GIL would protect you from concurrency between Python calls, not from concurrency arising from inner Rust code. Since you're spawning tokio threads, it looks to me like an indication you might fall in the latter case. This is seen from a distance, I suppose we'd need to see the actual code to help you further. |
The part in the parentheses is not a valid conclusion. Every function taking a Yes, there can be multiple threads concurrently having a reference to the interior of the |
Note that this applies not just to |
Interesting. Thank you for the explanation... it makes sense, although I think that I thought that all of the calls occurring under the critical section were "simple enough" (
I wonder how |
Any call into the Python interpreter could do one of the following:
Python doesn't have the concept of unique ownership, so it's quite tricky to ensure a Whenever possible, try to avoid calls into the Python interpreter while having borrowed a If you can't do this consistently for all uses of a |
Thank you for the explanation. I think that given what you've said, it is impossible to use Hypothetically, you could use I had initially used
A "static" |
If you need to implement an extension module with shared state that can safely be used from multiple threads, you somehow need to deal with these issues. One option is to avoid calling back into the Python interpreter while having the Basically, as usual, the GIL only protects the C/Rust-level state, any higher-level state (that can be interacted with from Python) must use separate synchronization. You could do that with a Python
|
I believe that I have observed
GILProtected
(and thus, surprisingly, the GIL) allowing concurrent access to aRefCell
that it is protecting, thus causing a panic.To investigate, I added a small wrapper around the
RefCell
borrows, and observed:What I believe that this is showing is that while
ThreadId(3)
[1] is holding theGIL
with the acquisition id81f2b10deb81ce84
, and before it has dropped theRefCell
(and thus definitely before it has dropped theGIL
), an attempt made by a different thread (ThreadId(5)
) to acquire the sameRefCell
(acquisitiona09bb2c19d0b0962
) fails. I do not know how to explain the further panics after the first one though.This code is using
cpython = 0.5
with theextension-module
feature.Thank you for creating this library. I've thoroughly enjoyed porting this CFFI code to use it, and this is (hopefully) the last issue to track down before committing the port.
[1] these are all tokio runtime threads, but am not sure that that is relevant
The text was updated successfully, but these errors were encountered: