Skip to content
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

Merged
merged 4 commits into from
Feb 19, 2025

Conversation

ngoldbaum
Copy link
Contributor

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.

@ngoldbaum
Copy link
Contributor Author

I ran cargo stress repeatedly for ~10 times to try to trigger any flaky tests due to thread safety issues. I didn't see any failures.

@nertpinx
Copy link

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) };
Copy link
Member

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?

Suggested change
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.

@ngoldbaum ngoldbaum mentioned this pull request Feb 19, 2025
7 tasks
@davidhewitt davidhewitt added this pull request to the merge queue Feb 19, 2025
Merged via the queue into PyO3:main with commit f2f30f3 Feb 19, 2025
47 of 48 checks passed
mgorny pushed a commit to mgorny/pyo3 that referenced this pull request Feb 19, 2025
…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
davidhewitt pushed a commit that referenced this pull request Feb 20, 2025
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error: KeyError: '__builtins__' with globals when importing from a callback on Python >= 3.10
3 participants