-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Avoid thread termination in scoped_released #2657
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
Changes from 1 commit
7e8a4cf
9e8bd73
c82145d
7b70787
461e81a
f5804e3
e5fe8d3
bb8cee6
93639b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2121,7 +2121,12 @@ class gil_scoped_acquire { | |
pybind11_fail("scoped_acquire::dec_ref(): internal error!"); | ||
#endif | ||
PyThreadState_Clear(tstate); | ||
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 6) && !defined(Py_LIMITED_API) | ||
if (!_Py_IsFinalizing()) | ||
PyThreadState_DeleteCurrent(); | ||
#else | ||
PyThreadState_DeleteCurrent(); | ||
#endif | ||
PYBIND11_TLS_DELETE_VALUE(detail::get_internals().tstate); | ||
release = false; | ||
} | ||
|
@@ -2153,7 +2158,14 @@ class gil_scoped_release { | |
~gil_scoped_release() { | ||
if (!tstate) | ||
return; | ||
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 6) && !defined(Py_LIMITED_API) | ||
// PyEval_RestoreThread() should not be called if runtime is finilizing | ||
// See https://docs.python.org/3/c-api/init.html#c.PyEval_RestoreThread | ||
if (!_Py_IsFinalizing()) | ||
PyEval_RestoreThread(tstate); | ||
#else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: typo: finilizing More significantly: I think the code could be made more readable and maintainable by centralizing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure that that's not just going to add more confusion, to have another indirection? As long as it's just these two (very related) spots, to me, this seem reasonably fine? (We currently have these kinds of things inline in other places as well, and this should make it easy to remove once we drop <= 3.6?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The practical version of DRY/SPOT that I've seen states that exactly one duplication is allowed, if it's clearly more complex to combine it. Trying to take DRY/SPOT to the extreme of truly one point of truth for all code can impose an unmanageable level of complexity, so allowing it to be relaxed by one at programmer discretion balances the ideal with practicality. (and if there's more than one duplication, the extra complexity is always worth it). In short, I'm fine either way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks Henry! I hadn't heard of DRY/SPOT before, good to know there is something to refer to. In this particular case I'm not so much concerned about the code duplication, but mostly about the comment only appearing in one place. For someone arriving at the second place from some completely different context, they will not even know the comment exists in the other place, and may miss it. But if they get curious about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment is true, though! I completely agree there. There should at least be a "see a few lines up/down, at ...". Apart from that, I don't feel too strongly, so up to @malfet AFAIC, but I just though the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the feedback. Fixed typo and moved and extended runtime finalization check to |
||
PyEval_RestoreThread(tstate); | ||
#endif | ||
if (disassoc) { | ||
auto key = detail::get_internals().tstate; | ||
PYBIND11_TLS_REPLACE_VALUE(key, tstate); | ||
|
Uh oh!
There was an error while loading. Please reload this page.