Adam
2009-Aug-04 07:10 UTC
First request after environment load always has an empty body (2.3.3)
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. Similarly, a web site with several Mongrels could easily GET from one, and then POST to another, newly restarted, Mongrel. 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. I created a ticket at https://rails.lighthouseapp.com/projects/8994/tickets/2993-first-request-will-always-have-an-empty-body-due-to-delayed-load-of-action_controllercgi_process