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
9 Answers
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
fyi, if the messages bother you, as a workaround you can use thin (add
gem 'thin'
to your gemfile, start your server usingrails server thin
). (oops, just noticed that @Scott Lowe already said this above.)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.
@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.
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.
The answer should contain the content of the twitter posts instead of links.
The following patch solved the problem in my case; no more warnings for me.
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.
On Debian squeeze, RVM-installed Ruby version 1.9.3-p327 WEBrick still gives these problematic warnings.
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
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.”
This is so much better than ‘ignore it’, or ‘patch webrick’. Thank you!
– nessur
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.
–@cam song: almost correct: rails s thin will use thin instead of Webrick, and the warn will disappear.
I put
thin
indevelopment
group. Rails 4 seems picking it up automatically when runningrails s
– draw
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.
Use your favorite editor to open this file:
~/.rvm/rubies/<ruby-version>/lib/ruby/1.9.1/webrick/httpresponse.rb
Go to the line that contains this(for me it was really line 206):
if chunked? || @header['content-length']
Change it, taken from this patch, to this:
if chunked? || @header['content-length'] || @status == 304 || @status == 204
Save the file and eventually restart your rails server
1
Thank you! Was
line 107
for me.– gbdev
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{.rbn}"`/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.
The backticks probably shouldn’t be escaped. So it should really be:
nano `ruby -e"print %x{gem which webrick}.chomp %Q{.rbn}"`/httpresponse.rb
.
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.
Use your favorite editor to open this file:
~/.rvm/rubies/jruby-<version>/lib/ruby/<1.8 or 1.9>/webrick/httpresponse.rb
Go to the line that contains this (for me it was line 205):
if chunked? || @header['content-length']
Change it, taken from this patch, to this:
if chunked? || @header['content-length'] || @status == 304 || @status == 204
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.
– Crimbo
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
)
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.
Same here, for me it is happening when it’s a remote call via JS.
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…
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.