Skip to content

Commit f30387a

Browse files
committed
Raise EOFError when the connection closes while a write waits.
1 parent 7d5a7db commit f30387a

3 files changed

Lines changed: 70 additions & 10 deletions

File tree

‎lib/protocol/http2/connection.rb‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,28 +298,25 @@ def receive_goaway(frame)
298298

299299
# Write a single frame to the connection.
300300
# @parameter frame [Frame] The frame to write.
301+
# @raises [EOFError] If the connection is closed.
301302
def write_frame(frame)
302303
synchronize do
303-
@framer.write_frame(frame)
304+
open_framer.write_frame(frame)
304305
end
305306

306-
@framer.flush
307+
open_framer.flush
307308
end
308309

309310
# Write multiple frames within a synchronized block.
310311
# @yields {|framer| ...} The framer for writing multiple frames.
311312
# @parameter framer [Framer] The framer instance.
312313
# @raises [EOFError] If the connection is closed.
313314
def write_frames
314-
if @framer
315-
synchronize do
316-
yield @framer
317-
end
318-
319-
@framer.flush
320-
else
321-
raise EOFError, "Connection closed!"
315+
synchronize do
316+
yield open_framer
322317
end
318+
319+
open_framer.flush
323320
end
324321

325322
# Update local settings and adjust stream window capacities.
@@ -668,6 +665,13 @@ def receive_continuation(frame)
668665
def receive_frame(frame)
669666
# ignore.
670667
end
668+
669+
private
670+
671+
# The connection is closed, and `@framer` cleared, by the task reading frames, which can happen while a writer waits for the lock or for the socket, so it is checked again at each use.
672+
def open_framer
673+
@framer or raise EOFError, "Connection closed!"
674+
end
671675
end
672676
end
673677
end

‎releases.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- `Connection#write_frame` and `#write_frames` raise `EOFError` when the connection closes while the write waits for the lock or the socket, rather than `NoMethodError` on the cleared framer.
6+
37
## v0.29.1
48

59
- Sign releases with the Socketry release certificate.

‎test/protocol/http2/connection.rb‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,58 @@
104104
end.to raise_exception(EOFError, message: be =~ /Connection closed/)
105105
end
106106

107+
with "a connection which closes while a write waits for the lock" do
108+
def before
109+
super
110+
111+
def connection.synchronize
112+
close
113+
super
114+
end
115+
end
116+
117+
it "can't write a frame" do
118+
expect do
119+
connection.write_frame(Protocol::HTTP2::PingFrame.new)
120+
end.to raise_exception(EOFError, message: be =~ /Connection closed/)
121+
end
122+
123+
it "can't write frames" do
124+
expect do
125+
connection.write_frames do |framer|
126+
end
127+
end.to raise_exception(EOFError, message: be =~ /Connection closed/)
128+
end
129+
end
130+
131+
with "a connection which closes while a write waits for the socket" do
132+
def before
133+
super
134+
135+
def connection.synchronize
136+
super
137+
ensure
138+
close
139+
end
140+
end
141+
142+
it "can't flush a frame" do
143+
frame = Protocol::HTTP2::PingFrame.new
144+
frame.pack("12345678")
145+
146+
expect do
147+
connection.write_frame(frame)
148+
end.to raise_exception(EOFError, message: be =~ /Connection closed/)
149+
end
150+
151+
it "can't flush frames" do
152+
expect do
153+
connection.write_frames do |framer|
154+
end
155+
end.to raise_exception(EOFError, message: be =~ /Connection closed/)
156+
end
157+
end
158+
107159
with "#close" do
108160
it "closes the connection without active streams" do
109161
# No active streams

0 commit comments

Comments
 (0)