Issue812369
Created on 2003-09-25 10:49 by arigo, last changed 2009-10-14 21:20 by nascheme.
|
msg44689 - (view) |
Author: Armin Rigo (arigo) |
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) |
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) |
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) |
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) |
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) |
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) |
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) |
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) |
Date: 2009-10-14 21:20 |
|
It should fix issue1545463 and running a quick test seems to show that
it does.
|
|
| Date |
User |
Action |
Args |
| 2009-10-14 21:20:40 | nascheme | set | messages:
+ msg94033 |
| 2009-10-14 20:44:16 | gregory.p.smith | set | assignee: gregory.p.smith
nosy:
+ gregory.p.smith |
| 2009-10-14 20:40:52 | belopolsky | set | nosy:
+ belopolsky messages:
+ msg94027
|
| 2009-03-31 13:18:10 | pitrou | set | type: behavior stage: patch review messages:
+ msg84775 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.patch nosy:
+ nascheme messages:
+ msg82136 |
| 2009-01-12 12:57:46 | pitrou | set | nosy:
+ pitrou messages:
+ msg79671 |
| 2008-01-04 19:57:20 | christian.heimes | set | nosy:
+ christian.heimes messages:
+ msg59257 versions:
+ Python 2.6, Python 3.0, - Python 2.4 |
| 2003-09-25 10:49:56 | arigo | create | |
|