classification
Title: Fix and improve errors handling in the garbage collector
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: miss-islington, pitrou, serhiy.storchaka, vstinner
Priority: low Keywords: patch

Created on 2018-05-23 17:41 by serhiy.storchaka, last changed 2019-03-21 13:09 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7078 merged serhiy.storchaka, 2018-05-23 17:50
PR 7094 merged miss-islington, 2018-05-24 12:19
PR 7095 merged serhiy.storchaka, 2018-05-24 12:30
PR 7096 merged serhiy.storchaka, 2018-05-24 12:30
PR 7126 merged serhiy.storchaka, 2018-05-26 10:07
Messages (8)
msg317431 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-23 17:41
There are following bugs in the garbage collector.

* If the garbage collector fails to add an object with __del__ or referenced by an object with __del__ to gc.garbage (in handle_legacy_finalizers()), it leaks it and other not added objects with __del__ and referenced by them. They become no longer accessible by the garbage collector.

* PyGC_Collect() is not documented, but it is a public C API. And it can be called by user with an exception set. PyGC_Collect() then can either crash or just silent the exception. It is safer to safe possible exception and restore it after collecting.

* A pointer to released member can be used (compared with NULL) in invoke_gc_callback(). This is an undefined behavior.
msg317567 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-24 12:19
New changeset 301e3cc8a5bc68c5347ab6ac6f83428000d31ab2 by Serhiy Storchaka in branch 'master':
bpo-33622: Fix issues with handling errors in the GC. (GH-7078)
https://github.com/python/cpython/commit/301e3cc8a5bc68c5347ab6ac6f83428000d31ab2
msg317568 - (view) Author: miss-islington (miss-islington) Date: 2018-05-24 12:39
New changeset 2fe940c727802ad54cff9486c658bc38743f7bfc by Miss Islington (bot) in branch '3.7':
bpo-33622: Fix issues with handling errors in the GC. (GH-7078)
https://github.com/python/cpython/commit/2fe940c727802ad54cff9486c658bc38743f7bfc
msg317569 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-24 13:05
New changeset f0e04b2ae27a4da940a76ae522f0438f80ce5d5a by Serhiy Storchaka in branch '3.6':
[3.6] bpo-33622: Fix issues with handling errors in the GC. (GH-7078) (GH-7095)
https://github.com/python/cpython/commit/f0e04b2ae27a4da940a76ae522f0438f80ce5d5a
msg317617 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-24 20:27
New changeset 9044cd675902b438bc500908e410382ff48299d8 by Serhiy Storchaka in branch '2.7':
[2.7] bpo-33622: Fix issues with handling errors in the GC. (GH-7078) (#7096)
https://github.com/python/cpython/commit/9044cd675902b438bc500908e410382ff48299d8
msg317732 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-26 10:13
PR 7126 adds several assert(!PyErr_Occurred()) for earlier detection of leaked exceptions. And the failure in adding to gc.garbage is no longer fatal. It is better to not add an object to gc.garbage in this iteration than crash when we run garbage collection when have no memory.
msg318048 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-29 15:50
New changeset c4653c9bf159c3919a50f4ced32eef713e7e764e by Serhiy Storchaka in branch 'master':
bpo-33622: Add checks for exceptions leaks in the garbage collector. (GH-7126)
https://github.com/python/cpython/commit/c4653c9bf159c3919a50f4ced32eef713e7e764e
msg318290 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-31 12:30
See issue33712, issue33713 and issue33714 for three cases in the stdlib where an exception can be set in the tp_clear handler. This caused a crash in the garbage collector, or can be just silenced if failed at the shutdown stage. In the master branch it will cause writing a traceback to the stderr. It would be better to handle exceptions locally in the tp_clear handlers. But perhaps it may be worth to handle leaked exceptions in the garbage collector too. I'm just not sure about a solution. Writing a traceback to the stderr can cause a regression if it was just silenced before. But silencing it can hide bugs.
History
Date User Action Args
2019-03-21 13:09:05serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-05-31 12:30:05serhiy.storchakasetmessages: + msg318290
2018-05-29 15:50:13serhiy.storchakasetmessages: + msg318048
2018-05-26 10:13:29serhiy.storchakasetmessages: + msg317732
2018-05-26 10:07:43serhiy.storchakasetstage: patch review
pull_requests: + pull_request6761
2018-05-26 10:02:57serhiy.storchakasetstatus: closed -> open
title: Fix errors handling in the garbage collector -> Fix and improve errors handling in the garbage collector
resolution: fixed -> (no value)
stage: resolved -> (no value)
2018-05-25 14:14:35serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-05-24 20:27:11serhiy.storchakasetmessages: + msg317617
2018-05-24 13:05:43serhiy.storchakasetmessages: + msg317569
2018-05-24 12:39:11miss-islingtonsetnosy: + miss-islington
messages: + msg317568
2018-05-24 12:30:35serhiy.storchakasetpull_requests: + pull_request6732
2018-05-24 12:30:23serhiy.storchakasetpull_requests: + pull_request6731
2018-05-24 12:19:45miss-islingtonsetpull_requests: + pull_request6730
2018-05-24 12:19:37serhiy.storchakasetmessages: + msg317567
2018-05-24 08:05:16pitrousetpriority: normal -> low
2018-05-23 18:04:41serhiy.storchakasettitle: Fix and improve errors handling in the garbage collector -> Fix errors handling in the garbage collector
2018-05-23 17:50:39serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request6708
2018-05-23 17:41:20serhiy.storchakacreate