Skip to content

Commit 81c7dbe

Browse files
committed
Better error handling in Buffered#sysread.
1 parent fb9f443 commit 81c7dbe

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/io/stream/buffered.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ def syswrite(buffer)
133133

134134
# Reads data from the underlying stream as efficiently as possible.
135135
def sysread(size, buffer)
136-
# Come on Ruby, why couldn't this just return `nil`? EOF is not exceptional. Every file has one.
137-
while true
136+
while !@io.closed?
138137
result = @io.read_nonblock(size, buffer, exception: false)
139138

140139
case result
@@ -146,8 +145,10 @@ def sysread(size, buffer)
146145
return result
147146
end
148147
end
149-
rescue Errno::EBADF
150-
raise ::IOError, "stream closed"
148+
149+
# Otherwise, the `@io` was closed while reading:
150+
# https://github.com/ruby/openssl/issues/798
151+
raise ::IOError, "closed stream"
151152
end
152153
end
153154
end

releases.md

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

55
- On Ruby v3.3+, use `IO#write` directly instead of `IO#write_nonblock`, for better performance.
6+
- `Buffered#sysread` now checks `@io.closed?` before attempting to read, improving error handling.
67

78
## v0.7.0
89

0 commit comments

Comments
 (0)