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

bpo-42015: Reorder dereferencing calls in meth_dealloc, to make sure m_self is kept alive long enough #22670

Merged

Conversation

@YannickJadoul
Copy link
Contributor

@YannickJadoul YannickJadoul commented Oct 12, 2020

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

…m_self is kept alive long enough
@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 12, 2020

And since this PR fixes a bug exposed in user code, add please a NEWS entry.

…l, and add NEWS blurb
@YannickJadoul
Copy link
Contributor Author

@YannickJadoul YannickJadoul commented Oct 12, 2020

And since this PR fixes a bug exposed in user code, add please a NEWS entry.

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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Thank you! Minor nitpick about the NEWS entry.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Great!

@YannickJadoul
Copy link
Contributor Author

@YannickJadoul YannickJadoul commented Oct 12, 2020

Thanks for the amazingly quick review, @serhiy-storchaka! Amazing to submit a PR like this :-)

@serhiy-storchaka serhiy-storchaka merged commit 04b8631 into python:master Oct 12, 2020
10 checks passed
10 checks passed
Docs
Details
Check for source changes
Details
Windows (x86)
Details
Windows (x64)
Details
macOS
Details
Ubuntu
Details
Azure Pipelines PR #20201012.48 succeeded
Details
Travis CI - Pull Request Build Passed
Details
bedevere/issue-number Issue number 42015 found
Details
bedevere/news News entry found in Misc/NEWS.d
@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Oct 12, 2020

Thanks @YannickJadoul for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒🤖

miss-islington added a commit to miss-islington/cpython that referenced this pull request Oct 12, 2020
…m_self is kept alive long enough (pythonGH-22670)

(cherry picked from commit 04b8631)

Co-authored-by: Yannick Jadoul <[email protected]>
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Oct 12, 2020

GH-22674 is a backport of this pull request to the 3.9 branch.

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 12, 2020

I will be glad to work with you!

@YannickJadoul YannickJadoul deleted the YannickJadoul:reorder-meth_dealloc-decref branch Oct 12, 2020
miss-islington added a commit that referenced this pull request Oct 12, 2020
…m_self is kept alive long enough (GH-22670)

(cherry picked from commit 04b8631)

Co-authored-by: Yannick Jadoul <[email protected]>
xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
erlend-aasland added a commit to erlend-aasland/cpython that referenced this pull request Oct 18, 2020
…m_self is kept alive long enough (pythonGH-22670)
lisroach added a commit to lisroach/cpython that referenced this pull request Oct 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.