Skip to content

Commit 374b1f6

Browse files
committed
Ignore connection errors in read_line?.
1 parent 03106fe commit 374b1f6

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

lib/protocol/http1/connection.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def read(length)
349349

350350
# Read a line from the connection.
351351
#
352-
# @returns [String | Nil] the line read, or nil if the connection is gracefully closed.
352+
# @returns [String | Nil] the line read, or nil if the connection is closed.
353353
# @raises [LineLengthError] if the line is too long.
354354
# @raises [ProtocolError] if the line is not terminated properly.
355355
def read_line?
@@ -366,8 +366,9 @@ def read_line?
366366
end
367367

368368
return line
369-
370-
# I considered rescuing Errno::ECONNRESET here, but it seems like that would be ignoring a potentially serious error.
369+
# If a connection is shut down abruptly, we treat it as EOF, but only specifically in `read_line?`.
370+
rescue Errno::ECONNRESET
371+
return nil
371372
end
372373

373374
# Read a line from the connection.

releases.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Tidy up implementation of `read_line?` to handle line length errors and protocol violations more clearly.
6+
- Improve error handling for unexpected connection closures (`Errno::ECONNRESET`) in `read_line?`.
67

78
## v0.35.0
89

test/protocol/http1/connection.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
server.read_line?
4949
end.to raise_exception(Protocol::HTTP1::ProtocolError)
5050
end
51+
52+
it "returns nil on Errno::ECONNRESET" do
53+
expect(server.stream).to receive(:gets).and_raise(Errno::ECONNRESET)
54+
55+
expect(server.read_line?).to be_nil
56+
end
5157
end
5258

5359
with "#read_request" do

0 commit comments

Comments
 (0)