Forum: Ruby on Rails optimistic locking with session store?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2009-04-20 23:45
Okay, continuing in my project of abusing Rails horribly.

It looks to me like even standard Rails sessions are subject to a race
condition if the same session is accessed concurrently. Request1 checks
out a session. Request 2 checks out a session, makes a change, and
writes it back to the store. Request 1, still finishing up, makes a
change, and writes the session back to the store... overwriting the
changes Request2 made.

So, if I'm using an ActiveRecord store... what happens if I just add a
lock_version column to the model?  But then I guess an exception will be
raised from the innards of ActiveController somewhere. And it would be
unclear how to recover from it. So that's not quite right.

Has anyone come up with an optimistic locking solution for Rails session
to get around this race condition issue?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-21 09:20
(Received via mailing list)
On Apr 20, 10:45 pm, Jonathan Rochkind <rails-mailing-l...@andreas-
s.net> wrote:
> Okay, continuing in my project of abusing Rails horribly.
>
> It looks to me like even standard Rails sessions are subject to a race
> condition if the same session is accessed concurrently. Request1 checks
> out a session. Request 2 checks out a session, makes a change, and
> writes it back to the store. Request 1, still finishing up, makes a
> change, and writes the session back to the store... overwriting the
> changes Request2 made.
>
Completely. See
http://www.paulbutcher.com/2007/05/race-conditions...
for how we dealt with this at work.

> So, if I'm using an ActiveRecord store... what happens if I just add a
> lock_version column to the model?  But then I guess an exception will be
> raised from the innards of ActiveController somewhere. And it would be
> unclear how to recover from it. So that's not quite right.
>
Yup, not quite as simple as just detecting the error condition.

> Has anyone come up with an optimistic locking solution for Rails session
> to get around this race condition issue?

http://github.com/fcheung/smart_session_store/tree/master

trunk actually uses a pessimistic lock on the session, I have a not
yet fully battle tested branch that uses optimistic locking.

Fred
This topic is locked and can not be replied to.