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

Nicolas Lehuen nicolas at lehuen.com
Fri Sep 10 09:17:18 EDT 2004


Mike Bayer wrote :
> thats not a weakref of the request object, thats the request 
> object pointing to a weakref of *your* object, which 
> effectively breaks the circular reference.
> 
> request <-- (strong ref) --
>         --- (weak ref) --> yourobject

I also thought about this, but until now I had dismissed this since I think
it would be too effective in breaking the reference cycle. My handler would
set the weak reference in the request object and give it to the application
object, but the weak ref being for a while the only reference to my object,
it could be cleared before I actually use it...

I'll give it a try, though.

Meanwhile, manually deleting the reference in a finally block at the end of
the handler does the trick... 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.

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.

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
> 



More information about the Mod_python mailing list