This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Print warning at shutdown if gc.garbage not empty
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: flox, gvanrossum, ncoghlan, pitrou, tim.peters
Priority: normal Keywords: patch

Created on 2001-11-03 20:07 by tim.peters, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
gc_fini.patch pitrou, 2010-08-04 12:07
gc_fini2.patch pitrou, 2010-08-04 12:25
gc_fini3.patch pitrou, 2010-08-04 23:11
Messages (10)
msg61071 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-11-03 20:07
Here are comments from Guido, taken from a patch 
report that's been closed:

"""
In a discussion before lunch, Tim suggested that when 
Python exits (or better, in Py_Finalize()) a check 
could be made if there is any garbage in gc.garbage, 
and if so, a warning about this should be printed to 
stderr. That seems a nice feature.

In that same discussion, I realized that the module 
cleanup behavior (where all globals in a module are 
set to None when the module object is deallocated) is 
probably no longer necessary now that we have GC.
"""

The point to #1 is that we let objects in unreachable 
cycles leak when they have __del__ methods.  While we 
give the user ways to know about that and to clean 
them up (via exposing the trash in gc.garbage), a non-
expert user (or an expert who simply isn't thinking 
about this -- whatever, you don't know unless you 
specifically look for it) may never know that they're 
leaking.
msg77936 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-16 21:41
Although this dates back to 2001, I think this might still be useful.
msg112783 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-04 10:19
Displaying the full list may be unwieldy if it's very large. Instead, we could by default display the following kind of message:

Python warning: 5 uncollectable objects remaining at interpreter shutdown. Use gc.DEBUG_UNCOLLECTABLE to list them.

And list gc.garbage only if gc.DEBUG_UNCOLLECTABLE is defined.
msg112788 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-04 11:21
Here is a patch. Perhaps I should add tests too.
msg112793 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-04 12:07
New patch with tests.
msg112795 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-04 12:25
More thorough tests.
msg112898 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2010-08-04 21:26
Patch looks basically OK on an eyeball scan. A couple of suggestions:

- use test.script_helper.run_python rather than rolling your own run_command (or, at least, just make run_command a thin wrapper around run_python). I've been trying to trim down the number of different ways the test suite launches interpreter subprocesses for testing purposes, and script_helper also does a bit of extra cleanup to try to eliminate false alarms regarding reference leaks. That said, I do like the way you're checking that the output went to the correct stream, so perhaps instead add a run_python variant to script_helper that returns a 3-tuple containing the exit code, stdout and stderr and make your run_command a wrapper around that (then create an issue to migrate the rest of the script_helper.run_python based tests to switch to the new version so they will also check that the output is on the correct stream rather than lumping the two streams together).

- update the description of gc.DEBUG_UNCOLLECTABLE in the docs to specifically mention that the complete list of uncollectable objects will also be printed at interpreter shutdown.
msg112915 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-04 22:45
> - use test.script_helper.run_python rather than rolling your own
> run_command (or, at least, just make run_command a thin wrapper around
> run_python). I've been trying to trim down the number of different
> ways the test suite launches interpreter subprocesses for testing
> purposes, and script_helper also does a bit of extra cleanup to try to
> eliminate false alarms regarding reference leaks.

After a quick grep, it seems script_helper is really in the minority
(it's only used in three test files) while ad hoc calls to subprocess
with sys.executable are all over the place. I'd rather open a separate
feature request for that new script_helper function than convert all
uses of subprocess in this issue.

> - update the description of gc.DEBUG_UNCOLLECTABLE in the docs to
> specifically mention that the complete list of uncollectable objects
> will also be printed at interpreter shutdown.

Ah, indeed. Thanks!
msg112920 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-04 23:11
Here is a new patch with doc.
msg113343 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-08 22:21
I went forward and committed the patch in r83861.
History
Date User Action Args
2022-04-10 16:04:36adminsetgithub: 35458
2010-08-08 22:21:45pitrousetstatus: open -> closed
resolution: fixed
messages: + msg113343
2010-08-04 23:11:07pitrousetfiles: + gc_fini3.patch

messages: + msg112920
2010-08-04 22:45:05pitrousetmessages: + msg112915
2010-08-04 21:26:10ncoghlansetmessages: + msg112898
2010-08-04 12:31:09pitrousetnosy: + gvanrossum, ncoghlan
2010-08-04 12:25:52pitrousetfiles: + gc_fini2.patch

messages: + msg112795
2010-08-04 12:07:41pitrousetfiles: - gc_fini.patch
2010-08-04 12:07:34pitrousetfiles: + gc_fini.patch

messages: + msg112793
2010-08-04 11:23:48pitrousetfiles: + gc_fini.patch
stage: needs patch -> patch review
2010-08-04 11:23:37pitrousetfiles: - gc_fini.patch
2010-08-04 11:21:44pitrousetfiles: + gc_fini.patch
keywords: + patch
messages: + msg112788
2010-08-04 10:19:37pitrousetmessages: + msg112783
versions: - Python 2.7
2010-03-17 17:46:04pitrousettitle: New gc work -> Print warning at shutdown if gc.garbage not empty
stage: needs patch
versions: + Python 3.2, - Python 3.1
2010-03-17 17:45:38pitrousetnosy: + flox
2008-12-16 21:41:32pitrousetnosy: + pitrou
messages: + msg77936
components: + Interpreter Core, - None
versions: + Python 3.1, Python 2.7
2001-11-03 20:07:42tim.peterscreate