Skip to content

Conversation

tdyas
Copy link
Contributor

@tdyas tdyas commented Sep 11, 2025

Add a new pyo3::sync::RwLockExt trait for std::sync::RwLock to detach from the Python interpreter while blocking to acquire a read or write lock on a std::sync::RwLock. This mirrors what pyo3::sync::MutexExt and pyo3::sync::OnceLockExt do for std::sync::Mutex and std::sync::OnceLock respectively. The trait is also implemented for parking_lot's / lock_api's RwLock.

@tdyas tdyas force-pushed the rwlockext_trait branch 2 times, most recently from f9ae19c to e36ab53 Compare September 11, 2025 13:34
@tdyas tdyas marked this pull request as ready for review September 11, 2025 14:29
@tdyas
Copy link
Contributor Author

tdyas commented Sep 11, 2025

I plan to submit support for parking_lot::RwLock in a follow-on PR.

tdyas added a commit to pantsbuild/pants that referenced this pull request Sep 16, 2025
Upgrade the `pyo3` crate to v0.26.0. There are several migrations due to
deprecated naming [as per the migration
guide](https://pyo3.rs/main/migration.html#from-025-to-026):

- The `PyObject` type alias is removed. Switched to its expansion
`Py<PyAny>`.
- "GIL" is removed as a legacy name since the free-threaded Python in
Python 3.13+ has no GIL. Threads still "attach" to the interpreter
though so `Python::with_gil` becomes `Python::attach` and
`Python::allow_threads` becomes `Python::detach`.
- The `GILProtected` wrapper is removed in favor of just using normal
`Mutex` or `RwLock` locks. The various usages have been switched to
`Mutex` and `RwLock`. There is a very small chance of acquiring a lock
deadlocking with the GIL so PyO3 provides a helper `lock_py_attached`
method to detach, acquire the lock, and reattach. For the `RwLock` used
in `PyGeneratorResponseCall`, equivalent `read_py_attached` and
`write_py_attached` methods won't be available until my PR
PyO3/pyo3#5435 lands upstream.
-  `pyo3::prepare_freethreaded_python` becomes `Python::initialize`.
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this branch today and sorry for the very slow review.

Overall this looks great, and is I think good for merge without any question about the validity of the design.

Just a few minor nits which would be great to clean up before we merge and ship.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks 👍

@davidhewitt davidhewitt added this pull request to the merge queue Oct 11, 2025
@ngoldbaum
Copy link
Contributor

ngoldbaum commented Oct 11, 2025

Thanks so much for doing this! Sorry for missing this PR - if you have future PRs that are related to supporting or improving multithreaded workflows please feel free to ping my handle for speedier reviews.

Merged via the queue into PyO3:main with commit 99aa9bd Oct 11, 2025
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants