Skip to content

fix(supervisor): reconnect a serving connection without waiting for the shared backoff - #1808

Open
cyppe wants to merge 1 commit into
cloudflare:masterfrom
cyppe:fix/reconnect-after-edge-close
Open

cyppe wants to merge 1 commit into
cloudflare:masterfrom
cyppe:fix/reconnect-after-edge-close

Conversation

@cyppe

@cyppe cyppe commented Nov 25, 2026

Copy link
Copy Markdown

Summary

  • When a tunnel connection ends after it had registered and stayed up for at least tunnelRetryDuration, the supervisor now starts it again at once. It no longer queues the connection behind the shared reconnect backoff (supervisor/supervisor.go). connection with edge closed is the common case.
  • A reconnect that fails before registering, and a connection that lasted less than tunnelRetryDuration, still wait for the shared backoff. So a connection reconnects at once at most once per tunnelRetryDuration, the same limit region changes use.
  • startFirstTunnel reads its protocolFallback once, before connection 0 can register. It used to read s.tunnelsProtocolFallback again on every retry, while initialize adds the other connections' entries to that map once connection 0 has registered. The new test's connection-0 case trips the race detector on the old code.
  • EdgeTunnelServer.Serve still waits its own per-connection backoff before it returns (0–2 s with the default --retries, 0–1 s with --retries 0). Reconnects after an event that closes many connections at once stay spread out.

Why

  • Until a closed connection registers again, the edge keeps routing new requests to it, and each one fails with a 502 about 20 s later (🐛 A clean edge close waits up to 20 s (40 s or 80 s if repeated) before cloudflared redials, and the edge returns 502 for that connection the whole time #1782). Every second the connector waits before redialling is a second of failed requests for that connection's share of traffic, even while the other HA connections are healthy.
  • Today the shared backoff adds a random 0–20 s to every such close (0–10 s with --retries 0), even for a connection that served for hours. Its second-close window can double that. The backoff is right for a connection that cannot be established. It is not needed for one that the edge closed after serving.
  • Measured on a connector with 4 HA connections and --retries 0 (30 days, 160 single-connection closes): close-to-registration gap mean 6.5 s, median 6 s, p90 11 s. Requests routed into those gaps failed with 502.

Tests

  • TestReconnectAfterServedConnectionCloses (testing/synctest) covers four cases. A connection that served for an hour reconnects at the same instant it closed, for connection 0 (startFirstTunnel) and for the others. One that closed one second after registering, or that never registered, waits for the shared backoff.

  • go test -race ./supervisor/ ./retry/ passes; gofmt and go vet clean; golangci-lint run ./supervisor/... reports 0 issues.

  • End to end, against the real edge, with a connector using 4 HA connections over HTTP/2: each trial dropped our outbound packets of one connection's flow and then closed its socket. That reproduces an edge-side close the edge itself does not see. 12 trials per build at 20 requests/s:

    Build Close to Registered tunnel connection Failed requests when the edge was routing to the killed connection (3 trials)
    2026.11.2 mean 6.15 s, max 11.59 s 336
    2026.11.2 with this change mean 1.08 s, max 1.57 s 81

    Every failure was a 502 that started after the close and ended about 20 s later. What remains after the change is the per-connection jitter, the dial and registration, and the edge's own switch-over.

Fixes #1782

🤖 Generated with Claude Code

…ckoff

When a tunnel connection ends after it had registered and stayed up for at
least tunnelRetryDuration, start it again at once instead of queueing it
behind the supervisor's shared reconnect backoff. Until the connection
registers again, the edge keeps routing requests to it and answers them with
a 502, so the shared backoff's random 0-20 s (0-10 s with --retries 0) turns
into failed requests even though the connection had been healthy.

A connection that fails before registering, or that lasted less than
tunnelRetryDuration, still waits for the shared backoff, so a connection
reconnects at once at most once per tunnelRetryDuration, as region changes
do. EdgeTunnelServer.Serve keeps its per-connection backoff, which spreads
reconnects after an event that closes many connections.

startFirstTunnel now reads its protocol fallback once, before connection 0
can register: it read the map again on every retry while initialize adds the
other connections' entries.

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

Labels

None yet

Projects

None yet

1 participant