active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

0.9.x breaks streaming features of ActionController::Live

Open jagthedrummer opened this issue 3 years ago • 0 comments
trafficstars

Expected behavior vs actual behavior

Consider a simple streaming example like this:

class StreamingTestController < ApplicationController
  include ActionController::Live

  def index
    # This header doesn't seem to be necessary, but it's probably good to set it.
    response.headers['Content-Type'] = 'text/event-stream'

    # These two headers are requried to work around a mismatch of expectations between
    # rack 2.2.x and rails. https://github.com/rack/rack/issues/1619
    response.headers['Last-Modified'] = Time.now.httpdate
    response.headers['ETag'] = '0'

    5.times do
      response.stream.write "hello world\n"
      sleep 1
    end
  ensure
    response.stream.close
  end
end

When you curl that endpoint you should see one instance of hello world output immediately, then a one second pause, and then another, and so on until you see five instances.

curl http://localhost:3001/streaming_test/index
hello world
hello world
hello world
hello world
hello world

When a version of active_model_serializers from the 0.9.x branch is loaded, instead of getting a streaming response you'll see nothing for about 5 seconds, then you'll see 5 instances of hello world output all at once.

With the 0.10.x branch the problem no longer happens.

I'm opening this issue mostly for documentation purposes so that if someone else discovers that streaming isn't working in their app they might stumble across this issue and save some time.

Steps to reproduce

  1. Generate a new rails app
  2. Make a streaming controller like the one above
  3. curl it to see that it streams
  4. Add gem "active_model_serializers", "0.9.8" to the Gemfile
  5. bundle install
  6. curl the endpoint again to see that streaming is no longer working

Environment

ActiveModelSerializers Version: 0.9.3 & 0.9.8

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.6.8p205 (2021-07-07 revision 67951) [x86_64-darwin20]

OS Type & Version: macOS 12.3

Integrated application and version (e.g., Rails, Grape, etc): rails 5.2.8

Backtrace

None

Additonal helpful information

Here's a repo that demonstrates the problem with the 0.9.x line. The README describes how to alter the Gemfile to see that the 0.10.x line fixes the issue.

https://github.com/Octo-Labs/rails_streaming_test

jagthedrummer avatar Jun 01 '22 20:06 jagthedrummer