This project is archived and is in readonly mode.

#2893 ✓committed
JDL

Rails 2.3.x - "render :text => proc" - problems with output buffer

Reported by JDL | July 9th, 2009 @ 02:20 PM | in 2.3.4

[Note: I am aware of #2873. I don't believe that it's the same bug.]

In Rails 2.3.2 and also Rails 2.3-stable (as of July 08, 2009 when I checked it) I am having a problem with render :text => proc {}

I created a new application with a single controller and a single action. The contents of the action are straight out of the online docs for render.

class LinesController < ApplicationController
  def show
    respond_to do |format|
      format.html { render :text => proc {|response, output|
          10.times do |i|
            output.write("This is line #{i}\n")
            output.flush
          end
        }
      }
    end
  end
end

When I run this under Rails 2.3.2 or 2.3-stable, I see the following.

$ curl http://localhost:3002/lines
curl: (18) transfer closed with outstanding read data remaining

If I hit this with a browser I see only the first line.

This is line 0

When I run this under Rails 2.2.2 I see the following response.

$ curl http://localhost:3002/lines
This is line 0
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9

For reference, I also started a discussion about this issue on stackoverflow, where I was asked to post a new bug ticket here.
http://stackoverflow.com/questions/1100443/rails-render-text-proc-i...

My local system:

OS X 10.5

$ ruby -v

ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-darwin9.3.0]

I tried this with both mongrel and passenger as well.

