You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Calling PyCG_Collect before destroying sub interpreter. #43
class C(object):
pass
class Pool(object):
def __del__(self):
print('del')
list()
C.pool = Pool()
Put this in a WSGI script file which is executed in context of a sub interpreter.
Simply adding PyGC_Collect() call before PyEnd_Interpreter() doesn't solve the problem.
There looks to be a bigger problem in the Python interpreter shutdown itself as __del__() methods of objects created in a sub interpreter should not be getting called when the main interpreter is being shutdown.
class C(object):
pass
class Pool(object):
def __init__(self):
self.myimport = __import__
def __del__(self):
mod_wsgi = self.myimport('mod_wsgi')
print('del in %r' % mod_wsgi.application_group)
list()
import mod_wsgi
print('init %r' % mod_wsgi.application_group)
C.pool = Pool()
When run as test, one gets:
init 'xxx'
del in ''
Exception TypeError: "'NoneType' object is not callable" in <bound method Pool.__del__ of <__wsgi__.Pool object at 0x10fe72150>> ignored
So the test confirms that the __del__() was called in the wrong interpreter context.
It's not a bug in Python, it just shows that subinterpreters are much less isolated from each other than people may assume. The GC is a global operation, it operates on the whole object graph: there is no distinct per-interpreter subgraph to operate on.
If you really depend on destructors being called from the "right" subinterpreter, then you should strive to avoid reference cycles (using weakrefs is a way to do so).
So there's a couple of separate things here. One is as @pitrou says, subinterpreters share a lot of machinery - malloc arenas, the GIL, and so on. Or they used to, I haven't tracked all the recent changes.
Secondly, __del__ doesn't usually do what people want it to do: Python does not guarantee calling del at all, ever. https://docs.python.org/3/reference/datamodel.html#object.__del__ - note the two specific cases: if the reference count does not hit zero, __del__ is not called (cycle breaking is one way that we try to call __del__ - but cannot guarantee we will. Secondly interpreter shutdown does not guarantee calling __del__ at all, and I'd expect subinterpreter shutdown to equally have no guarantee there - both because of the shared memory model and because of the requirement that shutdown actually, you know, shutdown: blocking forever until the ref count hits 0 would be a great way to not shutdown.
As for the issue you are encountering, doing a GC before subinterpreter shutdown is irrelevant to this issue: the underlying sequence of interactions is this:
there is a live reference to pool (mod.Pool)
that reference gets cleared when the module globals are cleared (now we have mod.Pool = None)
Pool has a refcount > 0 so it is not freed by step 2
builtins.list has been cleared at the same time (either before or after, doesn't matter right now)
sometime later gc runs which leads to Pool being freed and __del__ running. But list is not a valid symbol anymore, so boom.
If we do a GC before step 2, it has no effect and rightly so.
If we do a GC after step 4, it only moves the error to 'right now' rather than whenever GC would have occured on its own.
How can you fix this permanently?
Well the Pool code has to be written such that __del__ works when:
no module globals in any module have a value: Specifically, nothing in the current module, and nothing in builtins, can be depended on.
To avoid Pool hanging around, it must have exactly one reference to it - the module reference. It cannot have a self reference or anything in it refering to it.
NB: If builtins.list is cleared before step 2, then even if Pool is freed immediately the module globals are cleared, the code would still error - because the list function is set to None. So all things needed in the __del__ should be captured in a closure of some sort - e.g. on the instance
From mod_wsgi's perspective, you could abstract this and offer an object with the following semantics:
created before any user code
guaranteed (unless the user does something against the rules) to have exactly one reference and to be freed when the module globals are zeroed on
holds references to anything that should be freed at mod_wsgi shutdown: so rather than module.pool or whatever, people would say 'thisspecialobject.pool = whatever'.
Folk would still have to be careful of any use of globals in __del__ of course.
You can also use weakref.finalize, which will register an atexit handler for the finalizer to be called. In Python 3.7, atexit handlers are per-interpreter so it should work as you expect.
FWIW, mod_wsgi has implemented itself calling of atexit callbacks in sub interpreters prior to destruction of sub interpreters for all Python versions for a very long time. So if workarounds for this problem in underlying CPython can be implemented on top of atexit then that can be used. Based on my experience and attitudes I have seen by module authors in the past around other issues which cause their modules not to work in sub interpreters, I expect no one to actually implement any such workaround just so their code works in sub interpreters. It is such a niche use case they will not bother.
In practice the only real solution for this is to not use sub interpreters at all. As long as people follow the current best practice recommendation when using mod_wsgi of using daemon mode, running each WSGI application in a separate daemon process group, and forcing the use of the first interpreter context, they will avoid as much as they can the various issues with sub interpreters.
I am going to close this as there isn't anything more mod_wsgi can do about it given that it is due to how CPython itself works and the limitations on sub interpreters. The idea that mod_wsgi implement some special object to be used by code authors to deal with this case would be a waste of time as it would never be used.
Activity
GrahamDumpleton commentedon Nov 1, 2014
Test code is:
Put this in a WSGI script file which is executed in context of a sub interpreter.
Simply adding PyGC_Collect() call before PyEnd_Interpreter() doesn't solve the problem.
There looks to be a bigger problem in the Python interpreter shutdown itself as
__del__()methods of objects created in a sub interpreter should not be getting called when the main interpreter is being shutdown.GrahamDumpleton commentedon Nov 1, 2014
This is seriously obscure bug in Python.
Use test:
When run as test, one gets:
So the test confirms that the
__del__()was called in the wrong interpreter context.pitrou commentedon Jul 3, 2015
It's not a bug in Python, it just shows that subinterpreters are much less isolated from each other than people may assume. The GC is a global operation, it operates on the whole object graph: there is no distinct per-interpreter subgraph to operate on.
If you really depend on destructors being called from the "right" subinterpreter, then you should strive to avoid reference cycles (using weakrefs is a way to do so).
rbtcollins commentedon Jan 21, 2018
So there's a couple of separate things here. One is as @pitrou says, subinterpreters share a lot of machinery - malloc arenas, the GIL, and so on. Or they used to, I haven't tracked all the recent changes.
Secondly,
__del__doesn't usually do what people want it to do: Python does not guarantee calling del at all, ever. https://docs.python.org/3/reference/datamodel.html#object.__del__ - note the two specific cases: if the reference count does not hit zero,__del__is not called (cycle breaking is one way that we try to call__del__- but cannot guarantee we will. Secondly interpreter shutdown does not guarantee calling__del__at all, and I'd expect subinterpreter shutdown to equally have no guarantee there - both because of the shared memory model and because of the requirement that shutdown actually, you know, shutdown: blocking forever until the ref count hits 0 would be a great way to not shutdown.As for the issue you are encountering, doing a GC before subinterpreter shutdown is irrelevant to this issue: the underlying sequence of interactions is this:
__del__running. But list is not a valid symbol anymore, so boom.If we do a GC before step 2, it has no effect and rightly so.
If we do a GC after step 4, it only moves the error to 'right now' rather than whenever GC would have occured on its own.
How can you fix this permanently?
__del__works when:NB: If builtins.list is cleared before step 2, then even if Pool is freed immediately the module globals are cleared, the code would still error - because the list function is set to None. So all things needed in the
__del__should be captured in a closure of some sort - e.g. on the instanceFrom mod_wsgi's perspective, you could abstract this and offer an object with the following semantics:
Folk would still have to be careful of any use of globals in
__del__of course.pitrou commentedon Jan 21, 2018
You can also use
weakref.finalize, which will register anatexithandler for the finalizer to be called. In Python 3.7,atexithandlers are per-interpreter so it should work as you expect.pitrou commentedon Jan 21, 2018
Also @ericsnowcurrently
GrahamDumpleton commentedon Jan 21, 2018
FWIW, mod_wsgi has implemented itself calling of
atexitcallbacks in sub interpreters prior to destruction of sub interpreters for all Python versions for a very long time. So if workarounds for this problem in underlying CPython can be implemented on top ofatexitthen that can be used. Based on my experience and attitudes I have seen by module authors in the past around other issues which cause their modules not to work in sub interpreters, I expect no one to actually implement any such workaround just so their code works in sub interpreters. It is such a niche use case they will not bother.In practice the only real solution for this is to not use sub interpreters at all. As long as people follow the current best practice recommendation when using mod_wsgi of using daemon mode, running each WSGI application in a separate daemon process group, and forcing the use of the first interpreter context, they will avoid as much as they can the various issues with sub interpreters.
I am going to close this as there isn't anything more mod_wsgi can do about it given that it is due to how CPython itself works and the limitations on sub interpreters. The idea that mod_wsgi implement some special object to be used by code authors to deal with this case would be a waste of time as it would never be used.