socketry / async-http

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to force close `::Async::HTTP::Internet` instance without calling `response.read`

arseny-emchik opened this issue · comments

Hi! I make requests using ::Async::HTTP::Internet.new. Everything works well until there is an error before reading the body of the response. So closing ::Async::HTTP::Internet instance without calling response.read leads to Waiting for Async::HTTP::Protocol::HTTP1 pool to drain: #<Async::Pool::Controller(1/∞) 1/1/1> error which blocks the Fiber process (infinity loop I guess).

Question: Is there any way how to force close ::Async::HTTP::Internet instance without calling .read function?

Code example:

def make_get_request(url)
  client_instance = ::Async::HTTP::Internet.new
  response = client_instance.get(url)
  raise "Any Error" # Any logic that can raise an exception
  response.read
ensure
  client_instance.close # Warning and Fiber process hanging => Waiting for Async::HTTP::Protocol::HTTP1 pool to drain: #<Async::Pool::Controller(1/∞) 1/1/1>
end
Screenshot 2023-10-02 at 12 50 44

Research:
The error happens here in the Async::Client#close function:

def close
  while @pool.busy?
    Console.logger.warn(self) { "Waiting for #{@protocol} pool to drain: #{@pool}" }
    @pool.wait
  end

  @pool.close
end

I would appreciate any help! Thank you!

I think the simplest solution is to ensure you have

ensure
  response&.close
  client_instance&.close

@ioquatix Thank you for explaining how to do this! That works