Forum: Ruby on Rails Session being overwritten by different actions

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.
153868096910b6bca47e2a9212c2df56?d=identicon&s=25 Frederick Cheung (fcheung)
on 2007-02-14 16:31
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
153868096910b6bca47e2a9212c2df56?d=identicon&s=25 Frederick Cheung (fcheung)
on 2007-05-02 00:16
In case anyone digs up this thread, I implemented a solution to this
problem here:
http://about.82ask.com/2007/05/01/race-conditions-...
This topic is locked and can not be replied to.