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
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.
Comments and changes to this ticket
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.
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...