[mod_python] session locking causes hang with mpservlets

Graham Dumpleton grahamd at dscpl.com.au
Sun Mar 27 20:35:10 EST 2005

On 28/03/2005, at 10:31 AM, Graham Dumpleton wrote:
> The change in mpservlets is:
>     def __cleanup_session(self):
>         if self.use_session and self.session is not None:
>             self.session.save()
>             self.session.unlock()
>             self.session = None
> Ie., add call to unlock() just after saving the session.
> Normally this unlock() would be called, but only when the Session 
> object
> itself is deleted or by way of a cleanup function registered against 
> the
> request using req.register_cleanup().
> The problem is that even though one sets self.session to None, this 
> doesn't
> mean it will be deleted immediately as one is in part at the mercy of 
> the
> Python garbage collector. That it isn't unlocked may actually keep it 
> alive
> longer and normally it may only be unlocked by request cleanup function
> at which point it then gets destroyed.
> Unfortunately, because your internal redirect loops back onto the same
> servlet, the second time through it tries to lock the same session, but
> where the prior hasn't been unlocked yet. Thus deadlock as lock is not
> reentrant for same thread.
> Thus, believe mpservlet possibly needs to be fixed, but might also 
> question
> your design anyway, there possibly being better ways of achieving what 
> you
> want. You might want to explain to the list what you are wanting to 
> achieve
> and you might get some helpful suggestions back.

Actually, it is the registered cleanup function in Session code that 
the session object alive and locked. This is interesting, as it possibly
means that the general solution we give to people about session locking
problems and internal redirects should be changed to:


If you don't include the unlock(), any redirect back onto the same 
will cause a deadlock as was the case with the mpservlets case.

About time for a FAQ entry for redirects and mention this. I'll add it 
to my
list of things to do. :-)


More information about the Mod_python mailing list