My Rails session is getting reset when I have concurrent requests

Posted by alex_c on Stack Overflow See other posts from Stack Overflow or by alex_c
Published on 2010-06-07T22:25:09Z Indexed on 2010/06/07 22:32 UTC
Read the original article Hit count: 182

Filed under:
|
|

I think I might be misunderstanding something about Rails sessions, so please bear with me, I might not be phrasing my question the best way.

I'm working on an iPhone app with a Ruby on Rails backend. I have a web view which by default goes to the index action of one controller (and uses sessions), and in the background a bunch of API calls going to a different controller (and which don't need to use sessions).

The problem is, the sessions set by my web view seem to be overwitten by the API calls. My staging server is pretty slow, so there's lots of time for the requests to overlap each other - what I see in the logs is basically this:

Request A (first controller) starts.  Session is empty.
Request B (second controller) starts.  Session is empty.
Request A finishes.  Request A has done authentication, and stored the user ID in the session. Session contains user ID.
Request B finishes.  Session is empty.
Request C starts.  Session is empty - not what I want.

Now, the strange thing is that request B should NOT be writing anything to the session.

I do have before and after filters which READ from the session - things like:

user = User.find_by_id(session[:id])

or

logger.debug session.inspect

and if I remove all of those, then everything works as expected - session contents get set by request A, and they're still there when request C starts.

So. I think I'm missing something about how sessions work. Why would reading from the session overwrite it? Should I be accessing it some other way? Am I completely on the wrong track and the problem is elsewhere?

Thank you for any insights!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about session