From 306a05257b3ec8c7da8b481b98d3da07ef2bf750 Mon Sep 17 00:00:00 2001 From: Cole Robertson Date: Tue, 17 Nov 2026 10:44:57 -0600 Subject: [PATCH] Return chunks an enumerable body yields from a nested fiber. `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 #16. Errors raised by the body still reach the reader, and a `StopIteration` from the body still ends the read. --- lib/protocol/rack/body/enumerable.rb | 28 +++++- releases.md | 4 + test/protocol/rack/body/enumerable.rb | 119 ++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 4 deletions(-) diff --git a/lib/protocol/rack/body/enumerable.rb b/lib/protocol/rack/body/enumerable.rb index 0a33c18..09bbcb8 100644 --- a/lib/protocol/rack/body/enumerable.rb +++ b/lib/protocol/rack/body/enumerable.rb @@ -133,13 +133,33 @@ def call(stream) # Read the next chunk from the response body. # Returns nil when there are no more chunks. # + # The body is enumerated in a fiber of its own, and each chunk is transferred back to the reader. Unlike `Enumerator#next`, which relies on `Fiber.yield`, this also returns chunks the body yields from inside a fiber it created. + # # @returns [String | Nil] The next chunk or nil if there are no more chunks. def read - @chunks ||= @body.to_enum(:each) + return nil if @chunks == false + + @chunks ||= Fiber.new do |reader| + @body.each do |chunk| + @chunks = Fiber.current + reader = reader.transfer(chunk) + nil + end + rescue Exception => error + # Raised in the reader below. + ensure + # After an error, the next read enumerates the body again. + @chunks = error ? nil : false + reader.transfer(error) + end + + result = @chunks.transfer(Fiber.current) + + # As with `Enumerator#next`, a `StopIteration` raised by the body ends this read. + return nil if result.is_a?(StopIteration) + raise result if result.is_a?(Exception) - return @chunks.next - rescue StopIteration - return nil + return result end # Get a string representation of the body. diff --git a/releases.md b/releases.md index 4968833..7337a3a 100644 --- a/releases.md +++ b/releases.md @@ -1,5 +1,9 @@ # Releases +## Unreleased + + - Return chunks yielded from nested fibers when reading enumerable response bodies. + ## v0.23.0 - Respect the result of rewinding the underlying request body. diff --git a/test/protocol/rack/body/enumerable.rb b/test/protocol/rack/body/enumerable.rb index 9c851c9..355037c 100644 --- a/test/protocol/rack/body/enumerable.rb +++ b/test/protocol/rack/body/enumerable.rb @@ -5,6 +5,8 @@ require "protocol/rack/body/enumerable" +require "sus/fixtures/async/scheduler_context" + describe Protocol::Rack::Body::Enumerable do with "empty body" do let(:body) {subject.new([], nil)} @@ -30,6 +32,7 @@ expect(body.read).to be == " " expect(body.read).to be == "World" expect(body.read).to be_nil + expect(body.read).to be_nil end it "returns nil when exhausted" do @@ -38,6 +41,122 @@ body.read expect(body.read).to be_nil end + + it "returns nil to the body from each yield" do + values = [] + body = subject.new(Class.new{define_method(:each){|&block| values << block.call("Hello")}}.new, 5) + + expect(body.read).to be == "Hello" + expect(body.read).to be_nil + expect(values).to be == [nil] + end + + it "can be read from different fibers" do + expect(body.read).to be == "Hello" + expect(Fiber.new{body.read}.resume).to be == " " + expect(body.read).to be == "World" + end + + it "closes the wrapped body when closed partway through" do + closed = false + body = subject.new(Rack::BodyProxy.new(["Hello", " ", "World"]){closed = true}, 11) + + expect(body.read).to be == "Hello" + body.close + + expect(closed).to be == true + end + + with "a body that raises" do + let(:failing) do + Class.new do + def each + yield "Hello" + raise "Bad Enumerable" + end + end + end + + let(:body) {subject.new(failing.new, nil)} + + include Sus::Fixtures::Async::SchedulerContext + + it "raises the error to the reader" do + expect(body.read).to be == "Hello" + expect{body.read}.to raise_exception(RuntimeError, message: be =~ /Bad Enumerable/) + end + + it "enumerates the body again after the error" do + body.read + expect{body.read}.to raise_exception(RuntimeError) + + expect(body.read).to be == "Hello" + end + end + + with "a body that raises StopIteration" do + let(:stopping) do + Class.new do + def each + yield "Hello" + raise StopIteration + end + end + end + + let(:body) {subject.new(stopping.new, nil)} + + it "treats it as the end of the body" do + expect(body.read).to be == "Hello" + expect(body.read).to be_nil + + expect(body.read).to be == "Hello" + end + end + + with "a body that raises an exception that is not a StandardError" do + let(:failing) do + Class.new do + def each + yield "Hello" + raise Interrupt + end + end + end + + let(:body) {subject.new(failing.new, nil)} + + include Sus::Fixtures::Async::SchedulerContext + + it "raises the exception to the reader" do + expect(body.read).to be == "Hello" + expect{body.read}.to raise_exception(Interrupt) + end + end + + with "a body that yields from a nested fiber" do + let(:nested) do + Class.new do + def each(&block) + Fiber.new do + block.call("Hello") + block.call(" ") + end.resume + + block.call("World") + end + end + end + + let(:body) {subject.new(nested.new, 11)} + + it "returns every chunk" do + expect(body.read).to be == "Hello" + expect(body.read).to be == " " + expect(body.read).to be == "World" + expect(body.read).to be_nil + end + end end with "#each" do