-
Notifications
You must be signed in to change notification settings - Fork 761
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
gil: tidy ups to finalization #1355
Conversation
1b6f310
to
8ab9bb5
Compare
Hmm, the coverage job seems to have some kind of deadlock... |
559125b
to
cbfb8c4
Compare
@kngwyu this PR is now ready for review - would be interested to know what you think of it. |
If we really need this trick I don't want to use |
I think it's also good to provide |
The problem is in tests there is no main thread, so we will always need some kind of trick like this for tests.
|
Then how about memorizing the thread id and calling finalize if the current thread == initialized thread? Also, I think we need to rethink the use cases of The document notes some examples:
Our original motivation (clearing the buffer) does not match any of these, and I'm kind of doubtful about the idea that it must be called. |
I think I don't want to do anything like memorize the thread id, as then it's unpredictable in tests whether finalize is called, which doesn't seem good for reproducability. Also I'm now thinking that because we know lots of crashes can occur at finalization it's better not to have it as a default. I pushed a new commit which removes We could implement |
faae995
to
5cb7e3c
Compare
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.
Thanks!
This is a breaking change, but I believe it would be applicable.
assert_eq!( | ||
ffi::Py_IsInitialized(), | ||
0, | ||
"called `with_embedded_python_interpreter` but a Python interpreter is already running." | ||
); |
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 we force some error type (e.g., PyErr
), we can return an Err
here. It's an optional choice, though.
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.
I think PyErr
doesn't work here because then the user will have a PyErr
outside of the scope of a Python interpreter!
The hazards of unsafe
😅
94b8224
to
bc6a37a
Compare
with_embedded_python_interpreter
bc6a37a
to
dc7bcda
Compare
@kngwyu this PR is ready for another look. I'm quite happy with it now, would be great to hear if you think there should be any further changes. |
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.
Thanks, LGTM 💯
EDIT: I changed strategy. See #1355 (comment) for the latest design.
This is a follow-up to #1347 which adds some tidy-ups to our finalization behavior:
Calling
Py_Finalize
in a different thread toPy_Initialize
will cause anAssertionError
. (See https://bugs.python.org/issue26693)So I changedprepare_freethreaded_python
to initialize and finalize in a dedicated thread.We have some C-extensions which have crashes with
Py_Finalize
.So after disabling theauto-initialize
feature we can ask users with problematic extensions to call the newprepare_freethreaded_python_without_finalizer
to workaround these. I added this to the FAQ.Closes #1073
Closes #1261