Visitar URL original
bpo-44263: Py_TPFLAGS_HAVE_GC requires tp_traverse by vstinner · Pull Request #26463 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20210602053242/https://github.com/python/cpython/pull/26463
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-44263: Py_TPFLAGS_HAVE_GC requires tp_traverse #26463

Merged
merged 1 commit into from Jun 1, 2021

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented May 31, 2021 •

The PyType_Ready() function now raises an error if a type is defined
with the Py_TPFLAGS_HAVE_GC flag set but has no traverse function
(PyTypeObject.tp_traverse).

https://bugs.python.org/issue44263

@vstinner
Copy link
Member Author

@vstinner vstinner commented May 31, 2021

This change breaks C extensions which define types with Py_TPFLAGS_HAVE_GC but without tp_traverse, even if instances of the type are never tracked by the GC. Such extensions worked properly previously.

Maybe the check could be restricted to heap types?

If we don't want to break the backward compatibility, we should check in PyObject_GC_Track() if type->tp_traverse is not NULL.

Many instances are created by PyType_GenericAlloc() which calls _PyObject_GC_TRACK(). I tried to modify _PyObject_GC_TRACK() to ensure that it's possible to traverse the instance, as I did in PyObject_GC_Track(), but I got a lot of issues. Many types call _PyObject_GC_TRACK() too early, and it's really difficult to change that (it would require a lot of work).

cc @pablogsal @erlend-aasland @corona10

@vstinner
Copy link
Member Author

@vstinner vstinner commented May 31, 2021

As I explained at https://bugs.python.org/issue44263#msg394799, this PR cannot be merged yet since _testcapi and _decimal don't respect the GC protocol. I chose to create a separated PR to fix them, so it can be backported (and review should be easier): #26464

@vstinner
Copy link
Member Author

@vstinner vstinner commented May 31, 2021

Many instances are created by PyType_GenericAlloc() which calls _PyObject_GC_TRACK(). I tried to modify _PyObject_GC_TRACK() to ensure that it's possible to traverse the instance, as I did in PyObject_GC_Track(), but I got a lot of issues. Many types call _PyObject_GC_TRACK() too early, and it's really difficult to change that (it would require a lot of work).

My latest attempt in 2020: https://bugs.python.org/issue40142

Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

LGTM. I leave it for Pablo and Dong-hee to decide if this should be restricted to heap types only.

(Contributed by Dong-hee Na and Serhiy Storchaka in :issue:`44235`.)

This comment has been minimized.

@erlend-aasland

erlend-aasland May 31, 2021
Contributor

Why this change? :)

This comment has been minimized.

@vstinner

vstinner May 31, 2021
Author Member

Ask my text editor.

This comment has been minimized.

@vstinner

vstinner May 31, 2021
Author Member

My text editor (vim) likes to add newline at the end of a file.

@pablogsal
Copy link
Member

@pablogsal pablogsal commented May 31, 2021

@vstinner is the issue number correct?

The PyType_Ready() function now raises an error if a type is defined
with the Py_TPFLAGS_HAVE_GC flag set but has no traverse function
(PyTypeObject.tp_traverse).
@vstinner
Copy link
Member Author

@vstinner vstinner commented May 31, 2021

@vstinner is the issue number correct?

I consider that it's to ok reuse https://bugs.python.org/issue44263 My change implements a runtime check for 8b55bc3 "... it must implement the GC protocol itself by at least including a slot".

@vstinner vstinner force-pushed the vstinner:gc_type_traverse branch from ae21002 to ce8117e May 31, 2021
@vstinner
Copy link
Member Author

@vstinner vstinner commented May 31, 2021

PR rebased on top on the merged PR #26464 fix.

@pablogsal
Copy link
Member

@pablogsal pablogsal commented May 31, 2021

I consider that it's to ok reuse https://bugs.python.org/issue44263

🤷‍♂️ I don't know about that, https://bugs.python.org/issue44263 is a bout a documentation change and this is a breaking change in the C-API, which probably requires its own issue. But I don't feel strongly so you don't need to change anything. I just want to note that I find it a bit odd.

Copy link
Member

@corona10 corona10 left a comment

lgtm

@vstinner
Copy link
Member Author

@vstinner vstinner commented May 31, 2021

For the risk of breaking C extensions with this PR: I documented the change in What's New in Python 3.11. It's the bare minimum change that I can do.

@pablogsal: Apart of the bpo number, are you ok with this change?

A GC type with NULL traverse function is likely to crash, but not always, as we saw with the broken _ssl.SSLError exception ;-) This change makes the code more deterministic, it always fail (but don't crash) ;-)

Copy link
Member

@shihai1991 shihai1991 left a comment

Nice improvement.

@shihai1991
Copy link
Member

@shihai1991 shihai1991 commented Jun 1, 2021

For the risk of breaking C extensions with this PR: I documented the change in What's New in Python 3.11. It's the bare minimum change that I can do.

Hm. Ptr's pep-652 have redefined the lifetime of stable ABI, so it's not a big probleam, right?
refs: https://www.python.org/dev/peps/pep-0652/#stable-abi

@pablogsal
Copy link
Member

@pablogsal pablogsal commented Jun 1, 2021

Hm. Ptr's pep-652 have redefined the lifetime of stable ABI, so it's not a big probleam, right?

This doesn't change the ABI, it just makes a bug break early :)

@shihai1991
Copy link
Member

@shihai1991 shihai1991 commented Jun 1, 2021

This doesn't change the ABI, it just makes a bug break early :)

OK. It's make sense :)

@vstinner vstinner merged commit ee76375 into python:main Jun 1, 2021
12 checks passed
12 checks passed
@github-actions
Docs
Details
@github-actions
Check for source changes
Details
@github-actions
Check if generated files are up to date
Details
@github-actions
Windows (x86)
Details
@github-actions
Windows (x64)
Details
@github-actions
macOS
Details
@github-actions
Ubuntu
Details
@github-actions
Ubuntu SSL tests with OpenSSL
Details
Azure Pipelines PR #20210531.15 succeeded
Details
@travis-ci
Travis CI - Pull Request Build Passed
Details
@bedevere-bot
bedevere/issue-number Issue number 44263 found
Details
@bedevere-bot
bedevere/news News entry found in Misc/NEWS.d
@vstinner vstinner deleted the vstinner:gc_type_traverse branch Jun 1, 2021
@vstinner
Copy link
Member Author

@vstinner vstinner commented Jun 1, 2021

Ok, I merged my PR. If it breaks too many C extensions, we can revert it. I hope that it will detect more real bugs than it breaks C extensions for no good reasons :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants