pyramid_beaker icon indicating copy to clipboard operation
pyramid_beaker copied to clipboard

Session written when only read causing parallel issues

Open twiggy opened this issue 10 years ago • 1 comments

I have an app that calls multiple requests in parallel. One of the requests simply reads the session in order to track information while the next request actually posts information to the session. Given that we have many servers these request might not execute in the right order. What can happen is the request that only reads the session can overwrite the seconds requests set with stale data if they execute together just the right way.

Making the requests serial is not ideal as it is pretty much a fire and forget type event from the front ends perspective.

My app already explicitly calls beaker's persist() method when session variables are written. This prevents some of these issues.

On line 30 in init.py there is self.persist() and the "if" that drives it uses self.accessed() which I think is being flipped if the session variable is read.

I've hacked my code to reproduce the issue consistently which was fairly intermittent normally. Commenting out the persist on line 30 makes the issue go away.

I notice a pretty explicit comment about beaker's "auto" configuration option in the call_save method. From my perspective I need to control the sessions write explicitly. I'd like to somehow have this extension honor the auto option of beaker although I see this as probably breaking backwards compatibility of this plugin.

I'm happy to put together a pull request if you'd let me know how you'd prefer this be implemented. If you'd rather it not get implemented I'll just fork in some fashion.

I'm thinking that in call_save we could keep track of a dirty variable. Only if one of the dictionary modifying functions has been called do we call persist on line 30. call_save() will set a variables say session._pyramid_session_dirty = True given that _dirty might have a name conflict.

I tried changing self.accessed to self.dirty, but that didn't seem to fix it. Looking at beaker code that doesn't seem reliable anyways. I couldn't find a way to see if the session had actually been modified vs just read in beaker. Accessed seems to only be if it got loaded up.

Thanks!

twiggy avatar Jan 06 '15 21:01 twiggy

I released pyramid-beaker 0.9 which should fix this

fschulze avatar Apr 05 '24 06:04 fschulze