-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-33622: Fix issues with handling errors in the GC. #7078
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
Conversation
* Fixed a leak when the GC fails to add an object with __del__ into the gc.garbage list. * PyGC_Collect() can now be called when an exception is set and preserves it. * Fixed an UB with comparing a dead pointer with NULL.
Looks good to me! Note: I don't know what "UB" means in the "Fixed an UB with ..." comment means. |
UB = undefined behavior. A common abbreviation in the C/C++ word. |
In that case, what about the behavior was undefined? Memory-releasing functions don't magically change the value of a pointer passed to them; So I found the rewrite slightly clearer, but didn't see a need for it. |
In C the value of a pointer becomes invalid after releasing a memory. It can "work" on some platforms with specific versions of compilers, but the Standard doesn't promise anything. The next version of the compiler can generate the code that will crash or remove the code of the whole function. This is what called an undefined behavior. |
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.
Looks good to me.
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6, 3.7. |
GH-7094 is a backport of this pull request to the 3.7 branch. |
* Fixed a leak when the GC fails to add an object with __del__ into the gc.garbage list. * PyGC_Collect() can now be called when an exception is set and preserves it. * Fixed an undefined behavior with comparing a dead pointer with NULL. (cherry picked from commit 301e3cc) Co-authored-by: Serhiy Storchaka <[email protected]>
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
…-7078) * Fixed a leak when the GC fails to add an object with __del__ into the gc.garbage list. * PyGC_Collect() can now be called when an exception is set and preserves it. * Fixed an undefined behavior with comparing a dead pointer with NULL.. (cherry picked from commit 301e3cc) Co-authored-by: Serhiy Storchaka <[email protected]>
…-7078) * Fixed a leak when the GC fails to add an object with __del__ into the gc.garbage list. * PyGC_Collect() can now be called when an exception is set and preserves it. (cherry picked from commit 301e3cc) Co-authored-by: Serhiy Storchaka <[email protected]>
GH-7095 is a backport of this pull request to the 3.6 branch. |
GH-7096 is a backport of this pull request to the 2.7 branch. |
* Fixed a leak when the GC fails to add an object with __del__ into the gc.garbage list. * PyGC_Collect() can now be called when an exception is set and preserves it. * Fixed an undefined behavior with comparing a dead pointer with NULL. (cherry picked from commit 301e3cc) Co-authored-by: Serhiy Storchaka <[email protected]>
Not true. Dereferencing a freed pointer gives UB. The code here never dereferenced As I said, I found your rewrite slightly clearer in this case anyway, so I don't care. I just object to calling a thing UB when it isn't, lest we see a slew of needless changes all over the place based on hallucinations 😉. |
From the implementation point of view, the pointer can be not an arbitrary integer, but a more complex object. For example any use of the pointer (including comparing with NULL) may require loading into a segment register, and this can crash if the segment is no longer valid. Maybe comparing the pointer to a deallocated memory with NULL will not cause any problems on any platform supported by Python. I don't know. But is better to write a code that conforms the Standard (unless practicality beats purity). |
Excellent! Thank you for the reference - I stand gratefully corrected 😃 |
__del__
into thegc.garbage
list.PyGC_Collect()
can now be called when an exception is set and preserves it.https://bugs.python.org/issue33622