321

Since upgrading to Rails 3.1 I'm seeing this warning message in my development log:

WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

What does this mean and how can I remove it? Is it a problem?

3
  • 1
    Same here, for me it is happening when it's a remote call via JS.
    – Tim Baas
    Commented Aug 25, 2011 at 12:27
  • 2
    I started getting this as soon as I upgraded to Ruby 1.9.3 today. Wasn't seeing it before. I think it must be due to changes in WEBrick in Ruby 1.9.3...
    – Tyler Rick
    Commented Nov 18, 2011 at 19:56
  • 50
    It is indeed a WEBrick issue. In the meantime, you could add the 'thin' gem to your Gemfile and boot Rails with that instead of WEBrick, e.g. rails s thin; Ta-da! No more warnings.
    – Scott
    Commented Dec 4, 2011 at 14:08

9 Answers 9

229

Asked the same question to one of Rails-Core's members:

https://twitter.com/luislavena/status/108998968859566080

And the answer:

https://twitter.com/tenderlove/status/108999110136303617

ya, it's fine. Need to clean it up, but nothing is being hurt.

8
  • 9
    fyi, if the messages bother you, as a workaround you can use thin (add gem 'thin' to your gemfile, start your server using rails server thin). (oops, just noticed that @Scott Lowe already said this above.) Commented Dec 26, 2011 at 19:33
  • 280
    I find this annoying when these kinds of things are put in the category of "nothing is being hurt". Just the fact that thousands of people are wasting time having to figure out what is going on is enough to dispute that. Commented Apr 2, 2012 at 12:36
  • 16
    @KenThompson the problem is Webrick, not Rails. Webrick do not support keep-alive connections and thus raises the warning/issue we're seeing. It is recommended you use a proper/better webserver (like thin or passenger standalone) for web. Upcoming versions of Ruby will fix this issue. Commented Apr 4, 2012 at 23:16
  • 3
    The webrick server on our development PC renders the same .js.erb file twice. The twice rendering problem disappears on our production server which is running nginx. So this is a REAL problem in cases like ours.
    – user938363
    Commented Apr 24, 2012 at 15:26
  • 2
    The answer should contain the content of the twitter posts instead of links.
    – Pedro Rolo
    Commented Jul 15, 2016 at 14:26
78

The following patch solved the problem in my case; no more warnings for me.

204_304_keep_alive.patch

Just edit the file httpresponse.rb at line 205 as shown at the link above; in fact the link shows a correction made to a future release of Ruby.

I'm using rails 3.2.0 on ruby 1.9.3-p0 installed through RVM as a single user. So the location in my case is:

~/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb

The location of the file to be altered differs depending on the type of installation, RVM or not, or even multi-user or single user, so I'm just giving the last part of it:

.../ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb

I hope this can be helpful to someone.

EDIT: This is the link to the commit that altered the line in question in the trunk branch of ruby project.

2
  • I'm using debian squeeze, apt installed ruby version 1.9.3p194, and this problem still occurs. Ruby is dated 2012-04-20 and tenderlove's patch is dated Tue Dec 13 07:30:14 2011, but this still occurs.
    – kristianp
    Commented Jan 1, 2013 at 2:11
  • On Debian squeeze, RVM-installed Ruby version 1.9.3-p327 WEBrick still gives these problematic warnings. Commented Feb 26, 2013 at 15:00
56

Just explicitly adding the Gem to the Gemfile got rid of the warning messages for me:

group :development do
  gem 'webrick', '~> 1.3.1'
end
2
  • 5
    Yes, for me, too. A clue to why this works may be in Feature #5481 Gemifying Ruby standard library: "Because of 'fake gems', the new files of a stdlib installed by 'gem update' are ignored unless a user writes gem ['webrick'] explicitly." Commented Feb 26, 2013 at 17:23
  • 2
    This is so much better than 'ignore it', or 'patch webrick'. Thank you!
    – nessur
    Commented Oct 2, 2013 at 15:35
54

You can also use Thin instead of the default Webrick. Add this to Gemfile gem 'thin'

then rails s thin will use thin, and the warning will disappear.

3
  • Yes. This is what I've ended up doing in more recent months. Ryan Bates also mentioned in a recent Railscast.
    – Nate Bird
    Commented Apr 20, 2012 at 17:41
  • 1
    @cam song: almost correct: rails s thin will use thin instead of Webrick, and the warn will disappear. Commented Aug 3, 2012 at 3:55
  • 1
    I put thin in development group. Rails 4 seems picking it up automatically when running rails s
    – draw
    Commented Aug 7, 2013 at 5:04
15

If you're using .rvm, do this to fix it...

As mentioned by João Soares, all credits to him, this is what you can do if you wan't to get rid of this warning on development.

  1. Use your favorite editor to open this file:

    ~/.rvm/rubies/<ruby-version>/lib/ruby/1.9.1/webrick/httpresponse.rb
    
  2. Go to the line that contains this(for me it was really line 206):

    if chunked? || @header['content-length']
    
  3. Change it, taken from this patch, to this:

    if chunked? || @header['content-length'] || @status == 304 || @status == 204
    
  4. Save the file and eventually restart your rails server

0
12

This problem has been fixed in Ruby's trunk branch with this commit to webrick.

You can edit this particular webrick file similarly in your setup. The approximate location can be found by:

gem which webrick

To actually edit the file:

nano \`ruby -e"print %x{gem which webrick}.chomp %Q{.rb\n}"\`/httpresponse.rb

(Or instead of nano, use your favorite editor.)

2
  • My fancy command line above (humorously including the editor, nano) was lifted without attribution and copyrighted on the RailsRock site here. Commented Apr 23, 2013 at 17:59
  • The backticks probably shouldn't be escaped. So it should really be: nano `ruby -e"print %x{gem which webrick}.chomp %Q{.rb\n}"`/httpresponse.rb. Commented Nov 5, 2019 at 0:40
5

JRuby version: If you're using .rvm, do this to fix it...

As mentioned by João Soares and Kjellski, this is what you can do if you want to get rid of this warning on development and you are using JRuby.

  1. Use your favorite editor to open this file:

    ~/.rvm/rubies/jruby-<version>/lib/ruby/<1.8 or 1.9>/webrick/httpresponse.rb
    
  2. Go to the line that contains this (for me it was line 205):

    if chunked? || @header['content-length']
    
  3. Change it, taken from this patch, to this:

    if chunked? || @header['content-length'] || @status == 304 || @status == 204
    
  4. Save the file and eventually restart your rails server.

1
  • @schwabsauce Except for the first instruction, the rest is not JRuby-specific; it helps locate the file. The other instructions are repeated for clarity.
    – ckhatton
    Commented May 7, 2013 at 15:53
3

Another workaround that removes the offending line from webrick. It's just not that useful:

cd `which ruby`/../../lib/ruby/1.9.1/webrick/ && sed -i '.bak' -e'/logger.warn/d' httpresponse.rb

(you may need to sudo)

3

Add

config.middleware.use Rack::ContentLength

to your application.rb file, and the warning will disappear even with webrick. This will also set Content-Length properly in production when rendering a json or text response.

1
  • I love the idea of actually solving the problem instead of hiding it with the keep-alive patch. Unfortunately, this suggestion just spit out twice as many of the warnings.
    – labyrinth
    Commented Jun 28, 2016 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.