Skip to content

Commit 22f0cbd

Browse files
committed
The rescue clause is needed for the local variable error.
Effectively reverts 486074a.
1 parent bb87b1c commit 22f0cbd

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/async/http/protocol/http1/server.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ def each(task: Task.current)
151151
# Do not remove this line or you will unleash the gods of concurrency hell.
152152
task.yield
153153
end
154+
rescue => error
155+
# We store error (as a local variable) for later use in the ensure block.
156+
raise
154157
ensure
155158
body&.close(error)
156159
end

test/async/http/protocol/http11.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@
1010
require "async/http/protocol/http11"
1111
require "async/http/a_protocol"
1212

13+
# Custom error class to track in tests
14+
class BodyWriteError < StandardError; end
15+
16+
# A custom body class that raises during enumeration.
17+
class ErrorProneBody < ::Protocol::HTTP::Body::Readable
18+
def initialize(...)
19+
super(...)
20+
21+
@error = nil
22+
end
23+
24+
attr :error
25+
26+
def close(error = nil)
27+
@error = error
28+
super()
29+
end
30+
31+
def each
32+
super
33+
raise BodyWriteError, "error during write"
34+
end
35+
end
36+
1337
describe Async::HTTP::Protocol::HTTP11 do
1438
it_behaves_like Async::HTTP::AProtocol
1539

@@ -38,8 +62,30 @@
3862

3963
with "server" do
4064
include Sus::Fixtures::Async::HTTP::ServerContext
65+
4166
let(:protocol) {subject}
4267

68+
with "error during body write" do
69+
let(:body) {ErrorProneBody.new}
70+
71+
let(:app) do
72+
Protocol::HTTP::Middleware.for do |request|
73+
# Return a response with a body that will raise during enumeration:
74+
Protocol::HTTP::Response[200, {}, body]
75+
end
76+
end
77+
78+
it "handles error in ensure block without NameError" do
79+
response = client.get("/")
80+
81+
expect do
82+
response.read
83+
end.to raise_exception(EOFError)
84+
85+
expect(body.error).to be_a(BodyWriteError)
86+
end
87+
end
88+
4389
with "bad requests" do
4490
def around
4591
current = Console.logger.level

0 commit comments

Comments
 (0)