-
Notifications
You must be signed in to change notification settings - Fork 805
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
Use a critical section to serialize adding builtins to globals #4921
Conversation
I ran |
Works perfectly in our setup as well and looks good to me. Thank you! |
src/marker.rs
Outdated
// - https://github.com/python/cpython/pull/24564 (the same fix in CPython 3.10) | ||
// - https://github.com/PyO3/pyo3/issues/3370 | ||
let builtins_s = crate::intern!(self, "__builtins__").as_ptr(); | ||
let has_builtins = unsafe { ffi::PyDict_Contains(globals.as_ptr(), builtins_s) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're at it, perhaps let's just use safe APIs here?
let has_builtins = unsafe { ffi::PyDict_Contains(globals.as_ptr(), builtins_s) }; | |
let has_builtins = globals.contains(builtins)?; |
There was once a slight perf advantage where the raw FFI would have avoided a useless reference count incref / decref on the key, but since IntoPyObject
we can now avoid that cost completely. So the safe API should be zero cost and shorter, safer code.
... and similar below.
…4921) * add test that panics because __builtins__ isn't available * use a critical section to serialize adding __builtins__ to __globals__ * add release note * use safe APIs instead of PyDict_Contains
* add test that panics because __builtins__ isn't available * use a critical section to serialize adding __builtins__ to __globals__ * add release note * use safe APIs instead of PyDict_Contains
Fixes #4913
Because
PyDict
is a concurrent hash table, it's safe to first check if the dict has a key without holding a critical section then acquire the critical section and fill the dict.Also adds a test based on the reproducer that I verified fails on
main
.