-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
bpo-37261: Document sys.unraisablehook corner cases #14059
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
|
Shouldn't it make more sense to not pass the object if it happens in a delicate moment to avoid resurrection? Resurrection can be tricky especially if it can happen in a very delicate way like what we are documenting here. We are already avoiding passing the object in places where is complicated or close to finalization like: Lines 927 to 935 in 9549203
|
Document reference cycle and resurrected objects issues in sys.unraisablehook() and threading.excepthook() documentation. Fix test.support.catch_unraisable_exception(): __exit__() no longer ignores unraisable exceptions. Fix test_io test_writer_close_error_on_close(): use a second catch_unraisable_exception() to catch the BufferedWriter unraisable exception.
|
@pablogsal: I was wrong, the resurrected object is not finalized twice. Finalization only occurs once. First, I didn't understand why test_writer_close_error_on_close() of test_io logs 2 unraisable exceptions. In fact, the first comes from BufferedRWPair. catch_unraisable_exception() resurrects BufferedRWPair which prevents its deallocation. Once BufferedRWPair is deallocated (at catch_unraisable_exception() exit which does "del self.unraisable"), BufferedWriter logs a second unraisable exception. Again, it's only deallocated when the second catch_unraisable_exception() exit. I modified the test to use two (nested) catch_unraisable_exception(). |
I don't see how resurrection is "tricky". Since PEP 442 -- Safe object finalization, Python 3.4 finalization is now clearly separated from deallocation. When an object is resurected, it may become "unusable", but using it should not crash Python. You must not use an object after it's deallocated, but... you cannot: the last strong reference to the object is gone, the object is no longer accessible. |
|
For the very specific case of delete_garbage() in gcmodule.c, I agree that the object must not be passed to sys.unraisablehook, since tp_clear() can make an object inconsistent or "broken". Using it can crash Python. But for the general case of an exception occurred during an object finalization, IMHO it's fine to resurrect it in a hook. Well, this PR advices to avoid storing an object to avoid object resurrection :-) |
|
Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8. |
Document reference cycle and resurrected objects issues in sys.unraisablehook() and threading.excepthook() documentation. Fix test.support.catch_unraisable_exception(): __exit__() no longer ignores unraisable exceptions. Fix test_io test_writer_close_error_on_close(): use a second catch_unraisable_exception() to catch the BufferedWriter unraisable exception. (cherry picked from commit 212646c) Co-authored-by: Victor Stinner <[email protected]>
|
GH-14089 is a backport of this pull request to the 3.8 branch. |
Document reference cycle and resurrected objects issues in sys.unraisablehook() and threading.excepthook() documentation. Fix test.support.catch_unraisable_exception(): __exit__() no longer ignores unraisable exceptions. Fix test_io test_writer_close_error_on_close(): use a second catch_unraisable_exception() to catch the BufferedWriter unraisable exception. (cherry picked from commit 212646c) Co-authored-by: Victor Stinner <[email protected]>
Document reference cycle and resurrected objects issues in sys.unraisablehook() and threading.excepthook() documentation. Fix test.support.catch_unraisable_exception(): __exit__() no longer ignores unraisable exceptions. Fix test_io test_writer_close_error_on_close(): use a second catch_unraisable_exception() to catch the BufferedWriter unraisable exception.
Document reference cycle and resurrected objects issues in sys.unraisablehook() and threading.excepthook() documentation. Fix test.support.catch_unraisable_exception(): __exit__() no longer ignores unraisable exceptions. Fix test_io test_writer_close_error_on_close(): use a second catch_unraisable_exception() to catch the BufferedWriter unraisable exception.
Document reference cycle and resurrected objects issues in
sys.unraisablehook and threading.excepthook documentations.
https://bugs.python.org/issue37261