Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
gh-140530: fix a reference leak in an error path forraise exc from cause#140908
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation
cec6a51 toa445dd3Comparesubtype for which `T.__new__` does not return an exception instance.
a445dd3 toadb1bc4Comparebedevere-bot commentedNov 2, 2025
🤖 New build scheduled with the buildbot fleet by@picnixz for commitadb1bc4 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F140908%2Fmerge If you want to schedule another build, you need to add the🔨 test-with-refleak-buildbots label again. |
| self.fail("No exception raised") | ||
| deftest_class_cause_nonexception_result(self): | ||
| classConstructsNone(BaseException): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think we should keep the old test and add the new test in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
The old test is actually testing the same code. The problem was that the returned value was immortal...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
FTR:
if (PyExceptionClass_Check(cause)) {fixed_cause=_PyObject_CallNoArgs(cause);if (fixed_cause==NULL)gotoraise_error;if (!PyExceptionInstance_Check(fixed_cause)) {_PyErr_Format(tstate,PyExc_TypeError,"calling %R should have returned an instance of ""BaseException, not %R",cause,Py_TYPE(fixed_cause));Py_DECREF(fixed_cause);gotoraise_error;}Py_DECREF(cause);}
Sincecause is an exception class, we called__new__ andfixed_cause would have beenNone. We would then move toPyExceptionInstance_Check but sincefixed_cause is immortal the refleak didn't appear.
| Py_DECREF(fixed_cause); | ||
| gotoraise_error; | ||
| } | ||
| Py_DECREF(cause); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Just checking so I'm clear:PyException_SetCause steals a reference right? That's why we're not decrefingfixed_cause here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
AFAIR, yes.PyException_Set* always steal refs IIRC, but I will check this again (it's always a pain to remember which function steals or borrows)
efimov-mikhailNov 9, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yes,PyException_SetCause steals a reference tocause:
Lines 548 to 551 in7ae440f
| /* Steals a reference to cause */ | |
| void | |
| PyException_SetCause(PyObject*self,PyObject*cause) | |
| { |
efimov-mikhail left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM
0c77e7c intopython:mainUh oh!
There was an error while loading.Please reload this page.
Thanks@picnixz for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
… from cause` (pythonGH-140908)Fix a reference leak in `raise E from T` when `T` is an exceptionsubtype for which `T.__new__` does not return an exception instance.(cherry picked from commit0c77e7c)Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sorry,@picnixz, I could not cleanly backport this to |
GH-141282 is a backport of this pull request to the3.14 branch. |
GH-141283 is a backport of this pull request to the3.13 branch. |
…c from cause` (GH-140908) (#141282)gh-140530: fix a reference leak in an error path for `raise exc from cause` (GH-140908)Fix a reference leak in `raise E from T` when `T` is an exceptionsubtype for which `T.__new__` does not return an exception instance.(cherry picked from commit0c77e7c)Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Uh oh!
There was an error while loading.Please reload this page.