Skip to content

Return chunks an enumerable body yields from a nested fiber. - #42

Open
cole-robertson wants to merge 1 commit into
socketry:mainfrom
cole-robertson:enumerable-read-nested-fiber
Open

cole-robertson wants to merge 1 commit into
socketry:mainfrom
cole-robertson:enumerable-read-nested-fiber

Conversation

@cole-robertson

Copy link
Copy Markdown

Body::Enumerable#read reads the body with to_enum(:each).next. Enumerator#next is built on Fiber.yield, so when the body yields a chunk from inside a fiber it created, the chunk goes to whoever resumed that fiber instead of the reader, and is dropped.

Rails' render stream: true does this: ActionView::StreamingTemplateRenderer renders the layout in a Fiber so the template can be rendered when the layout reaches yield. On Falcon, a streamed template comes back as 200 with an empty or partial body. Puma and other servers call #each directly, so they're unaffected.

This enumerates the body in a fiber of its own and transfers each chunk back to the reader with Fiber#transfer, following the precedent in #16. Otherwise read behaves as before: an exception raised by the body is raised in the reader and the next read starts the body again, a StopIteration from the body ends the read, reading after the end returns nil, and close closes the wrapped body. A body that previously lost its nested-fiber chunks may now also raise errors that came after them.

Reproduction (no Rails needed)
require "async"
require "protocol/rack/body/enumerable"

# The shape of Rails' streaming renderer: the layout runs in a nested fiber.
body = Class.new do
  def each(&block)
    layout = Fiber.new do
      block.call("<html>")
      Fiber.yield
      block.call("</html>")
    end

    layout.resume
    block.call("template")
    layout.resume
  end
end.new

Sync do
  enumerable = Protocol::Rack::Body::Enumerable.new(body, nil)
  chunks = []
  while chunk = enumerable.read
    chunks << chunk
  end
  p chunks
end

On main: ["template"]. With this change: ["<html>", "template", "</html>"].

Through Falcon 0.57.0, a Rails 8.1 app's render stream: true returned 0 of 8 complete responses before this change and 8 of 8 after. The other streaming paths (an Enumerator body, a callable body, ActionController::Live, send_stream and Active Storage's proxy) returned the same results before and after.

Types of Changes

  • Bug fix.

Contribution

`Body::Enumerable#read` used `to_enum(:each).next`. `Enumerator#next` is built on `Fiber.yield`, so a chunk the body yields from inside a fiber it created goes to whoever resumed that fiber, not to the reader, and is dropped. Rails' `render stream: true` renders its layout in such a fiber, so on Falcon the response was `200` with an empty or partial body.

Enumerate the body in a fiber of its own and transfer each chunk back to the reader using `Fiber#transfer`, following the precedent in socketry#16. Errors raised by the body still reach the reader, and a `StopIteration` from the body still ends the read.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant