Skip to content

Commit

Permalink
Merge pull request #153 from sdsykes/fix-net-reader-error
Browse files Browse the repository at this point in the history
Ensure nil is returned from dead fibers, fixes #152
  • Loading branch information
sdsykes committed Mar 28, 2024
2 parents 7d86b40 + 36ac127 commit 8495352
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/fastimage.rb
Expand Up @@ -321,6 +321,7 @@ def fetch_using_http_from_parsed_uri
res.read_body do |str|
Fiber.yield str
end
nil
end

case res['content-encoding']
Expand All @@ -335,6 +336,7 @@ def fetch_using_http_from_parsed_uri
while data = gzip.readline
Fiber.yield data
end
nil
end
end

Expand Down Expand Up @@ -388,12 +390,14 @@ def fetch_using_read(readable)
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

Expand Down Expand Up @@ -471,6 +475,9 @@ class FiberStream # :nodoc:
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
Expand All @@ -484,7 +491,6 @@ def peek(n)
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
Expand Down Expand Up @@ -573,7 +579,7 @@ def parse_type
# 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
Expand Down

0 comments on commit 8495352

Please sign in to comment.