Merge pull request #153 from sdsykes/fix-net-reader-error

Ensure nil is returned from dead fibers, fixes #152
This commit is contained in:
Stephen Sykes 2024-03-28 20:25:54 +02:00 committed by GitHub
commit 8495352ffe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -321,6 +321,7 @@ class FastImage
res.read_body do |str|
Fiber.yield str
end
nil
end
case res['content-encoding']
@ -335,6 +336,7 @@ class FastImage
while data = gzip.readline
Fiber.yield data
end
nil
end
end
@ -388,12 +390,14 @@ class FastImage
Fiber.yield str
offset += LocalFileChunkSize
end
nil
end
else
read_fiber = Fiber.new do
while str = readable.read(LocalFileChunkSize)
Fiber.yield str
end
nil
end
end
@ -471,6 +475,9 @@ class FastImage
include StreamUtil
attr_reader :pos
# read_fiber should return nil if it no longer has anything to return when resumed
# so the result of the whole Fiber block should be set to be nil in case yield is no
# longer called
def initialize(read_fiber)
@read_fiber = read_fiber
@pos = 0
@ -484,7 +491,6 @@ class FastImage
unused_str = @str[@strpos..-1]
new_string = @read_fiber.resume
new_string = @read_fiber.resume if new_string.is_a? Net::ReadAdapter
raise CannotParseImage if !new_string
# we are dealing with bytes here, so force the encoding
new_string.force_encoding("ASCII-8BIT") if new_string.respond_to? :force_encoding
@ -573,7 +579,7 @@ class FastImage
# the file, and is within the first 1000 chars.
begin
:svg if (1..100).detect {|n| @stream.peek(10 * n).include?("<svg")}
rescue FiberError
rescue FiberError, CannotParseImage
nil
end
end