| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
…m_self is kept alive long enough
|
And since this PR fixes a bug exposed in user code, add please a NEWS entry. |
Sorry, something went wrong.
…l, and add NEWS blurb
Not sure whether that mostly belongs in "C API" or "Core and Builtins", but I went with "C API" because I expected users are mostly affected when using the C API. If necessary, I can still move it. |
Sorry, something went wrong.
There was a problem hiding this comment.
Thank you! Minor nitpick about the NEWS entry.
Sorry, something went wrong.
There was a problem hiding this comment.
Great!
Sorry, something went wrong.
|
Thanks for the amazingly quick review, @serhiy-storchaka! Amazing to submit a PR like this :-) |
Sorry, something went wrong.
|
Thanks @YannickJadoul for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9. |
Sorry, something went wrong.
…m_self is kept alive long enough (pythonGH-22670) (cherry picked from commit 04b8631) Co-authored-by: Yannick Jadoul <yannick.jadoul@belgacom.net>
|
GH-22674 is a backport of this pull request to the 3.9 branch. |
Sorry, something went wrong.
|
I will be glad to work with you! |
Sorry, something went wrong.
…m_self is kept alive long enough (pythonGH-22670)
|
YannickJadoul:reorder-meth_dealloc-decref |
Sorry, something went wrong.
…m_self is kept alive long enough (pythonGH-22670)
| Back | FazBrowse Home | New Git URL |
In Python 3.9, the line Py_XDECREF(PyCFunction_GET_CLASS(m)); was added to meth_dealloc (in methodobject.c). Unfortunately for pybind11, it's inserted exactly two lines too low, since it accesses the PyMethodDef and we store the PyMethodDef instance in the capsule that's used as self-argument of the PyCFunction.
Result: UB, since Py_XDECREF(m->m_self); brings down the refcount of the capsule to 0 and (indirectly) frees the PyMethodDef, while its contents are now still accessed.
From the pybind11 perspective, it would be optimal if this could be fixed in CPython itself, by moving up this one Py_XDECREF 2 lines. This would a) be more efficient than creating a workaround, and b) allow old, existing versions of pybind11 to work with Python 3.9 (well, 3.9.1, then, hopefully); the user base of pybind11 has grown quite a bit and now includes giants like scipy or some Google libraries.
This PR reorders those lines.
If there's a different, recommended way of creating these PyCFunctionObjects dynamically and cleaning up the PyMethodDef, we'd be interested as well, to make sure these kinds of breakages are avoided in the future.
Apologies for only figuring out now how to debug this, using valgrind. Up until yesterday, we only saw some failures in CI on macOS, but it was hard to reproduce and debug locally.
https://bugs.python.org/issue42015