Comments and changes to this ticket

  • Michael Koziarski

    Michael Koziarski August 3rd, 2009 @ 06:08 AM

    • Tag changed from 2-3-stable, 2.3.2, render to 2-3-stable, 2.3.2, bugmash, render
  • Nikolay Petrachkov

    Nikolay Petrachkov August 8th, 2009 @ 11:42 AM

    • Assigned user set to “CancelProfileIsBroken”

    Code above throw error:

     
    

    Internal Server Error

    
        undefined method `flush' for #<ActionController::Response:0x00000002bf4300>
    

    ActionController::Response doesn't contain method response .

    Without output.flush code works:

    curl http://localhost:3000/lines/show
    This is line 0
    This is line 1
    This is line 2
    This is line 3
    This is line 4
    This is line 5
    This is line 6
    This is line 7
    This is line 8
    This is line 9
    
  • CancelProfileIsBroken

    CancelProfileIsBroken August 8th, 2009 @ 12:29 PM

    • Assigned user changed from “CancelProfileIsBroken” to “Michael Koziarski”
  • Nick Quaranto

    Nick Quaranto August 8th, 2009 @ 03:43 PM

    Verified with 2-3-stable, used the same example and it breaks with both a proc and lambda.

    2893 master % curl http://localhost:3000/lines/show
    This is line 0
    curl: (18) transfer closed with outstanding read data remaining
    
    Processing LinesController#show (for 127.0.0.1 at 2009-08-08 10:39:05) [GET]
    Completed in 3ms (View: 1, DB: 0) | 200 OK [http://localhost/lines/show]
    Sat Aug 08 10:39:05 -0400 2009: Read error: #<NoMethodError: undefined method `flush' for #<ActionController::Response:0x2915b34>>
    /Users/qrush/Dev/bugmash/2893/app/controllers/lines_controller.rb:8:in `show'
    /Users/qrush/Dev/bugmash/2893/app/controllers/lines_controller.rb:6:in `times'
    /Users/qrush/Dev/bugmash/2893/app/controllers/lines_controller.rb:6:in `show'
    /Users/qrush/Dev/bugmash/2893/vendor/rails/actionpack/lib/action_controller/response.rb:153:in `call'
    /Users/qrush/Dev/bugmash/2893/vendor/rails/actionpack/lib/action_controller/response.rb:153:in `each'
    /Users/qrush/Dev/bugmash/2893/vendor/rails/actionpack/lib/action_controller/reloader.rb:21:in `send'
    /Users/qrush/Dev/bugmash/2893/vendor/rails/actionpack/lib/action_controller/reloader.rb:21:in `method_missing'
    /opt/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/chunked.rb:37:in `each'
    /opt/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/mongrel.rb:74:in `process'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:159:in `process_client'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:158:in `each'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:158:in `process_client'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `run'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `initialize'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `new'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `run'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in `initialize'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in `new'
    /opt/local/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in `run'
    /opt/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/mongrel.rb:34:in `run'
    /Users/qrush/Dev/bugmash/2893/vendor/rails/railties/lib/commands/server.rb:111
    /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3
    

    This definitely works with 2-2-stable for me as well:

    2893 master % curl http://localhost:3000/lines/show
    This is line 0
    This is line 1
    This is line 2
    This is line 3
    This is line 4
    This is line 5
    This is line 6
    This is line 7
    This is line 8
    This is line 9
    
  • Josh Nichols

    Josh Nichols August 8th, 2009 @ 05:34 PM

    Verified, as I was watching over Nick's shoulder.

  • Nick Quaranto

    Nick Quaranto August 8th, 2009 @ 05:54 PM

    Looks like ActionController already supports this. From actionpack/test/controller/rack_test.rb:

      def test_streaming_block
        @response.body = Proc.new do |response, output|
          5.times { |n| output.write(n) }
        end
        @response.prepare!
    
        status, headers, body = @response.to_a
        assert_equal 200, status
        assert_equal({
          "Content-Type" => "text/html; charset=utf-8",
          "Cache-Control" => "no-cache",
          "Set-Cookie" => ""
        }, headers)
    
        parts = []
        body.each { |part| parts << part }
        assert_equal ["0", "1", "2", "3", "4"], parts
      end
    

    Josh and I have prepared a patch that implements flush on ActionController::Response and warns the user about this.

  • Josh Nichols

    Josh Nichols August 8th, 2009 @ 06:01 PM

    +1 on the patch. It giving a deprecation warning seems the most reasonable, since chunking already happens without it, so just warning will give the user a heads up that they can safely remove it.

  • Brian Cardarella
  • Sam Goldstein
  • Pratik

    Pratik August 8th, 2009 @ 06:49 PM

    Adding Yehuda to notifications.

  • Nikolay Petrachkov
  • Nick Quaranto

    Nick Quaranto August 8th, 2009 @ 07:55 PM

    Looks like this is an issue with Rails 3.0 as well, and since the Response class is now under ActionDispatch it needed a new patch. Here's patches for both 2-3-stable and master.

  • Dan Croak

    Dan Croak August 8th, 2009 @ 08:04 PM

    +1 confirmed test and patch apply in both 2-3-stable and master (rake test_action_pack is clean in both branches)

  • Dan Pickett

    Dan Pickett August 8th, 2009 @ 08:09 PM

    +1 and I verified Nick's patch applies cleanly to master - patch applies and suite passes

  • Jeremy Kemper

    Jeremy Kemper August 8th, 2009 @ 11:04 PM

    • State changed from “new” to “open”
    • Milestone changed from 2.x to 2.3.4

    Could you update the docs to omit the flush call?

    Then only 2-3-stable needs the deprecation warning and master is fine as-is.

  • Nick Quaranto
  • Jeremy Kemper

    Jeremy Kemper August 9th, 2009 @ 01:24 AM

    • State changed from “open” to “committed”
    • Tag changed from 2-3-stable, 2.3.2, bugmash, render to 2-3-stable, 2.3.2, render

    2-3-stable: d39c45690e501e7572de2f70f68224bc95e6bf01

  • JDL

    JDL August 9th, 2009 @ 03:27 AM

    Thank you very much to everyone who worked on this. I really appreciate it.

  • Repository

    Repository August 9th, 2009 @ 05:24 AM

    (from [d39c45690e501e7572de2f70f68224bc95e6bf01]) Adding a deprecation warning for output.flush when rendering a proc or lambda

    [#2893 state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/d39c45690e501e7572de2f70f68224...

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages