# Maddening error: "marshal data too short"

**URL:** <https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359>\
**Category:** Rails\
**Created:** [August 7, 2009, 9:25am UTC](https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359 "2009-08-07T09:25:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyle\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kyle\_F](https://www.ruby-forum.com/u/Kyle_F)\
**Post date:** [August 7, 2009, 9:25am UTC](https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359/1 "2009-08-07T09:25:05Z")

</div>

I’m occasionally getting an odd Rails error that says “marshal data  
too short”.

Our entire web app seems to be working fine – we can post forms, save  
models, redirect, login, etc, without any problems. Then out of  
nowhere a request will result in the 500 error “marshal data too  
short”. Subsequent requests to _any_ page then result in the same  
error – even pages that were previously working.

The only way to (temporarily) make the problem go away is to `rake db:sessions:clear` – but I need to determine what the root cause is.

Some forum posts led me to believe the `data` column in the Rails  
sessions table was too short. I have it as ‘text’, which should  
support roughly 65,000 characters. The data in my actual session is  
only 305 characters, so I doubt this is the problem.

It’s basically impossible for me to replicate the problem exactly, as  
I have no idea what’s triggering it. If anyone could shed some light  
on how I could investigate further, I’d be ever so thankful 🙂

Kyle.

---

<div class="post-metadata">

**Author:** ![Kyle\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kyle\_F](https://www.ruby-forum.com/u/Kyle_F)\
**Post date:** [August 7, 2009, 9:28am UTC](https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359/2 "2009-08-07T09:28:01Z")

</div>

On Aug 7, 12:07 am, Kyle F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Some forum posts led me to believe the `data` column in the Rails  
> sessions table was too short. I have it as ‘text’, which should  
> support roughly 65,000 characters. The data in my actual session is  
> only 305 characters, so I doubt this is the problem.

I wouldn’t use a text column (use a blob column instead). If you use a  
text column then the database is expecting the content to be actual  
text. If it’s expecting utf8 and you give it invalid utf8 data it may  
well truncate your data at the first occurrence of illegal data.

Fred

---

<div class="post-metadata">

**Author:** ![Kyle\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kyle\_F](https://www.ruby-forum.com/u/Kyle_F)\
**Post date:** [August 7, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359/3 "2009-08-07T17:03:25Z")

</div>

> I wouldn’t use a text column (use a blob column instead).

AFAIK, we just used the default table and column types that Rails  
creates for sessions. Why in the world wouldn’t it use a more robust  
column type in the first place?

---

<div class="post-metadata">

**Author:** ![Kyle\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kyle\_F](https://www.ruby-forum.com/u/Kyle_F)\
**Post date:** [August 7, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359/4 "2009-08-07T17:08:04Z")

</div>

Kyle F. wrote:

> > I wouldn’t use a text column (use a blob column instead).
> 
> AFAIK, we just used the default table and column types that Rails  
> creates for sessions. Why in the world wouldn’t it use a more robust  
> column type in the first place?

Kyle,

How much stuff are you storing in user sessions?  
ilan

---

<div class="post-metadata">

**Author:** ![Kyle\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kyle\_F](https://www.ruby-forum.com/u/Kyle_F)\
**Post date:** [August 7, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359/5 "2009-08-07T17:43:49Z")

</div>

On Aug 7, 4:02 pm, Kyle F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I wouldn’t use a text column (use a blob column instead).
> 
> AFAIK, we just used the default table and column types that Rails  
> creates for sessions. Why in the world wouldn’t it use a more robust  
> column type in the first place?

What session store are you using? if it’s the default activerecord  
store then it’s base64 encoding everything and so you can ignore my  
comment.  
If I were you I’d instrument the session store code to log when it  
updates the sessions table and when it loads from it: if you can spot  
how the data changes when this error occurs that may be a valuable  
clue into why it changes.

Fred

---

<div class="post-metadata">

**Author:** ![Kyle\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kyle\_F](https://www.ruby-forum.com/u/Kyle_F)\
**Post date:** [August 7, 2009, 5:46pm UTC](https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359/6 "2009-08-07T17:46:45Z")

</div>

> How much stuff are you storing in user sessions?

We’re storing basic authentication info and then a little object that  
holds some data to pre-populate a form. I wouldn’t think that’s too  
much data to be storing in a session…

The last time I encountered this error I ran `SELECT LENGTH(data) FROM sessions;` and it returned “305”, which from what I understand is the  
number of bytes – well below the max length of TEXT columns.

I’m going to try using a BLOB, and we’ll see if the error occurs  
again. However, I have a feeling the problem isn’t too much data in  
the session, so feel free to post any other suggestions 🙂

---

<div class="post-metadata">

**Author:** ![Kyle\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kyle\_F](https://www.ruby-forum.com/u/Kyle_F)\
**Post date:** [August 7, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/maddening-error-marshal-data-too-short/173359/7 "2009-08-07T17:48:30Z")

</div>

Fred: we’re using activerecord. Perhaps I’ll try adding some code to  
log what’s happening behind the scenes.
