Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure nil is returned from dead fibers #153

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/fastimage.rb
Original file line number Diff line number Diff line change
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