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?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Nate Bird
  • 5,243
  • 2
  • 27
  • 37
  • 1
    Same here, for me it is happening when it's a remote call via JS. – Tim Baas Aug 25 '11 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 Nov 18 '11 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 Dec 04 '11 at 14:08

9 Answers9

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.

Jean
  • 825
  • 8
  • 17
Luis Lavena
  • 10,348
  • 1
  • 37
  • 39
  • 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.) – fearless_fool Dec 26 '11 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. – Mark Fraser Apr 02 '12 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. – Luis Lavena Apr 04 '12 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 Apr 24 '12 at 15:26
  • I put this in a development & test group in my Gemfile just to keep it isolated. – Michael Durrant Sep 01 '12 at 21:21
  • @AlexL: it is included, see the below the link. – Luis Lavena Jan 18 '13 at 15:26
  • wrt nothing being hurt - well for one it clutters up my output making it hard to see anything else that's relevant. But well glad to see a simple solution :) – msanjay Feb 06 '14 at 07:57
  • 2
    The answer should contain the content of the twitter posts instead of links. – Pedro Rolo Jul 15 '16 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.

MarkDBlackwell
  • 1,994
  • 18
  • 27
jasoares
  • 1,811
  • 17
  • 22
  • 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 Jan 01 '13 at 02:11
  • On Debian squeeze, RVM-installed Ruby version 1.9.3-p327 WEBrick still gives these problematic warnings. – MarkDBlackwell Feb 26 '13 at 15:00
57

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
ootoovak
  • 1,063
  • 9
  • 17
  • 5
    Yes, for me, too. A clue to why this works may be in [Feature #5481 Gemifying Ruby standard library](https://bugs.ruby-lang.org/issues/5481#note-1): "Because of 'fake gems', the new files of a stdlib installed by 'gem update' are [ignored](https://bugs.ruby-lang.org/issues/5481#note-36) unless a user writes gem ['webrick'] explicitly." – MarkDBlackwell Feb 26 '13 at 17:23
  • 2
    This is so much better than 'ignore it', or 'patch webrick'. Thank you! – nessur Oct 02 '13 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.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
Cam Song
  • 2,956
  • 1
  • 22
  • 18
  • Yes. This is what I've ended up doing in more recent months. Ryan Bates also mentioned in a recent Railscast. – Nate Bird Apr 20 '12 at 17:41
  • 1
    @cam song: almost correct: rails s thin will use thin instead of Webrick, and the warn will disappear. – fearless_fool Aug 03 '12 at 03:55
  • 1
    I put `thin` in `development` group. Rails 4 seems picking it up automatically when running `rails s` – draw Aug 07 '13 at 05: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

Kjellski
  • 925
  • 10
  • 24
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.)

geoff
  • 2,251
  • 1
  • 19
  • 34
MarkDBlackwell
  • 1,994
  • 18
  • 27
  • My fancy command line above (humorously including the editor, nano) was lifted without attribution and copyrighted on the RailsRock site [here](http://railsrock.com/how-to-get-rid-of-warning-message-on-webrick-about-keep-alive-connections/). – MarkDBlackwell Apr 23 '13 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`. – MarkDBlackwell Nov 05 '19 at 00: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.

ckhatton
  • 1,359
  • 1
  • 14
  • 43
  • @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 May 07 '13 at 15:53
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.

Michael Franzl
  • 1,341
  • 14
  • 19
  • 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 Jun 28 '16 at 16:03
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)

Xavier Shay
  • 4,067
  • 1
  • 30
  • 54