This project is archived and is in readonly mode.
Mongrel doesn't work with rails 2.3.8
-
...Paul
I'm getting a similar problem. script/server works fine, all good, but when I push out to my servers which use mongrel_cluster, when setting a cookie (such as after a login), I get the following:
Error calling Dispatcher.dispatch #<NoMethodError: undefined method '[]' for nil:NilClass>
.../mongrel/1.1.5/lib/mongrel/cgi.rb:108:in 'send_cookies'ruby 1.8.7 (2009-04-08 patchlevel 160) [amd-freebsd6]
rack (1.1.0, 1.0.1)
mongrel (1.1.5)
mongrel_cluster (1.0.5)I did a rake rails:update, but did not note any changes resulting.
-
Edgars Beigarts
In some apps I also had the same issue that Paul has.
-
Evgeny
I have the same problem on hosting, even worse. When I create any app with a single scaffolded page the Mongrel returns a page as plain text. Here is the link to test page (won't be here for long): http://morningmare.com/users
It seems like Mongrel crashes trying to send cookies. The response header has no Content-Type and it look like it's incomplete:
curl 127.0.0.1:12002/users -I
HTTP/1.1 200 OK
Connection: close
Date: Wed, 26 May 2010 09:24:44 GMT
Content-Length: 0The Mongrel log shows the same error as Paul's:
The error occurred while evaluating nil.[]>
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/cgi.rb:108:insend_cookies' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/cgi.rb:136:inout' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/http_response.rb:65:instart'ruby 1.8.7, mongrel 1.1.5
Any help would be appreciated!
-
...Paul
- Tag changed from 2.3.8, rack to 2.3.8, mongrel_cluster, rack
Given that I know mongrel_cluster has fallen somewhat out of favor, I decided this might be the time to check out using Nginx with Passenger. Sure enough, the app works fine in that environment, so it's definitely some new incompatibility with mongrel_cluster 1.1.5...
-
Edgars Beigarts
The problem has been solved in http://github.com/rack/rack/commit/f6f3c60938ea3b08f3292a2480e6753c...
Rack (HEAD and 1.0):
>> h = Rack::Utils::HeaderHash.new => {} >> h.include?('Set-Cookie') => false >> h['Set-Cookie'] => nil >> h.include?('Set-Cookie') => falseRack (1.1):
>> h = Rack::Utils::HeaderHash.new => {} >> h.include?('Set-Cookie') => false >> h['Set-Cookie'] => nil >> h.include?('Set-Cookie') => true -
Edgars Beigarts
The problem has been solved in http://github.com/rack/rack/commit/f6f3c60938ea3b08f3292a2480e6753c...
Rack (HEAD and 1.0):
>> h = Rack::Utils::HeaderHash.new => {} >> h.include?('Set-Cookie') => false >> h['Set-Cookie'] => nil >> h.include?('Set-Cookie') => falseRack (1.1):
>> h = Rack::Utils::HeaderHash.new => {} >> h.include?('Set-Cookie') => false >> h['Set-Cookie'] => nil >> h.include?('Set-Cookie') => true -
...Paul
How does that solve the problem, given that I have rack 1.1.0? (Rails 2.3.8 will complain loudly if you don't have that revision...)
-
Edgars Beigarts
Actually this solves only the first problem with "nil.split". :(
-
Jonny_K
- Tag changed from 2.3.8, mongrel_cluster, rack to 2.3.8, mongrel, mongrel_cluster, rack
-
demersus
I can confirm this issue is still happening. As of today. I just tried to deploy an app to a server with Rails 2.3.8 installed on it. It is running mongrel to serve the rails app.
My log displays the exact same error as Edgar's first post.
I keep getting a page with the following text: You are now being redirected
Whenever the rails application sends a redirect. Instead of my browser redirecting.Also, if I try to submit a form it will kick back an error about an invalid authenticity token
I was able to get my app deployed without error by downgrading to Rails 2.3.5
-
Togg
Same issue. Works for script/server, but server as cluster (for apache balancer) fails with the exact same error as above.
So is there any patch available yet?
-
Mike C
Same issue. Since nothing appears to have been done about it, I dug into it myself. Here is what I found:
In file
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/cgi.rb
method
def send_cookies(to)
First, this method is half-baked. It has a hash variable called options which may never have been called until now. It is getting called now because the "cookie" is always a string -- never an Array nor Hash (at least in my tests). I'm not sure what is correct for this, but after tinkering around a bunch, I got this to work:
when String cookies = cookie.split.map { |a| a[-1..-1]==';'?a[0..-2]:a } cookies.each do |c| if c.split('=').count == 2 to['Set-Cookie'] = c else to['Set-Cookie'] = c+'=' end end else to['Set-Cookie'] = cookie.to_s endI believe this was the code it replaced (I didn't save my original file -- bad me):
else to['Set-Cookie'] = options['cookie'].to_s endI don't know if it is necessary to remove the ';'. I did and it works. I'll accept an official patch, but I'm done messing with it for now.
-
Scott Taylor
Seems to be fixed in fauna/mongrel:
http://github.com/fauna/mongrel/blob/master/lib/mongrel/cgi.rb#L108
-
bshand
The fix currently in fauna/mongrel does not resolve this issue.
Mike C was on the right track, but was too-aggressively splitting apart cookies.
I attach rails238_mongrel115_patch.diff which resolves this for me -- I've tested this in Windows XP SP3 with all permutations of Ruby 1.8.6 vs 1.8.7, Rails 2.3.5 vs 2.3.8, on our in-house application.
As described by others, this issue is only apparent if you start mongrel before Rails, e.g. as a service or using
$ mongrel_rails startHere are telnet logs that I used as a manual test case to verify my fix:
Expected behaviour (Rails 2.3.5, Ruby 1.8.6 without fix):
$ telnet localhost 3000 GET /login HTTP/1.0
HTTP/1.1 200 OK Connection: close Date: Thu, 10 Jun 2010 10:43:02 GMT Set-Cookie: BALANCEID=balancer127-0-0-1_b73a.x4000; path=/; expires=Thu, 10-Jun-2010 22:43:03 GMT Set-Cookie: _era_session_1968=e36001c1bc0008535135dc9bf5b931ac; path=/; HttpOnly Status: 200 ETag: "49b3e5defddedb74868b40692f24ede1" X-Runtime: 593 Cache-Control: no-store Server: Mongrel 1.1.5 Content-Type: text/html; charset=utf-8 Content-Length: 4399
<!DOCTYPE HTML SYSTEM "http://:/dtd/era.dtd?1261589568">Actual behaviour (Rails 2.3.8, Ruby 1.8.6 without fix):
$ telnet localhost 3000 GET /login HTTP/1.0
HTTP/1.1 200 OK Connection: close Date: Thu, 10 Jun 2010 10:41:04 GMT Content-Length: 4399
<!DOCTYPE HTML SYSTEM "http://:/dtd/era.dtd?1261589568">Fixed behaviour (Rails 2.3.8, Ruby 1.8.6 with fix):
$ telnet localhost 3000 GET /login HTTP/1.0
HTTP/1.1 200 OK Connection: close Date: Thu, 10 Jun 2010 10:36:54 GMT Set-Cookie: BALANCEID=balancer127-0-0-1_dd1a.x4000; path=/; expires=Thu, 10-Jun-2010 22:36:55 GMT Set-Cookie: _era_session_1468=a62eda630f1846d621f100470fda693d; path=/; HttpOnly Status: 200 X-Runtime: 31 ETag: "c0e26ca3e1ca46f354d4bd134e0ef1c8" Cache-Control: no-store Server: Mongrel 1.1.5 Content-Type: text/html; charset=utf-8 Content-Length: 4399
<!DOCTYPE HTML SYSTEM "http://:/dtd/era.dtd?1261589568"> -
Nat Brown
for those of you still having issues, you do need to apply both the rack fix and the diff bshand posted.
or if updating rack is tough, just make this change manually (sorry no diff) to gems/actionpack-2.3.8/lib/action_controller/cgi_process.rb at around :51.if headers.include?('Set-Cookie') headers['cookie'] = headers.delete('Set-Cookie').split("\n") endbecomes
if headers['Set-Cookie'] headers['cookie'] = headers.delete('Set-Cookie').split("\n") end -
...Paul
The simple fix natbro provided seems to have fixed the cookie issue, but redirects are still broken. Any redirect done results in page saying "You are being redirected" -- the redirect header, obviously not being transmitted in the response.
-
Matthew Denner
I don't know if this is relevant but in our environment (mongrel 1.1.5, rack 1.1.0, rails 2.3.8) the issue is actually with the 'header' method in lib/mongrel/cgi.rb. The Rack HeadersHash class appears to have different behaviour depending on whether you access a header directly or iterate over everything. In the 'header' method the latter is used, which causes @head['cookie'] to be a string, even though Rails sets an Array. By adding the following two lines:
@head['cookie'] = options['cookie'] if options['cookie'] options.delete('cookie')before:
REMOVED_KEYS.each {|k| options.delete(k) }in the 'headers' method (about line 80-ish), we suddenly get not only the cookies but all of the headers our application sets, including the 'Location' header.
-
Matthew Denner
A little bit more investigation reveals that something really is odd with options['cookie'] in the 'header' method as it's an array of arrays, and the elements are not quite split properly (with a leading '\n'). The simplest solution for our environment was to monkeypatch Mongrel using a Rails initializer:
class Mongrel::CGIWrapper def header_with_rails_fix(options = 'text/html') @head['cookie'] = options.delete('cookie').flatten.map { |v| v.sub(/^\n/,'') } if options.class != String and options['cookie'] header_without_rails_fix(options) end alias_method_chain(:header, :rails_fix) end if Rails.version == '2.3.8' and Gem.available?('mongrel', Gem::Requirement.new('~>1.1.5')) and self.class.const_defined?(:Mongrel) -
Andrew Selder
+1 for Matthew's monkey patch above.
This actually looks like a bug in Mongrel, specifically their handling off cookies when @head['cookies'] is a string
In lib/mongrel/cgi.rb, I believe there is a bug in send_cookies. When @head['cookies'] is an array or a hash, it iterates over the values and to_s's them, but when it's something else and tries to to_s the value of options['cookie'].
The simple change of modifying line 110 of cgi.rb
fromto
to['Set-Cookie] = cookie.to_s
seems to fix this problem as well.
-
Andrew Selder
Seems to have stripped the initial value of line 110 above
to['Set-Cookie] = options['cookie'].to_s
-
Luis Lavena
Hello,
As commented in Mongrel issue tracker:
http://github.com/fauna/mongrel/issues#issue/5
Due the nature of mongrel_rails, it will be great get a fix applied that works with older versions of Rails.
Up to now, no patch has been proposed for this.
-
Ken Collins
I took the throw it all at the wall approach and so far so good. I added this mongrel.rb to my config/initializers
-
bshand
Ken Collins' mongrel.rb initializer works for me -- on Windows with RubyGems 1.3.7 and Rails 1.8.6 (OneClick installer) / Rails 1.8.7 (RubyInstaller).
It fails with RubyGems 1.3.5, unless I make the following addition patch to his mongrel.rb:
$ diff mongrel.rb.orig mongrel.rb 2c2 < if Rails.version == '2.3.8' && Gem.available?('mongrel', Gem::Requirement.new('~>1.1.5')) && self.class.const_defined?(:Mongrel) --- > if Rails.version == '2.3.8' && Gem.available?('mongrel', '~>1.1.5') && self.class.const_defined?(:Mongrel)To verify that the fix works with multiple cookies, here's my telnet log, as above.
$ telnet localhost 3000 GET /login HTTP/1.0 HTTP/1.1 200 OK Connection: close Date: Tue, 27 Jul 2010 09:04:59 GMT Set-Cookie: BALANCEID=balancer127-0-0-1_37fc.x4000; path=/; expires=Tue, 27-Jul-2010 21:05:00 GMT Set-Cookie: _era_session_4964=c0ee034da3546146cfa92021312c8892; path=/; HttpOnly Status: 200 X-Runtime: 688 ETag: "170e02320aab49443d3f7235ef06feb7" Cache-Control: no-store Server: Mongrel 1.1.5 Content-Type: text/html; charset=utf-8 Content-Length: 4262 <!DOCTYPE html> -
timboisvert
Ken Collins' and bshand's solution worked for me. Thanks!
-
amnesia7
I'm new to RoR and my app is running from a shared host so bear with me.
I've used the code by Ken Collins in a /config/initializers/mongrel.rb file but I'm still getting the issue.
I've tried changing the first line of the file as per bshand's suggestion but no joy either, so I put it back to Ken's version again.
My app (based on RefineryCMS) works fine on my localhost. However, on the shared host the frontend of my app runs ok (http://spectrumplastics.net:12001/) but when I try to go to the login area (http://spectrumplastics.net:12001/refinery) I get a page using Firefox that says "You are being redirected." If I click the link to go to session/new then the login fails. I assume a cookie should be set somewhere along the way.
Here's what I can gather:
mongrel 1.1.5
rails 2.3.8
rubygems-update 1.3.7Any help would be much appreciated.
Col
-
Damien White
- Tag changed from 2.3.8, mongrel, mongrel_cluster, rack to 2, mongrel, mongrel_cluster, rack
Ken,
Thanks! Your solution worked great.-Damien
-
Damien White
- Tag changed from 2, mongrel, mongrel_cluster, rack to 2.3.8, mongrel, mongrel_cluster, rack
-
etlund
Ken's mongrel.rb initializer works for me on my Engine Yard cloud apps.
-
amnesia7
My app seems to be working now using Ken's code.
For some reason I had to comment out the "if" statement:
if Rails.version == '2.3.8' && Gem.available?('mongrel', Gem::Requirement.new('~>1.1.5')) && self.class.const_defined?(:Mongrel)
and the associated "end" at the bottom.
This line of code didn't seem to agree with the way my shared host is setup.
-
Eduardo
- Tag changed from 2.3.8, mongrel, mongrel_cluster, rack to 2, mongrel, mongrel_cluster, rack
Ken Collins' solution did no work for me. This error is being shown whenever a redirect_to is being executed: "ActionController::RoutingError (No route matches "/404.shtml" with {:method=>:get}):
config/initializers/mongrel.rb:63:indispatch_cgi'"RubyGems 1.3.7
Rails 2.3.8
mongrel_cluster 1.0.5
mongrel (1.1.5 ruby java x86-mingw32 x86-mswin32-60)
ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux]
rack 1.2.1 -
Mike Bethany
The great guys that put all the hard work into coming up with these fixes sometimes forget a few of us are new to all this and they assume a lot of steps. So, for the other complete ruby newbies like myself you have to do this:
- Save this ruby code in your app to config/mongrel.rb: http://gist.github.com/471663
- In your config/environment.rb under the "require 'boot'" line
file add:
require File.join(File.dirname(File.expand_path(FILE)), 'mongrel') - In that same file add this line in the Rails::initializer.run
block:
config.gem "mongrel"
Note: I've been adding the File.expand_path because that seems to fix a lot of "require 'blah_blah_blah' not found" bugs.
- Save this ruby code in your app to config/mongrel.rb: http://gist.github.com/471663
-
Mike Bethany
Major correction to my above statement:
- In your config/environment.rb at the very end of the
file add:
require File.join(File.dirname(File.expand_path(__FILE__)), 'mongrel')
- In your config/environment.rb at the very end of the
file add:
-
SUzB
Thanks Mike! Your solution (August 30th, 2010 @ 06:43 PM and @ 07:58 PM) worked for me
-
Lenart
Mikes solution can by simplified by creating mongrel.rb in config/initializers since Rails loads all the files from that folder automatically.
I also had to change first line of this patch according to bshand's comment.
Great work guys!
-
Steven Parkes
FWIW, this effects 2.3.9, too, but the mongrel.rb doesn't quite fit with 2.3.9 ...
-
Mauro Morales
As Lenart mentioned Mike's and bshand's comments fixed it for me to. Thank you guys! I was getting mental.
Also, like Eduardo I was getting a routing error but in my case it was my bad since I was redirecting the address without a slash / at the end of my redirection, adding it fixed the problem.
-
David Trasbo
- State changed from new to resolved
- Importance changed from to Low
Marking this as resolved for now.
If this is still an issue, please note it here and I'll reopen. Patches with fixes/failing tests are much appreciated.
-
Andrea Campi
- Tag changed from 2, mongrel, mongrel_cluster, rack to 2-3-stable, mongrel, mongrel_cluster, rack
-
annabels
My app ran fine on my development laptop using webrick, but when I deployed it to the production server with mongrel_rails it developed two bugs:
a. The content-type header of all the web pages it sent out was text/plain instead of text/html (which caused all pages to appear as black and white code in most browsers, and seemed to stop redirects working).
b. I had to turn off the forgery prevention/authentication cookies.
The Mike C, Lenart solution solved both issues (for Rails 2.3.8).
Here is an idiot's guide - to save anyone having to trawl through all posts above to work it out:
-
Download the file at http://gist.github.com/471663 and save to config/initializers/mongrel.rb in your application. (Then transfer to your production server obviously)
-
Run mongrel_rails cluster::restart on your server.
-
The first 2 steps solved both bugs for me - but if they don't make the change to mongrel.rb suggested by bshand above.
Simple - only took a few minutes
-
-
Jeff Kreeftmeijer
Automatic cleanup of spam.
-
zizilo
For some reason I had to comment out the "if" statement:
http://www.robesfr.com -
Woody
I was hesitant to monkey-patch w/o a better understanding of the problem. A summary of what I've found about the bug:
The OP's problem (calling nil.split) won't be resolved until rails 2 depends on a version of rack that has either https://github.com/rack/rack/commit/f6f3c60938ea3b08f3292a2480e6753... or https://github.com/rack/rack/commit/b937c01bd16243276b24845efea4119... in it (v1.2 or master, respectively). Until then, monkey patching will be necessary to get mongrel_rails working due to it's use of Mongrel::CGIWrapper (script/server does not "require 'mongrel/rails'", which uses Mongrel's CGI wrapper, which in turn uses rack, which in version 1.1 has a bug that chokes on rails' cgi handling of mongrel's 'Set-Cookie'). OR, Rails' cgi handling could not use headers.include?, but that's a work-around.
Also, FYI, the other bug mentioned here (calling nil.[]) is from a bug in Mongrel's CGI wrapper resolved 2 years ago, but still existing in Mongrel 1.1.5. Once the OP's bug is fixed, this case is hit because somewhere between Rails' mongrel-specific CGI code (see https://github.com/rails/rails/commit/b6e56efe) setting 'cookie' to an array and where mongrel looks at 'cookie' (https://github.com/fauna/mongrel/blob/rel_1-1-5/lib/mongrel/cgi.rb, 'send_cookie'), the 'cookie' header gets set back to a string, effectively nullifying that commit and triggering the old bug. I couldn't find where that happens, and won't be resolved until rails/rack/whatever doesn't un-array the 'cookie' header, or a new version of mongrel comes out.
The gist: Mongrel's CGI doesn't work in Rails 2.3.8+ w/o monkey patching 2 separate (old) bugs, one for Rack, and the other for Mongrel.
-
Roberto Aguilar
We spent a while researching this problem and ended up here to get the problem solved, but we have a quick note that hopefully will save others some time. When pulling the 'mongrel.rb' initializer file from Github, there are edits in the comments to address different versions of Rails as well as some incorrect syntax for a condition about the Mongrels which may give you an 'illformed requirement' error. A version of the gist that works for us can be found below. It has been tweaked to work for Rails versions ['2.3.8', '2.3.9', '2.3.10', '2.3.11'].
Thanks.
-
learn italian online
brick, but when I deployed Ray Ban 2140 it to the production server with mongrel_rails it developed two Ray Ban 3025 bugs:a. The content-type header of all the web pages it sent out wa

