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.

Title: module shutdown procedure based on GC
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.2
Status: closed Resolution: duplicate
Dependencies: Superseder: Stop purging modules which are garbage collected before shutdown
View: 18214
Assigned To: Nosy List: BreamoreBoy, Stefan.Friesel, andrea.corbellini, arigo, asvetlov, belopolsky, brett.cannon, cburroughs, christian.heimes, eric.snow, flox, glchapman, gregory.p.smith, loewis, nascheme, ncoghlan, pitrou
Priority: normal Keywords: patch

Created on 2003-09-25 10:49 by arigo, last changed 2022-04-10 16:11 by admin. This issue is now closed.

File name Uploaded Description Edit
x.txt arigo, 2003-09-25 10:49 patch
0001-update-GC-shutdown-patch.patch nascheme, 2009-02-15 01:00
Messages (24)
msg44689 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2003-09-25 10:49
This patches changes PyImport_Cleanup() in an attempt
to make the module shutdown order more predictable in
the presence of modules that import each other. Before
it explicitely clears the globals of the modules, it
relies on the GC to try to do it more cleanly.

To prevent objects with __del__ methods from keeping
whole modules alive I actually replace each module with
a weak reference to it in sys.modules. This allows me
to find modules that remain alive after a call to
PyGC_Collect(), and then go back to the old technique
of clearing their globals.

Note that weak references to dead cycles containing
objects with finalizers have a strange property in
Python: if you use the weak reference again you can
break the cycles, but the objects with finalizers still
won't be collected because they are in gc.garbage. As
this is exactly what occurs above, I clear the
gc.garbage list explicitely before the final
PyGC_Collect() call. I'm not sure exactly what this
might do; could it release older objects that were
never put in a module but that at some time were put in
gc.garbage and whose cycles were later broken? If so,
is it a good thing to release them now? (Would it make
sense to clear gc.garbage and call gc.collect again
from time to time to check if the objects are still in
a cycle?)

This patch does not change the behavior of module
objects clearing their globals dictionary as soon as
they are deallocated. This could be work investigating.

This patch puts weak references (actually proxies) in
sys.modules but only at shutdown time. Moure thoughts
could be given towards allowing weak references during
normal program execution as well. To do so we must
ensure that 'import' statements return the real module,
not the weak proxy object, which is more difficult than
it first seems in the presence of packages.

And finally -- this patch has not really been tested,
apart from the fact that it passes the test suite.
msg44690 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2003-09-27 17:31
Logged In: YES 

I think clearing gc.garbage at shutdown time is a good idea;
gc.garbage is mostly a debugging aid, and should be empty in
production code. If it still contains objects at shutdown
time, it is IMO reasonable to give them a chance to become
collected, in case somebody broke their cycles.
msg44691 - (view) Author: Greg Chapman (glchapman) Date: 2004-03-11 13:56
Logged In: YES 

Not sure if this is a good idea, but I wonder if the extensions 
dictionary should be cleared (ie _PyImport_Fini called) 
sooner, possibly even before PyImport_Cleanup.  This would 
allow garbage collection during PyImport_Cleanup to catch 
anything a C module might have created which is in a cycle 
with its module (through a bad design on my part, I recently 
discovered I had created just such a cycle).

I suppose _PyImport_Fini is called when it is called because 
some code during PyImport_Cleanup might import a 
dynamic module, which would then create a new extensions 
dictionary if _PyImport_Fini had already been run.  Perhaps 
a flag could be added so that _PyImport_FixupExtension 
would not try to add a module's dict to extensions if Python 
is currently shutting down.
msg44692 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2004-05-18 18:40
Logged In: YES 

