Posted on August 20, 2010 - Filed Under RubyOnRails
I was stuck for a while when application maintained by me have started to throw
ArgumentError: marshal data too short errors in random places When user have encountered that problem then it was unable to use application at all.
Logs were showing that it happens when Rails was trying to create session object. Session store was in ActiveRecord and
sessions table was not corrupted.
After watching that for a while it have shown that places in code were this exception was thrown were random but there was pattern. Page visited before was common in each case.
It have turned out that application was storing in session whole ActiveRecord object. Like:
session[:some_info] = @variable
And later we were trying to use that way:
@variable = Model.find session[:some_info]
Due to Rails magic AcitiveRecord’s find when provided with AR object will return that object (of course if is the same model). Code was working well (maybe not very effectively since You should avoid storing large objects in session) until object stored that way started to grow. Application was collecting some data and amount of data stored have grown to that point that after
Marshal.dump size of string was more than 64 kB. And this is default size of
text field used to store session data in MySQL.
When You try to store too much data in
text field in MySQL, excessive data is being truncated, so
Marshal.load throws that exception.
To have that error solved is enough to store just object id in session (
session[:some_info] = @variable.id).
Popularity: 48% [?]
Hits for this post: 51584
- Session store – don’t get trapped
- Skinny controller, fat model and Facebook
- Migrations headache
- Dont use sessions table in rails
- Interacting with Facebook without user