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.