Just so you know, Armin, the patch did not apply cleanly; the comment 
for pythonrun.c did not apply.  I also need to add an extern declaration in 
import.c for _PyGC_garbage for the code to compile (OS X 10.3, gcc 
msg59257 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-04 19:57
Consider this patch for 2.6 and discuss it at the bug day.
msg79671 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-01-12 12:57
Looking at the patch, is there any reason it doesn't get rid of the
current _PyModule_Clear() implementation to replace it by a call to
PyDict_Clear() followed by PyGC_Collect()?
(the call to PyGC_Collect could be disabled while finalizing, because
there's no use calling it as many times as there are modules to be

The major annoyance with the current scheme is that, at interpreter
shutdown, some globals you want to rely on in your destructors suddenly
become None.

About what to do of gc.garbage at shutdown, there was another proposal
in #477863.
msg82136 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2009-02-15 01:00
This sounds like a nice idea.  The current cleanup procedure in
pythonrun.c is pretty lame since it can play havoc with __del__ methods
(e.g. if they run after globals have been cleared).

I've updated the patch to work with the current SVN head.  Probably this
should get tested with big applications based on Zope, Django, etc.
msg84775 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-03-31 13:18
Retargetting, and I hope someone can take a look at the patch and give
it the green light :-)
msg94027 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2009-10-14 20:40
Does this patch fix issue1545463 by any chance?  I am away from a 
development box ATM and cannot test the patch myself.
msg94033 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2009-10-14 21:20
It should fix issue1545463 and running a quick test seems to show that
it does.
msg110337 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-14 22:42
issue1545463 has been closed as "won't fix", so wouldn't implementing this patch kill two birds with one stone?
msg114285 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-08-18 22:25
#1545463 has been reopened with comments about being used as a stop gap.  Possibly review and implementation of the patch here would be a better option, sorry it's over my head.
msg118245 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2010-10-09 04:50
0001-update-GC-shutdown-patch.patch looks sane to me at first glance.  any other opinions?
msg118259 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-09 10:01
The patch is obviously against 2.x (there are some PyString_Check's on module names, for example). It should be regenerated against 3.x.

Also, it would be nice if a test could be devised to check that the shutdown procedure works as expected (I'm not sure how such a test would look like).
msg153530 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-02-17 03:47
In #14035, Florent pointed out the current behaviour potentially causes problems for some uses of import_fresh_modules() in the test suite (with globals sometimes being set to None if there's no indepenent reference to the module).

GC based module cleanup would avoid that problem automatically.
msg153576 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2012-02-17 17:19
Fwiw, the behavior in PyPy is: don't do anything particular at shut-down, just shut down and quit the process.  No hacking at module globals to replace them with None, but also no guaranteeing that any __del__ method is ever called.  We didn't get a particular bug report about this so far, so it seems to work.

(This is just a report about PyPy's situation; I understand that the situation in CPython is a bit more delicate if CPython is embedded in a larger process.)
msg153603 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-02-17 21:52
Also, since this issue was last updated, Antoine devised a scheme to test some of the embedding functionality (mainly to test subinterpreters, IIRC). Perhaps that could be harnessed to check GC-based shutdown is working correctly (it might even do it already, without any changes to the test suite).
msg154094 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-02-23 22:13
> (This is just a report about PyPy's situation; I understand that the
> situation in CPython is a bit more delicate if CPython is embedded in
> a larger process.)

I think that would indeed be unacceptable for Python - there is a
long-standing expectation that we free all memory that we allocated,
as well as release any other resources that we hold. There are also
expectations wrt. running atexit code. So there clearly must be a
shutdown procedure.
msg154122 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2012-02-24 08:31
Obviously we run atexit code too.  There is no point in having atexit if it's not guaranteed to run in a normal shutdown.
msg172099 - (view) Author: Stefan Friesel (Stefan.Friesel) Date: 2012-10-05 16:57
What is the status of this? Does the patch need more reviewing?
msg172106 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-10-05 18:23
At the moment, it's like that the status of the patch needs to be reestablished. Does it apply? Does it work? Does the test suite still pass?
msg172165 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2012-10-06 03:18
It's been quite a long time since I played with this patch so my memory might be a bit fuzzy.  As I recall, it sounds good in theory but in practice it doesn't really work.  One of the core problems is that many extension modules keep references to Python objects in global or static variables.  These references keep pretty much everything alive and prevent GC cleanup of modules.

So, a necessary condition to this working is to get rid of those references and use the new module struct facility introduced by Martin.  That would be a huge amount of work but I think should be the long term goal.
msg179098 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-01-05 01:33
In addition to the problem Neil noted with references in extension modules keeping module objects themselves alive, Antoine recently noted that the other major challenge is the reference cycles between module global dictionaries and their contents. As soon as a module global has both a __del__ method and a reference back to the module globals, the entire cycle becomes uncollectable. I suspect one of the reasons PyPy can cope without the explicit reference breaking step is that their GC is better able to cope with __del__ methods than ours.

I wonder if a useful interim step might be to make the current explicit reference breaking hack a bit smarter by looking at the reference counts. (Note: some aspects of this idea could be made simpler if modules supported weak references)

1. Call importlib.invalidate_caches()
2. Delete the first module from sys.modules that has a reference count of exactly one
3. Repeat 2 until sys.modules is empty or every remaining module has a reference count greater than 1 (meaning another module has a reference to it one way or another)
4. Pick the module in sys.modules with the lowest number of references to it, delete it from sys.modules and delete the reference from the module object to its dictionary
5. Repeat 4 until sys.modules is empty

Throughout the process, keep an eye on gc.garbage - if we see a module dict show up there, hit it with the "set all globals to None" hammer. (The new callback functionality in 3.3 makes that easier - for example, you could put a sentinel object in the globals of the module being cleared and watching for a dict containing that sentinel object showing up in 'uncollectable' during the stop phase)
msg193946 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-07-30 18:34
Superceded by patch in issue 18214.
Date User Action Args
2022-04-10 16:11:22adminsetgithub: 39300
2013-08-21 06:13:40pitrousetsuperseder: module shutdown procedure based on GC -> Stop purging modules which are garbage collected before shutdown
2013-08-21 06:13:40pitrouunlinkissue812369 superseder
2013-07-30 18:34:23pitrousetstatus: open -> closed
superseder: module shutdown procedure based on GC
resolution: duplicate
messages: + msg193946
2013-07-30 18:34:23pitroulinkissue812369 superseder
2013-01-05 01:33:08ncoghlansetmessages: + msg179098
2012-12-24 16:31:36asvetlovsetnosy: + asvetlov
2012-11-13 02:45:18eric.snowsetnosy: + eric.snow
2012-11-10 22:38:57gregory.p.smithsetassignee: gregory.p.smith ->
2012-10-06 03:18:32naschemesetmessages: + msg172165
2012-10-05 18:23:20loewissetmessages: + msg172106
2012-10-05 16:57:07Stefan.Frieselsetnosy: + Stefan.Friesel
messages: + msg172099
2012-02-24 08:31:39arigosetmessages: + msg154122
2012-02-23 22:13:02loewissetmessages: + msg154094
2012-02-17 21:52:35ncoghlansetmessages: + msg153603
2012-02-17 17:19:14arigosetmessages: + msg153576
2012-02-17 07:45:29floxsetnosy: + flox
2012-02-17 03:47:59ncoghlansetnosy: + ncoghlan
messages: + msg153530
2012-02-17 03:45:22ncoghlanlinkissue14035 superseder
2010-10-09 10:01:44pitrousetmessages: + msg118259
versions: - Python 3.1, Python 2.7
2010-10-09 04:50:20gregory.p.smithsetmessages: + msg118245
2010-08-18 22:25:33BreamoreBoysetmessages: + msg114285
versions: + Python 3.2
2010-07-29 16:52:57andrea.corbellinisetnosy: + andrea.corbellini
2010-07-14 22:42:22BreamoreBoysetnosy: + BreamoreBoy
messages: + msg110337
2010-05-04 19:19:56cburroughssetnosy: + cburroughs
2009-10-14 21:20:40naschemesetmessages: + msg94033
2009-10-14 20:44:16gregory.p.smithsetassignee: gregory.p.smith

nosy: + gregory.p.smith
2009-10-14 20:40:52belopolskysetnosy: + belopolsky
messages: + msg94027
2009-03-31 13:18:10pitrousettype: behavior
stage: patch review
messages: + msg84775
versions: + Python 3.1, Python 2.7, - Python 2.6, Python 3.0
2009-03-31 13:16:00pitroulinkissue1717900 superseder
2009-02-15 01:00:47naschemesetfiles: + 0001-update-GC-shutdown-patch.patch
nosy: + nascheme
messages: + msg82136
2009-01-12 12:57:46pitrousetnosy: + pitrou
messages: + msg79671
2008-01-04 19:57:20christian.heimessetnosy: + christian.heimes
messages: + msg59257
versions: + Python 2.6, Python 3.0, - Python 2.4
2003-09-25 10:49:56arigocreate