What does "WARN Could not determine content-length of response body." mean and how to I get rid of it?
Asked Answered
R

9

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?

Reins answered 16/8, 2011 at 17:28 Comment(3)
Same here, for me it is happening when it's a remote call via JS.Paramount
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...Microtone
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.Ajay
I
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.

Intrigante answered 31/8, 2011 at 20:29 Comment(8)
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 @Ajay Lowe already said this above.)Brahe
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.Illogicality
@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.Intrigante
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.Deciduous
I put this in a development & test group in my Gemfile just to keep it isolated.Chaves
@AlexL: it is included, see the below the link.Intrigante
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 :)Deuteranopia
The answer should contain the content of the twitter posts instead of links.Dezhnev
U
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.

Unpractical answered 30/1, 2012 at 22:3 Comment(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.Memoir
On Debian squeeze, RVM-installed Ruby version 1.9.3-p327 WEBrick still gives these problematic warnings.Salted
R
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
Rudie answered 25/2, 2013 at 22:13 Comment(2)
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."Salted
This is so much better than 'ignore it', or 'patch webrick'. Thank you!Blanca
U
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.

Undergrown answered 20/4, 2012 at 15:42 Comment(3)
Yes. This is what I've ended up doing in more recent months. Ryan Bates also mentioned in a recent Railscast.Reins
@cam song: almost correct: rails s thin will use thin instead of Webrick, and the warn will disappear.Brahe
I put thin in development group. Rails 4 seems picking it up automatically when running rails sCaress
W
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

Witkin answered 19/1, 2013 at 15:51 Comment(0)
S
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.)

Salted answered 4/8, 2012 at 14:24 Comment(2)
My fancy command line above (humorously including the editor, nano) was lifted without attribution and copyrighted on the RailsRock site here.Salted
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.Salted
J
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.

Jowl answered 19/3, 2013 at 14:10 Comment(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.Jowl
S
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)

Shaylashaylah answered 20/1, 2012 at 0:11 Comment(0)
R
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.

Regression answered 10/3, 2015 at 7:33 Comment(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.Adiathermancy

© 2022 - 2024 — McMap. All rights reserved.