Skip to content

Retry a GET that times out #1302

Closed

Description

@woodm1979

Hex.HTTP.retry/6 retries a GET on an HTTP error, :socket_closed_remotely and some :failed_connect reasons, but not when the request times out. One stalled request then fails the whole mix deps.get, even though a second attempt would very likely succeed.

What we see

In GitLab CI, mix deps.get with a 112-package lock fails about once a day across our branches. In each job, one registry request out of the 112 gets no answer within 15 s, and the other 111 succeed:

Failed to fetch record for websockex from registry
:timeout
** (Mix) Unknown package websockex in lockfile

It's a different package each time (websockex, finch, expo, makeup, ex_slop, yamerl over six jobs in five days), and rerunning the job passes. This is on Hex 2.6.0 with Elixir 1.20 and OTP 29, and it happened on earlier Hex versions too.

Cause

On main (68e9be4), retry/6 in lib/hex/http.ex sends {:error, :timeout}, which timeout/4 returns when the request task doesn't answer in time, to {:noretry, other}. Raising HEX_HTTP_TIMEOUT doesn't help with a request that has stalled.

#1139 retries %Hex.Mint.TransportError{reason: :timeout}, but as far as I can tell a stall there also surfaces as a bare {:error, :timeout}: Host.request/6 and the outer timeout/2 both turn their exits into it, so retryable?/1 still returns false.

Proposal

Retry a GET on {:error, :timeout} like the other transient errors, up to @request_retries. GETs are idempotent, and request_to_file reopens its file with :write on each attempt, so a partial download from a timed-out attempt is overwritten. The worst case for a request that never answers goes from 15 s to 45 s.

I have a test and a one-clause change ready and am happy to open a PR, against main or on top of #1139, whichever you prefer.

Drafted by Claude on behalf of @woodm1979.

Activity

  1. woodm1979 commented on Nov 3, 2026

    @woodm1979
    ContributorAuthor

    Bah. Sorry. I didn't really intend for Claude to open an issue against an external repo. I can only imagine how much LLM-spam you guys get.

    I guess I should leave it here in case you find it useful, but PLEASE don't even hesitate for a second to delete this if you feel it is nonsense. 🫤

  2. woodm1979 commented on Nov 3, 2026

    @woodm1979
    ContributorAuthor

    In case it saves anyone time, here is the change as a patch against main (68e9be4), applied with git apply. The new test fails with {:error, :timeout} without the lib/hex/http.ex hunk. With it, mix test --exclude integration passes (509 tests) and mix format --check-formatted is clean.

    diff --git a/lib/hex/http.ex b/lib/hex/http.ex
    index 4f5376e5..243dbee1 100644
    --- a/lib/hex/http.ex
    +++ b/lib/hex/http.ex
    @@ -341,6 +341,9 @@ defmodule Hex.HTTP do
             {:error, :socket_closed_remotely} = error ->
               {:retry, error}
     
    +        {:error, :timeout} = error ->
    +          {:retry, error}
    +
             {:error, {:failed_connect, [{:to_address, to_addr}, {inet, inet_l, reason}]}}
             when inet in [:inet, :inet6] and
                    reason in [:ehostunreach, :enetunreach, :eprotonosupport, :nxdomain] ->
    diff --git a/test/hex/http_test.exs b/test/hex/http_test.exs
    index 4fe56193..239fa194 100644
    --- a/test/hex/http_test.exs
    +++ b/test/hex/http_test.exs
    @@ -566,4 +566,23 @@ defmodule Hex.HTTPTest do
           refute File.exists?(filename)
         end)
       end
    +
    +  test "retries a GET that times out" do
    +    {:ok, listener} = :gen_tcp.listen(0, [:binary, active: false])
    +    {:ok, port} = :inet.port(listener)
    +
    +    # The first connection never answers, the second one does
    +    spawn_link(fn ->
    +      {:ok, stalled} = :gen_tcp.accept(listener)
    +      {:ok, _request} = :gen_tcp.recv(stalled, 0)
    +
    +      {:ok, socket} = :gen_tcp.accept(listener)
    +      {:ok, _request} = :gen_tcp.recv(socket, 0)
    +      :ok = :gen_tcp.send(socket, "HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\nok")
    +      Process.sleep(:infinity)
    +    end)
    +
    +    assert {:ok, {200, _headers, "ok"}} =
    +             Hex.HTTP.request(:get, "http://localhost:#{port}", %{}, nil, %{timeout: 200})
    +  end
     end

    Drafted by Claude on behalf of @woodm1979.

  3. woodm1979 commented on Nov 18, 2026

    @woodm1979
    ContributorAuthor

    Oh awesome @ericmj That's fantastic. And again, sorry for the LLM-Spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions