Skip to content

Commit

Permalink
Ensure nil is returned from dead fibers
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsykes committed Mar 28, 2024
1 parent 7d86b40 commit 7de7faf
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion 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

0 comments on commit 7de7faf

Please sign in to comment.