# Session madness \*argh\*

**URL:** <https://www.ruby-forum.com/t/session-madness-argh/138486>\
**Category:** Rails\
**Created:** [May 23, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/session-madness-argh/138486 "2008-05-23T15:58:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [May 23, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/session-madness-argh/138486/1 "2008-05-23T15:58:25Z")

</div>

Environment: Rails 2.0.2; cookie-based sessions (as far as I know… I  
added the two appropriate lines to environment.rb)

Problem: I’m getting inconsistent results storing things in the  
session.

For example, I create a data structure like this: people = [[“bob”,133]  
[“joe”,145]]  
Then I set session[:people] = people

I’ve got sections of my current page with some JavaScript elements  
(YUI DataTables) that, based on some user actions, generate new  
requests to my controller.

In my actions in the controller, I’m expecting session[:people] to be  
valid (because I initialized it with people as described above).  
However, I’m seeing inconsistent behaviors with that session. Some  
data is persisting from one call to the next, but others are not.

My session itself isn’t being cleared or reset, because if it were I’d  
lose my “authentication” and be redirected to my login page.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [May 23, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/session-madness-argh/138486/2 "2008-05-23T16:03:46Z")

</div>

On 23 May 2008, at 14:57, michael\_teter wrote:

> Then I set session[:people] = people
> 
> I’ve got sections of my current page with some JavaScript elements  
> (YUI DataTables) that, based on some user actions, generate new  
> requests to my controller.

Do you ever have concurrent requests? The following can happen

request 1 loads session  
request 2 loads session  
request 2 sets session[:people]  
request 2 saves session (ie sends the cookie back to the user in the  
case of the session store  
request 1 saves session

Request 1 never knew about the changes made by request 2 and will  
happily stomp all over the changes made by request 2. I wrote this up  
in more detail a while ago  
([钛刻专家——官网](http://www.texperts.com/2007/05/01/race-conditions-in-rails-sessions-and-how-to-fix-them/)  
)

Fred

---

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [May 23, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/session-madness-argh/138486/3 "2008-05-23T16:24:21Z")

</div>

Oh wow, I didn’t realize that’s how it would work underneath. That  
could be my problem.

My page is creating a dozen divs, each of which contains a YUI  
DataTable, and each of which makes its own (concurrent) request to the  
controller for data (which I’m rendering out as XML to feed into the  
DataTable). It’s not my choice to make something this complex, but  
that aspect is beyond my control.

There are multiple requests to my controller-\>action going on.

Forgive my confusion here, but my prior experience is with Java  
(Servlet/JSP) sessions. My assumption here was that by saying  
session[:something] = x, that would go into memory on the server and  
persist until I explicitly reset the session or some session timeout  
occurred. My Googling on Rails sessions hasn’t led me to what I feel  
is a complete, definitive document on the subject. I’d love if  
someone had a good reference for me.

Thanks,  
Michael

On May 23, 9:02 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [May 23, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/session-madness-argh/138486/4 "2008-05-23T16:54:16Z")

</div>

On 23 May 2008, at 15:23, michael\_teter wrote:

> Oh wow, I didn’t realize that’s how it would work underneath. That  
> could be my problem.
> 
> My page is creating a dozen divs, each of which contains a YUI  
> DataTable, and each of which makes its own (concurrent) request to the  
> controller for data (which I’m rendering out as XML to feed into the  
> DataTable). It’s not my choice to make something this complex, but  
> that aspect is beyond my control.

Sounds like it could be your problem.

> There are multiple requests to my controller-\>action going on.
> 
> Forgive my confusion here, but my prior experience is with Java  
> (Servlet/JSP) sessions. My assumption here was that by saying  
> session[:something] = x, that would go into memory on the server and  
> persist until I explicitly reset the session or some session timeout  
> occurred. My Googling on Rails sessions hasn’t led me to what I feel  
> is a complete, definitive document on the subject. I’d love if  
> someone had a good reference for me.

There are several session stores in rails, the default is the cookie  
store. With all the other stores, your session cookie just contains  
some unique identifier which enables rails to locate the correct file/  
database row/ memcache entry etc… With the cookie store the entirety  
of the session is stored in the cookie.  
But apart from that what you say about sessions is basically true.  
What is unfortunate is the behaviour when there are multiple requests  
from the same user.  
the situation could probably be improved creating sessions on demand  
and only writing them back if the session has changed (there’s a  
ticket proposing this and other changes be made  
[http://rails.lighthouseapp.com/projects/8994/tickets/92-feature-more-efficient-session-handling](http://rails.lighthouseapp.com/projects/8994/tickets/92-feature-more-efficient-session-handling)  
)

Fred
