-
Notifications
You must be signed in to change notification settings - Fork 803
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
flaky tests when importing module #3262
Comments
Could you expand how you came to the conclusion that
The pool is used to track deferred reference count increments/decrements, but I do not understand yet how it would affect module look-up? |
So I'm not sure this is really what happens and it seems more to be about object lookup (!= module lookup) If I understand correctly, here is what could happen:
|
"popping form the release pool" could only result in using an object that was already freed due to its reference count reaching zero. But this does not seem to happen in your case where all objects are valid, you just do not end up with the object you expect. I think the most likely culprit here is that both |
isn't freeing / using the wrong object possible?
Just to clarify, I also have the issue without importing time (and by replacing it with a loop so one test ends before the other) |
Since #887 was released in v0.10 the release pools are now unique to each thread, so I'm highly skeptical that there's a bug here caused by a pool interaction. Both tests are wrapped in |
yes I'm not able to reproduce the issue without the sys.modules part, I hadn't realized sys.module it could introduce race conditions. In my specific case, I still need to make the module available to imports so I guess I could just add some static mutex as a work around before running the python code |
If you want to tie in lazy initialization with GIL-based synchronization, the |
Thanks, this was helpful. I'm just surprised that the GIL can be released while running the code. |
Indeed, as PyO3 developers we all had this experience. e.g. https://pyo3.rs/v0.19.0/faq#im-experiencing-deadlocks-using-pyo3-with-lazy_static-or-once_cell |
Bug Description
In some scenario, when running
cargo test
, objects from another thread seem to get popped from the release pool.Here is an example:
test_race1
, we add a new moduleapi
in scope with an object and a method that returns1
test_race2
, we add a new moduleapi
in scope with an object and a method that returns2
Now when we run the following python code we don't always get the expected object and the assertion sometimes breaks.
In
test_race2
, the objectapi.obj
will refer to the object intest_race1
and return 1 instead.You'll notice a timeout, this is because it makes it easier to trigger this bug, basically having one test finish before the other.
I tried to find some similar issues and it seems like we previously had some mutex in some tests: dfbe22b and that got fixed
Finally, playing with the locals and globals in
py.run
seems to solve this but trigger other issues (like missing classes) that I wanted to keep this example simple.Steps to Reproduce
If you run the example in the description a few times (in my case it takes 2 runs max):
Backtrace
No response
Your operating system and version
linux
Your Python version (
python --version
)python 3.11.3
Your Rust version (
rustc --version
)rustc 1.68.2
Your PyO3 version
0.19
How did you install python? Did you use a virtualenv?
pyenv
Additional Info
No response
The text was updated successfully, but these errors were encountered: