Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#2893

[Note: I am aware of #2873 Application reloading should be deferred to end of request, currently interfe.... 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.

Reported by JDL · July 9th, 2009 @ 02:20 PM

State: committed
Milestone: 2.3.4
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski
    • Tag changed from 2-3-stable, 2.3.2, render to 2-3-stable, 2.3.2, bugmash, render

    August 3rd, 2009 @ 06:08 AM

  2. Nikolay Petrachkov
    Nikolay Petrachkov
    • 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
    

    August 8th, 2009 @ 11:42 AM

  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • Assigned user changed from CancelProfileIsBroken to Michael Koziarski

    August 8th, 2009 @ 12:29 PM

  4. Nick Quaranto
    Nick Quaranto

    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
    

    August 8th, 2009 @ 03:43 PM

  5. Josh Nichols
    Josh Nichols

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

    August 8th, 2009 @ 05:34 PM

  6. Nick Quaranto
    Nick Quaranto

    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.

    August 8th, 2009 @ 05:54 PM

  7. Josh Nichols
    Josh Nichols

    +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.

    August 8th, 2009 @ 06:01 PM

  8. Brian Cardarella
  9. Sam Goldstein
  10. Pratik
    Pratik

    Adding Yehuda to notifications.

    August 8th, 2009 @ 06:49 PM

  11. Nikolay Petrachkov
  12. Nick Quaranto
    Nick Quaranto

    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.

    August 8th, 2009 @ 07:55 PM

  13. Dan Croak
    Dan Croak

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

    August 8th, 2009 @ 08:04 PM

  14. Dan Pickett
    Dan Pickett

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

    August 8th, 2009 @ 08:09 PM

  15. Jeremy Kemper
    Jeremy Kemper
    • 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.

    August 8th, 2009 @ 11:04 PM

  16. Nick Quaranto
  17. Jeremy Kemper
    Jeremy Kemper
    • 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

    August 9th, 2009 @ 01:24 AM

  18. JDL
    JDL

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

    August 9th, 2009 @ 03:27 AM

  19. Repository