Skip to content

Commit 5c0e1cf

Browse files
Use pool ownership for graceful GOAWAY connections
Assisted-By: devx/618580b0-d55f-4c2e-95b6-87e648f60543
1 parent 806d6a1 commit 5c0e1cf

4 files changed

Lines changed: 35 additions & 25 deletions

File tree

‎async-http.gemspec‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
2525
spec.required_ruby_version = ">= 3.3"
2626

2727
spec.add_dependency "async", ">= 2.35.1"
28-
spec.add_dependency "async-pool", "~> 0.11"
28+
spec.add_dependency "async-pool", "~> 0.12"
2929
spec.add_dependency "io-endpoint", "~> 0.18"
3030
spec.add_dependency "io-stream", "~> 0.14"
3131
spec.add_dependency "protocol-http", "~> 0.66"

‎lib/async/http/protocol/http2/connection.rb‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,8 @@ def start_connection
8787
end
8888

8989
# Close the connection and stop the background reader.
90-
#
91-
# If the remote peer sent a graceful GOAWAY frame, the streams it accepted are still being processed and their responses are still on the way. Closing the connection now would fail those requests, even though the remote peer has already processed them. Instead we defer: the background reader closes the connection once the last stream completes. An explicit error still closes the connection immediately.
9290
def close(error = nil)
9391
if reader = @reader
94-
# Only the background reader can drain the connection, so if it is not running, there is nothing to wait for:
95-
return self if error.nil? and self.draining?
96-
9792
@reader = nil
9893

9994
# The reader task can close the connection itself, e.g. when the last stream completes and the connection is released back to the pool. Stopping it here would cancel the current task in the middle of this method, leaving the underlying stream open, so we let it unwind by itself: `closed?` is now true, so the read loop exits.

‎releases.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
- HTTP/2 connections which received a graceful `GOAWAY` are no longer closed while the server is still answering the streams it accepted. Such a connection is retired from the pool, but stays open until the last accepted stream completes, so those requests no longer fail with `EOFError: Connection closed with N active stream(s)!`.
5+
- HTTP/2 connections which received a graceful `GOAWAY` are removed from availability immediately, but remain in the pool until the server has finished answering the streams it accepted. The connection is closed after its final user releases it, so those requests no longer fail with `EOFError: Connection closed with N active stream(s)!`.
66

77
## v0.101.0
88

‎test/async/http/protocol/http2/graceful_goaway.rb‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def before
137137

138138
it "is closed" do
139139
expect(connection).to be(:goaway_received?)
140-
expect(connection).not.to be(:draining?)
141140
expect(connection).to be(:closed?)
142141
end
143142

@@ -163,7 +162,6 @@ def before
163162

164163
it "is not offered to new requests" do
165164
expect(connection).to be(:goaway_received?)
166-
expect(connection).to be(:draining?)
167165
expect(connection).not.to be(:closed?)
168166

169167
expect(connection).not.to be(:reusable?)
@@ -176,27 +174,42 @@ def before
176174
end.to raise_exception(::Protocol::HTTP::RefusedError)
177175
end
178176

179-
it "is not closed while the accepted stream is still in flight" do
180-
Async do
181-
connection.read_in_background
182-
183-
# The pool retires a connection which is no longer reusable, but the stream the server accepted is still being processed, so the connection must stay open:
184-
connection.close
185-
186-
expect(connection).not.to be(:closed?)
187-
expect(connection.streams).not.to be(:empty?)
188-
ensure
189-
connection.close(EOFError.new("Test finished!"))
190-
end.wait
191-
end
192-
193-
it "is closed if there is no reader to drain it" do
177+
it "closes synchronously" do
194178
connection.close
195179

196180
expect(connection).to be(:closed?)
197181
end
198182
end
199183

184+
it "is retained by the pool until all users release it" do
185+
connection.open!
186+
pool = Async::Pool::Controller.wrap(limit: 1){connection}
187+
188+
resource1 = pool.acquire
189+
resource2 = pool.acquire
190+
191+
response1 = connection.create_response
192+
response2 = connection.create_response
193+
connection.receive_goaway(goaway_frame(response2.stream.id))
194+
195+
response1.stream.close!
196+
pool.release(resource1)
197+
198+
expect(pool.resources[connection]).to be == 1
199+
expect(pool).not.to be(:available?)
200+
expect(connection).not.to be(:closed?)
201+
expect(connection.streams.keys).to be == [response2.stream.id]
202+
203+
response2.stream.close!
204+
expect(connection.framer).not.to be_nil
205+
206+
pool.release(resource2)
207+
208+
expect(pool.resources).not.to be(:key?, connection)
209+
expect(connection.framer).to be_nil
210+
expect(sockets.first).to be(:closed?)
211+
end
212+
200213
it "closes itself when the last drained stream completes on the sending side" do
201214
server = Protocol::HTTP2::Server.new(Protocol::HTTP2::Framer.new(sockets.last))
202215
server.open!
@@ -214,7 +227,9 @@ def before
214227
server.streams[stream.id].send_headers(response_headers, Protocol::HTTP2::END_STREAM)
215228

216229
Async::Task.current.sleep(0.1)
217-
expect(connection).to be(:draining?)
230+
expect(connection).to be(:goaway_received?)
231+
expect(connection).not.to be(:closed?)
232+
expect(connection.streams.keys).to be == [stream.id]
218233

219234
stream.send_data("body", Protocol::HTTP2::END_STREAM)
220235
Async::Task.current.sleep(0.1)

0 commit comments

Comments
 (0)