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

Audit Py<T> methods for GIL safety #949

Closed
davidhewitt opened this issue Jun 2, 2020 · 1 comment · Fixed by #970
Closed

Audit Py<T> methods for GIL safety #949

davidhewitt opened this issue Jun 2, 2020 · 1 comment · Fixed by #970

Comments

@davidhewitt
Copy link
Member

As part of the invariant that Py<T>'s data can only be accessed when the GIL is locked, all the member functions should either acquire the GIL or take a GIL token as an argument. Py::get_refcnt and several of the unsafe functions doesn't appear to lock the GIL internally.

Note that it is Undefined Behavior to read the reference count using a plain memory read (what ffi::Py_REFCNT does) while a different thread is concurrently modifying it, making get_refcnt have safety issues.

Originally posted by @programmerjake in #948 (comment)

Proposed actions to resolve this:

  • Add Python token as an argument to all safe function that require the GIL.
  • Add # Safety documentation to all unsafe functions that require the GIL. (Or alternatively add Python token argument to them too.)
@davidhewitt davidhewitt changed the title As part of the invariant that Py<T>'s data can only be accessed when the GIL is locked, all the member functions should either acquire the GIL or take a GIL token as an argument. [Py::get_refcnt](https://pyo3.rs/master/doc/pyo3/struct.Py.html#method.get_refcnt) and several of the unsafe functions doesn't appear to lock the GIL internally. Audit Py<T> methods for GIL safety Jun 2, 2020
@programmerjake
Copy link
Contributor

There are similar issues for PyObject::get_refcnt and probably other member functions.

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 a pull request may close this issue.

2 participants