Issue 812369: module shutdown procedure based on GC (original) (raw)

process

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.

Files
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. http://mail.python.org/pipermail/python-dev/2003-September/038238.html 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 user_id=21627 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 user_id=86307 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 user_id=357491 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 3.3).
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 disbanded) 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 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 and running a quick test seems to show that it does.
msg110337 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-14 22:42
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: Alyssa 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: Alyssa 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: Alyssa 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.
History
Date User Action Args
2022-04-10 16:11:22 admin set github: 39300
2013-08-21 06:13:40 pitrou set superseder: module shutdown procedure based on GC -> Stop purging modules which are garbage collected before shutdown
2013-08-21 06:13:40 pitrou unlink issue812369 superseder
2013-07-30 18:34:23 pitrou set status: open -> closedsuperseder: module shutdown procedure based on GCresolution: duplicatemessages: +
2013-07-30 18:34:23 pitrou link issue812369 superseder
2013-01-05 01:33:08 ncoghlan set messages: +
2012-12-24 16:31:36 asvetlov set nosy: + asvetlov
2012-11-13 02:45:18 eric.snow set nosy: + eric.snow
2012-11-10 22:38:57 gregory.p.smith set assignee: gregory.p.smith ->
2012-10-06 03🔞32 nascheme set messages: +
2012-10-05 18:23:20 loewis set messages: +
2012-10-05 16:57:07 Stefan.Friesel set nosy: + Stefan.Frieselmessages: +
2012-02-24 08:31:39 arigo set messages: +
2012-02-23 22:13:02 loewis set messages: +
2012-02-17 21:52:35 ncoghlan set messages: +
2012-02-17 17:19:14 arigo set messages: +
2012-02-17 07:45:29 flox set nosy: + flox
2012-02-17 03:47:59 ncoghlan set nosy: + ncoghlanmessages: +
2012-02-17 03:45:22 ncoghlan link issue14035 superseder
2010-10-09 10:01:44 pitrou set messages: + versions: - Python 3.1, Python 2.7
2010-10-09 04:50:20 gregory.p.smith set messages: +
2010-08-18 22:25:33 BreamoreBoy set messages: + versions: + Python 3.2
2010-07-29 16:52:57 andrea.corbellini set nosy: + andrea.corbellini
2010-07-14 22:42:22 BreamoreBoy set nosy: + BreamoreBoymessages: +
2010-05-04 19:19:56 cburroughs set nosy: + cburroughs
2009-10-14 21:20:40 nascheme set messages: +
2009-10-14 20:44:16 gregory.p.smith set assignee: gregory.p.smithnosy: + gregory.p.smith
2009-10-14 20:40:52 belopolsky set nosy: + belopolskymessages: +
2009-03-31 13🔞10 pitrou set type: behaviorstage: patch reviewmessages: + versions: + Python 3.1, Python 2.7, - Python 2.6, Python 3.0
2009-03-31 13:16:00 pitrou link issue1717900 superseder
2009-02-15 01:00:47 nascheme set files: + 0001-update-GC-shutdown-patch.patchnosy: + naschememessages: +
2009-01-12 12:57:46 pitrou set nosy: + pitroumessages: +
2008-01-04 19:57:20 christian.heimes set nosy: + christian.heimesmessages: + versions: + Python 2.6, Python 3.0, - Python 2.4
2003-09-25 10:49:56 arigo create