My Rails session gets reset when I have concurrent requests

I think that I might misunderstand something about Rails sessions, so please bear with me, I may not be able to formulate my question in the best way.

I am working on an Ruby on Rails-enabled iPhone application. I have a web view that by default switches to the index action of one controller (and uses sessions), and in the background a bunch of API calls go to another controller (and which does not require the use of sessions).

The problem is that the sessions set up by my web view seem to be overwritten by API calls. My intermediate server is rather slow, so there is a lot of time when requests overlap with 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 it’s strange that request B should not write anything to the session.

I have before and after filters that are READ from the session - things like:

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

or

logger.debug session.inspect

and if I delete all of them, then everything will work as expected - the contents of the session will be set at the request of A, and they are still there when the request is launched C.

So. I think I am missing something about how sessions work. Why is reading from a session overwriting it? Should I address him differently? Am I completely mistaken, and is the problem elsewhere?

Thank you for understanding!

+3
source share
3 answers

, , cookie. , cookie (). cookie . . , , async.

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
   .....AND returning the session content in a cookie.
Request B finishes.  Session is empty. 
   THIS one is setting blank session.
Request C starts.  Session is empty - not what I want. 
   Thats because B has set reset the session
+1

, ": id" .: id . , session [: user_id].

!

-1
source

Source: https://habr.com/ru/post/1748936/


All Articles