finch icon indicating copy to clipboard operation
finch copied to clipboard

Bad argument in arithmetic expression in `Finch.HTTP2.Pool`

Open pepicrft opened this issue 10 months ago • 5 comments

We are getting a "bad argument in arithmetic expression" error in this line. It seems that either max_backoff, base_backoff, or factor have a non-numeric value.

When would that happen? Could it be related to our configuration?

pepicrft avatar Feb 12 '25 17:02 pepicrft

That's possible. Could you please share your config so we could see?

sneako avatar Feb 17 '25 09:02 sneako

Thanks @sneako, we create the child with the following pools option:

%{
  :default => [size: 10],
  "https://fly.storage.tigris.dev" => [
    conn_opts: [log: true],
    size: 500,
    count: 1,
    protocols: [:http2]
  ]
}

pepicrft avatar Feb 21 '25 09:02 pepicrft

Bumping this one :)

pepicrft avatar Mar 19 '25 12:03 pepicrft

This happens here: https://github.com/sneako/finch/blob/f857ad514411f8ae7383bb431827769612493434/lib/finch/http2/pool.ex#L677

defp backoff(base_backoff, max_backoff, failure_count) do
  factor = :math.pow(2, failure_count)
  max_sleep = trunc(min(max_backoff, base_backoff * factor))
  :rand.uniform(max_sleep)
end

Can be observed when failure count reaches 1024

iex(1)> :math.pow(2,1023)
8.98846567431158e307
iex(2)> :math.pow(2,1024)
** (ArithmeticError) bad argument in arithmetic expression
    (stdlib 6.2.2.2) :math.pow(2, 1024)
    iex:2: (file)

In general this happens with ephemeral backends, especially if you are using IP's with headless services in Kubernetes.

Not sure if this helps @pepicrft but I am looking at similar issues.

Edit: It actually happens when the failure_count reaches 1015 with a base_backoff of 500, but the cause remains the same, unbounded calculations with :math.pow.

sindrip avatar Sep 26 '25 21:09 sindrip

Bumping above, any insights to when/how to fix this?

bluesbettle avatar Oct 08 '25 07:10 bluesbettle