Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

First request will always have an empty body due to delayed load of action_controller/cgi_process

#2993

After you restart a Mongrel server the first request will always fail to have the correct HTTP request body. The reason for this is the override of CGI#initialize_query method by ActionController in the cgi_ext/query_extension.rb file.

Specifically, the overridden function does not read from the IO stream representing the request body, while the original method does. This means the original method leaves the read pointer at the end of the buffer, so any subsequent reads return nothing.

The problem arrises on server restart because loading the Rails environment doesn't preload the cgi_ext.rb or cgi_ext/*.rb files by default. The query_extension.rb file doesn't load until after the first request body has been processed, so for that first request the original method gets invoked, and the request appears to have no body.

This isn't generally a problem for web sites, since the first request is nearly invariably a GET request with an empty body (for the homepage). However, for a web service, if the first request after a server restart or environment reload is a POST or PUT, then that request will fail, and fail messily.

I realize everything CGI has been deprecated, which is why I haven't provided a patch, but I don't really know how long this could be a problem. I'm curious to hear if this won't be a problem going forward (i.e. as of 2.3.4), or if it should be patched. Finding the problem was a challenge, but the solution is easy: just require action_controller/cgi_ext.rb when ActionController loads.

Reported by Adam Milligan · August 4th, 2009 @ 08:06 AM

State: resolved
Milestone: 2.3.6
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Michael Koziarski
    • Milestone set to 2.3.4

    We should take care of this in time for 2.3.4.

    Would you care to submit a patch to preload those files?

    August 4th, 2009 @ 10:36 PM

  2. Adam Milligan
    Adam Milligan

    Here you go. I worked from the 2-3-stable branch since all of the CGI stuff is gone in master.

    August 6th, 2009 @ 02:40 AM

  3. Fjan
    Fjan

    Thanks a million Adam! This bug had me scratching my head for quite a while. I think it's quite unacceptable that there is a small chance of a failing POST after a restart: the user might have been in the middle of a checkout process or might lose a long blog entry he had just been typing.

    Too bad a fix didn't make it into 2.3.4, but it is easy enough to monkey patch by doing the require in the environment.rb. By the way: somehow the problem only exhibits itself when the Mongrels are proxied behind Apache, it doesn't happen in my development environment which made it hard to find.

    September 9th, 2009 @ 03:20 PM

  4. Adam Milligan
    Adam Milligan

    Fjan, I'm pleased that this helped you out. My goal with things like this is to create a Google result for people who run into the same problem. If you're interested in more of the details I wrote a bit about it here: http://pivotallabs.com/users/amilligan/blog/articles/979-rails-requ...

    September 10th, 2009 @ 03:41 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  6. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to resolved

    Seems to be resolved in 2-3-stable.

    October 21st, 2009 @ 08:06 PM