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
Fix segfault with error report in FITS compression #15489
Conversation
ffpmsg requires the GIL.
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
PyErr_SetString(CfitsioException, err_message); | ||
PyGILState_Release(gstate); |
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.
Is this assuming that we are always releasing the GIL when calling out to cfitsio? If for some reason that were to change would it explode?
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.
void PyGILState_Release(PyGILState_STATE)
Part of the Stable ABI.
Release any resources previously acquired. After this call, Python’s state will be the same as it was prior to the corresponding PyGILState_Ensure() call (but generally this state will be unknown to the caller, hence the use of the GILState API).
So should be fine ?
https://docs.python.org/3/c-api/init.html#c.PyGILState_Release
But this will not work with subinterpreters (https://docs.python.org/3/c-api/init.html#non-python-created-threads).
The other option would be to stop releasing the GIL before calling cfitsio, not sure what the impact is ?
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.
Not releasing the GIL would mean no longer being able to do multithreaded decompression of tiles which does work at the moment.
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.
So I guess the fix here is fine for now, but we may have to reconsider when subinterpreters become a thing (planned for 3.13) ?
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.
If you need to call Python code from these threads (often this will be part of a callback API provided by the aforementioned third-party library), you must first register these threads with the interpreter by creating a thread state data structure, then acquiring the GIL, and finally storing their thread state pointer, before you can start using the Python/C API. When you are done, you should reset the thread state pointer, release the GIL, and finally free the thread state data structure.
Nope, I am out lol too much C 🤣
Don't suppose you have any bright ideas on how we could test this? I am pretty sure I ran screaming from the idea of testing the error stuff when I originally wrote this. |
Not really, I guess we could create a file that would trigger the warnings/errors but not sure how. |
I cannot tell if more fixes are needed or not based on Simon's comment in that issue, so I'll let @saimn decide when to merge. Thanks, all! |
I think I have an idea for a regression test - please don't merge yet so I can double check. |
I have come up with a regression test that causes the segfault and have pushed it to this PR. I have also temporarily reverted @saimn's change so that we can double check that a segfault does indeed happen in the CI. |
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.
This looks good to me now, but @saimn please check the test I added!
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.
Magical!
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.
🧙
Excellent, thanks @astrofrog ! |
This comment was marked as resolved.
This comment was marked as resolved.
Backport PR #15489 on branch v5.3.x (Fix segfault with error report in FITS compression)
ffpmsg requires the GIL.
Fix #15482
Description
This pull request is to address ...
Fixes #