Wednesday, August 12, 2009

Rails Dump Format Error Mystery

One day your rails app stopped working and the only clue you saw from console log was the 'Dump format error' exception. It started when users logged in and visit a page and received the dreaded HTTP 500 internal error. After it happened, they could no longer browse to any page of your rails app, what should you do?

The first I did was trying to find out what caused this problem, once this exception was thrown, I couldn't reload the page or visit path of the app as it kept showing the 500 error. I knew it must have something to do with session. So I closed the web browser and reopened it, it worked but as long as I visited the problematic page, the same problem happened again.

What does the dump format error mean? After a quick google, I found out that it was thrown when there was a problem with marshalling/unmarshalling objects. Who did that? Why did rails send objects over the wire? Then I looked at the controller's action and found out this line:

flash[:object_with_error] = user

So someone put an object to the flash which was in turn kept in a cookie session data store. Since the cookie is limited to 4K bytes, storing I suspect that the cookie was full and the data couldn't be fully marshalled. That caused the dump format error.

The flash hash usually contains a message to be displayed in a view. Why did someone put a binary object (in this case, it's an activerecord model) in it? I found out later that it was because the action took a form, verify the data, and redirect to a different action. If there were validation errors, it stored the activerecord with errors in the flash so that the redirecting action would be able to fill out the form with errors.

However, we should avoid storing binary objects in flash (and in cookies) since the object graph can easily exceed the cookie size limit. Instead, if we really need to pass temporary data between sessions, pass only string values. In this case, pass only the form data and error messages.

Problem solved, lesson learned. Case closed.

3 comments:

joel said...

I am having this kind of error although I am not sure I understood what you did to get it fixed.

This problem started happening to me when I ported my application to rails 3.1 ... looks like there is something to do with my session store ... but I can't really pin point what is wrong now.

I posted my issue on http://stackoverflow.com/questions/7528080/argumenterror-dump-format-error-for-symbol0x6f-when-upgrading-to-rails-3-1 if you wish to enlighten me about what I should be doing. Thanks for the post. That's the only one I found on the subject.

Constant Meiring said...

Seems you just clear your browser cookies for the rails app, ie. all cookies for localhost.

Matt Morrison said...

Just hit this same issue and very much appreciate the assistance. Clearing the cache worked like a charm.