Session being overwritten by different actions


#1

Suppose you have a controller with 2 actions that look like this

def long
session[:foo] = “bar”
sleep 15
render :nothing => true
end

def short
session[:short] = 1
render :nothing => true
end

def status
render :text => “session[:short] = #{session[:short]}”
end

This app is using the default Pstore sessions

I ran this app on a bunch of mongrels. In one browser window I kicked
off the long action, meanwhile in the second I ran the short action, and
then the status action to verify that the session did indeed contain the
value 1 for the key :short.

However once the long running action completes in then clobbers the hash
with its view of the world. session[:short] goes back to being empty.

Looking at the source it is clear why this happens: the first time the
session is accessed it is loaded from disk, and it is written back when
the action completes: it doesn’t care if the session has changed in
between.

Obviously we don’t store anything vital in the session, but I was
wondering how other people cope with this.

Thanks,

Fred


#2

In case anyone digs up this thread, I implemented a solution to this
problem here:
http://about.82ask.com/2007/05/01/race-conditions-in-rails-sessions-and-how-to-fix-them/