Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-40217: Ensure Py_VISIT(Py_TYPE(self)) is always called for PyType_FromSpec types #19414

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

Merged
merged 1 commit into from
Apr 27, 2020

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented Apr 7, 2020

https://bugs.python.org/issue40217

Types created by PyType_FromSpec own a strong reference to their type, but this was added in Python 3.8. The tp_traverse function needs to call Py_VISIT on the type but all existing traverse functions cannot be updated (especially the ones from existing user functions) so we need to provide a tp_traverse that manually calls Py_VISIT(Py_TYPE(self)) and then call the provided tp_traverse. In this way, user functions do not need to be updated, preserve backwards compatibility.

To do this, we store the user-provided traverse function at the end of the type (we need to allocate space for it) so we can call it from our PyType_FromSpec_tp_traverse wrapper.

@pablogsal pablogsal changed the title bpo-40217: Ensure Py_VISIT(Py_TYPE(self)) is always called for PyType… bpo-40217: Ensure Py_VISIT(Py_TYPE(self)) is always called for PyType_FromSpec types Apr 7, 2020
@pablogsal pablogsal force-pushed the bpo-40217 branch 4 times, most recently from ea96f0b to 88f807c Compare April 7, 2020 20:25
@pablogsal
Copy link
Member Author

Before this PR:

❯ ./python -m test -R 3:3 test_threading -m test.test_threading.SubinterpThreadingTests.test_threads_join_2
0:00:00 load avg: 2.55 Run tests sequentially                                                  
0:00:00 load avg: 2.55 [1/1] test_threading                                                    
beginning 6 repetitions                                                                        
123456                                                                                         
......                                                                                                                                                                                                                                                                                                                                                                                         test_threading leaked [19, 19, 19] references, sum=57                                                                                                                                                                                                                                                                                                                                          test_threading leaked [12, 12, 12] memory blocks, sum=36                                                                                                                                                                                                                                                                                                                                       test_threading failed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         == Tests result: FAILURE ==                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   1 test failed:                                                                                                                                                                                                                                                                                                                                                                                 
    test_threading                                                                                                                                                                             
                                                                                                                                                                                               
Total duration: 385 ms                                                                         
Tests result: FAILURE 

With this PR:

❯ ./python -m test -R 3:3 test_threading -m test.test_threading.SubinterpThreadingTests.test_threads_join_2
0:00:00 load avg: 2.37 Run tests sequentially
0:00:00 load avg: 2.37 [1/1] test_threading
beginning 6 repetitions
123456
......

== Tests result: SUCCESS ==

1 test OK.

Total duration: 367 ms
Tests result: SUCCESS

@pablogsal pablogsal marked this pull request as ready for review April 7, 2020 21:27
@pablogsal pablogsal force-pushed the bpo-40217 branch 4 times, most recently from 353f682 to ab1ca32 Compare April 7, 2020 21:58
@pablogsal pablogsal added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 8, 2020
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @pablogsal for commit ab1ca32 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 8, 2020
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I have a few minor remarks.

if (obj == NULL)
return PyErr_NoMemory();

obj = obj;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the purpose of that?

/* note that we need to add one, for the sentinel and space for the
provided tp-traverse: See bpo-40217 for more details */

if (PyType_IS_GC(type))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 7: please add { ... }. Same remark for other if.

@@ -2985,6 +3047,26 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
memcpy(PyHeapType_GET_MEMBERS(res), slot->pfunc, len);
type->tp_members = PyHeapType_GET_MEMBERS(res);
}
else if (slot->slot == Py_tp_traverse) {

/* Types created by PyType_FromSpec own a strong reference to their
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put bpo-40217 somewhere in the comment.

@vstinner
Copy link
Member

@pablogsal: I added "skip news" to be able to merge your PR, but IMHO it's worth it to add a NEWS entry for it (I suggest the C API category). Would you mind to add one?

@pablogsal
Copy link
Member Author

Would you mind to add one?

Will do but I didn't had time to add your suggestions for this PR before you merged it. Should I add this in the future PR?

@vstinner
Copy link
Member

Will do but I didn't had time to add your suggestions for this PR before you merged it. Should I add this in the future PR?

Yeah.

pablogsal added a commit to pablogsal/cpython that referenced this pull request May 23, 2020
miss-islington pushed a commit that referenced this pull request May 27, 2020
…_FromSpec types (reverts GH-19414) (GH-20264)

Heap types now always visit the type in tp_traverse. See added docs for details.

This reverts commit 0169d30.

Automerge-Triggered-By: @encukou
miss-islington added a commit that referenced this pull request May 28, 2020
…_FromSpec types (reverts GH-19414) (GH-20264)

Heap types now always visit the type in tp_traverse. See added docs for details.

This reverts commit 0169d30.

Automerge-Triggered-By: @encukou
(cherry picked from commit 1cf15af)

Co-authored-by: Pablo Galindo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants