[mod_python] Circular references starting from the request object are not collected

Gregory (Grisha) Trubetskoy grisha at modpython.org
Fri Sep 10 11:32:19 EDT 2004



On Fri, 10 Sep 2004, Nicolas Lehuen wrote:

> Meanwhile, manually deleting the reference in a finally block at the end of
> the handler does the trick...

Keep in mind that doing it at the end of the handler won't always work, as 
handler's execution can be aborted in the middle if, for exampe, the 
client decides to suddenly disconnect. This is exactly why Apache has 
cleanups - cleanups run even if the handler errored out and is aborted.

> But there is a leak remaining, which means I have to search thoroughly 
> through the code to find where this circular reference is built, and 
> delete a link from the reference chain when the request processing is 
> over. No exactly what I'd expect from a high level language.

Well it's not really an artifact of the language. Mod_python keeps the 
interpreter persistent throughout requests, and therefore you need to 
think in terms of a long-running service, including whatching out for 
memory leaks. Fortunately, Apache does a lion's share of work in this 
respect and provides tools like cleanups. I don't have much experience 
with pure-Python servers, but I imagine the memory situation is much worse 
when it comes to servers such as Zope, webware or twisted - anyone have 
any experience with this?

> I also had a look at how to support circular references in native coded
> objects (
> http://www.python.org/doc/2.3.4/api/supporting-cycle-detection.html ). It
> seems that it can be done without changing too much in requestobject.c,
> namely by modifying MpRequest_FromRequest, request_dealloc and
> MpRequest_Type and adding a tp_traverse and a tp_clear handler. The only
> thing I don't see is how to get the extraneous attributes of the request
> object (since I must traverse them in the traverse_tp handler). I just
> managed to build mod_python from source, so I may try to work on this this
> week-end.

If you can get it to a point where you have a patch, that would be 
fantastic. Also, internal stuff like this is better discussed on the 
python-dev at httpd.apache.org list - it's very low volume with much higher 
low-level expertiese on it.

Thanks,

Grisha

>
> Regards,
>
> Nicolas
>
>>
>>
>>>
>>> Mike Bayer wrote :
>>>>
>>>> workaround: use a weakref for one side of the circular reference.
>>>>
>>>> import weakref
>>>> req.foobar = weakref.ref(myfoobar)
>>>
>>> Indeed, I thought about using a weakref, alas the request object
>>> cannot be weak referenced (there again, native code must
>> implement a
>>> few trick to support weak references).
>>>
>>> Regards,
>>>
>>> Nicolas
>>>
>>> _______________________________________________
>>> Mod_python mailing list
>>> Mod_python at modpython.org
>>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>>
>>
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list