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-35961: Fix a crash in slice_richcompare() #11830

Merged
merged 2 commits into from Feb 13, 2019

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Feb 12, 2019

Fix a crash in slice_richcompare(): use strong references rather than
borrowed references for the two temporary internal tuples.

The crash (or assertion error) occurred if a garbage collection
occurred during slice_richcompare(), especially while calling
PyObject_RichCompare(t1, t2, op).

https://bugs.python.org/issue35961

Fix a crash in slice_richcompare(): use strong references rather than
borrowed references for the two temporary internal tuples.

The crash (or assertion error) occurred if a garbage collection
occurred during slice_richcompare(), especially while calling
PyObject_RichCompare(t1, t2, op).
pitrou
pitrou approved these changes Feb 12, 2019
Copy link
Member

@pitrou pitrou left a comment

+1 from me. Much cleaner than the other PR.

Copy link
Member

@tim-one tim-one left a comment

I prefer this approach: briefer, simpler, clearer code than playing GC tricks.

@@ -0,0 +1,2 @@
Fix a crash in slice_richcompare(): use strong references rather than stolen
Copy link
Member

@methane methane Feb 13, 2019

Choose a reason for hiding this comment

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

s/stolen/borrowed/

Copy link
Member Author

@vstinner vstinner Feb 13, 2019

Choose a reason for hiding this comment

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

Well, @pablogsal and @tim-one said the opposite... I will stay with stolen :-)

Copy link
Member

@methane methane Feb 13, 2019

Choose a reason for hiding this comment

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

Oh, I'm sorry about it. I'm concur with you (#11830 (comment)).
"Owner" has responsibility to "DECREF". In this case, tuples don't DECREF range members.

But it is not important enough to continue discussion.

Copy link
Member

@tim-one tim-one Feb 13, 2019

Choose a reason for hiding this comment

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

@methane , please read the docs I already linked to. After PyTuple_SET_ITEM(), it is the tuple's responsibility to decref the item it grabbed. And it would indeed do so, if it were not for the bizarre-looking code at the end to force all the items to NULL without touching their refcounts.. As the docs say, the tuple stole the references from the slice object, and at that point - which is the point at which gc blows up - it was the slice object that was using "borrowed" references. As the docs say, PyTuple_SET_ITEM() transfers ownership.

Which is the fundamental bug here: two objects "believe" they own a reference, but the refcount only accounts for one of them. It's more-or-less arbitrary to pick which one "really" owns it and which "really" borrows it, but the docs are 100% clear about that PyTuple_SET_ITEM() made the tuple the owner at the time gc blows up.

Copy link
Member

@methane methane Feb 14, 2019

Choose a reason for hiding this comment

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

See https://docs.python.org/3/c-api/intro.html#reference-count-details

"“Owning a reference” means being responsible for calling Py_DECREF on it when the reference is no longer needed."

PyTuple_SET_ITEM() is defined as stole reference because the reference will be Py_DECREFed by tuple.
But in this case, tuple never call Py_DECREF for the reference. It will be overwritten soon.

Tuple "believes" it stoled (owns) the reference, but it is "borrowed" reference actually. If tuple really stoled the reference, there were no problem.

Copy link
Member

@methane methane Feb 14, 2019

Choose a reason for hiding this comment

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

In other words, "Use strong reference rather than stolen reference" doesn't make sense, because
"stolen reference" is "strong reference".
It was "borrowed reference which tuple believed stolen (strong) reference".

@methane
Copy link
Member

methane commented Feb 13, 2019

I like this too.
If this is performance critical, we can avoid temporal tuple.
But it seems performance is not important here.

@vstinner
Copy link
Member Author

vstinner commented Feb 13, 2019

AppVeyor failed because of a random failure (ENV_CHANGED): test_multiprocessing_spawn and test_threading. I rescheduled the job.

@vstinner
Copy link
Member Author

vstinner commented Feb 13, 2019

But it seems performance is not important here.

Right, https://bugs.python.org/issue35961#msg335347

@vstinner vstinner merged commit dcb68f4 into python:master Feb 13, 2019
@vstinner vstinner deleted the slice_richcompare2 branch Feb 13, 2019
@miss-islington
Copy link
Contributor

miss-islington commented Feb 13, 2019

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒🤖

@bedevere-bot
Copy link

bedevere-bot commented Feb 13, 2019

GH-11839 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 13, 2019
Fix a crash in slice_richcompare(): use strong references rather than
stolen references for the two temporary internal tuples.

The crash (or assertion error) occurred if a garbage collection
occurred during slice_richcompare(), especially while calling
PyObject_RichCompare(t1, t2, op).
(cherry picked from commit dcb68f4)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
miss-islington added a commit that referenced this pull request Feb 13, 2019
Fix a crash in slice_richcompare(): use strong references rather than
stolen references for the two temporary internal tuples.

The crash (or assertion error) occurred if a garbage collection
occurred during slice_richcompare(), especially while calling
PyObject_RichCompare(t1, t2, op).
(cherry picked from commit dcb68f4)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants