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

Make PyDateTime_IMPORT FFI wrapper thread-safe #4623

Merged
merged 3 commits into from
Oct 20, 2024

Conversation

ngoldbaum
Copy link
Contributor

Use a std::sync::once to ensure the UnsafeCell is only ever written to once. I don't think it's possible for setting the pointer to panic, so I don't think it makes a difference to use call_once vs call_once_force.

This is the only singleton like this in the FFI wrappers I can find by grepping.

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, this looks good to me. A couple of thoughts of adjustments / alternatives to briefly consider.


PyCapsule_Import(PyDateTime_CAPSULE_NAME.as_ptr(), 1) as *mut PyDateTime_CAPI
};

Copy link
Member

Choose a reason for hiding this comment

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

Can PyDateTime_Import ever fail? I guess if the return value is null then it might be unhelpful to continue to set the once. 🤔

Copy link
Contributor Author

@ngoldbaum ngoldbaum Oct 15, 2024

Choose a reason for hiding this comment

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

So, the docs for PyCapsule_Import imply that it might set an exeption and fail, but then again the definition of the PyDateTime_IMPORT macro doesn't do any error checking:
https://github.com/python/cpython/blob/12eaadc0ad33411bb02945d700b6ed7e758bb188/Include/datetime.h#L199-L200

so I think in practice it can't fail. Also, our PyDateTime_IMPORT FFI wrapper doesn't return anything (following CPython...), so we would need to change the API or tell people they need to call PyErr_Occurred after every import call, which would be kind of awful, especially for an issue that CPython itself doesn't seem to worry about.

I commented on an upstream issue about this: python/cpython#122184 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

I commented there, I think it can fail. I think it's good enough here to add

Suggested change
if py_datetime_c_api.is_null() {
return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, easy enough to add the error check

pyo3-ffi/src/datetime.rs Show resolved Hide resolved
@ngoldbaum ngoldbaum force-pushed the datetime-import-thread-safety branch from afb9c99 to 8e02786 Compare October 19, 2024 23:19
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.

LGTM, thanks 👍

@davidhewitt davidhewitt added this pull request to the merge queue Oct 20, 2024
Merged via the queue into PyO3:main with commit 7909eb6 Oct 20, 2024
42 of 43 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.

2 participants