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
Reported by Adam Milligan | August 4th, 2009 @ 08:06 AM | in 2.3.6
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
-
Michael Koziarski August 4th, 2009 @ 10:36 PM
- 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?
-
Adam Milligan August 6th, 2009 @ 02:40 AM
Here you go. I worked from the 2-3-stable branch since all of the CGI stuff is gone in master.
-
Fjan September 9th, 2009 @ 03:20 PM
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.
-
Adam Milligan September 10th, 2009 @ 03:41 AM
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...
-
Jeremy Kemper September 11th, 2009 @ 11:04 PM
- Milestone changed from 2.3.4 to 2.3.6
[milestone:id#50064 bulk edit command]
-
Yehuda Katz (wycats) October 21st, 2009 @ 08:06 PM
- State changed from new to resolved
Seems to be resolved in 2-3-stable.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 3155 First POST to a fresh production server loses form data I just noticed this is a dupe of [2993][https://rails.lig...
- 3155 First POST to a fresh production server loses form data Dupe of #2993