edge: bound the health prober's dial at the probe timeout
The prober dialed with upstreamDialer, which has no connect timeout. Its http.Client.Timeout ends the request at 2 s, but net/http dials outside the request's deadline, so each tick against a black-holed upstream left a dial running for the kernel's connect timeout (about two minutes). probeDialer gives the dial a Timeout of the probe timeout, the time the spec allows a whole probe, connect through body, and keeps upstreamguard in front so the link-local refusal holds. TestProberDialIsBounded holds the connect in a Control hook until the dial's own context ends: it fails without the bound and passes with it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+87
−12
internal/edge/prober.go+12 −5internal/edge/upstreamdial.go+19 −7internal/edge/upstreamdial_test.go+56 −0
diff --git a/internal/edge/prober.go b/internal/edge/prober.go| index 4dc10dd7e..aa13e2429 100644 |
| --- a/internal/edge/prober.go |
| +++ b/internal/edge/prober.go |
| @@ -34,6 +34,7 @@ import ( |
| "fmt" |
| "io" |
| "log" |
| + "net" |
| "net/http" |
| "slices" |
| "sync" |
| @@ -129,17 +130,23 @@ type proberSet struct { |
| // newProberSet builds the set with hold.probe as the probe timeout (T in |
| // the readmission formula, spec §3), and hold.dialer, when set, as the |
| // probes' dialer, so a test's instrumented dialer sees the probes too; |
| -// unset (production), the probes dial with upstreamDialer, which refuses |
| -// link-local and IMDS addresses. A non-positive probe falls back to |
| -// probeTimeout: an unbounded probe would stall every tick behind one |
| -// black-holed upstream. |
| +// unset (production), the probes dial with probeDialer, which refuses |
| +// link-local and IMDS addresses and gives up a connect at the probe |
| +// timeout. A non-positive probe falls back to probeTimeout: an unbounded |
| +// probe would stall every tick behind one black-holed upstream. |
| func newProberSet(hold localHold) *proberSet { |
| + return newProberSetDialing(hold, net.Dialer{}) |
| +} |
| + |
| +// newProberSetDialing is newProberSet with base as the dialer probeDialer |
| +// starts from: a plain net.Dialer in production, a test's black hole. |
| +func newProberSetDialing(hold localHold, base net.Dialer) *proberSet { |
| if hold.probe <= 0 { |
| hold.probe = probeTimeout |
| } |
| dial := hold.dialer |
| if dial == nil { |
| - dial = upstreamDialer().DialContext |
| + dial = probeDialer(base, hold.probe).DialContext |
| } |
| return &proberSet{ |
| hosts: make(map[string]*hostProber), |
diff --git a/internal/edge/upstreamdial.go b/internal/edge/upstreamdial.go| index c9b081407..6c3d4c52c 100644 |
| --- a/internal/edge/upstreamdial.go |
| +++ b/internal/edge/upstreamdial.go |
| @@ -10,13 +10,10 @@ import ( |
| // upstreamDialer is the dialer for a TCP upstream when no test dialer is set |
| // (hold.dialer nil, as in production): the plain net.Dialer the transports |
| // used before, behind upstreamguard, which refuses link-local and IMDS |
| -// addresses after resolution and before connect. multiProxy and the prober |
| -// use it; tcpProxy uses tcpUpstreamDialer; unix sockets, backhaul hops and |
| -// tunnels use neither. multiProxy bounds each dial with a context deadline |
| -// (multiDialTimeout). The prober's dial is still unbounded: its |
| -// http.Client.Timeout and ResponseHeaderTimeout do not reach the dial, |
| -// since net/http strips the request's deadline before dialing. Bounding it |
| -// is follow-up work. |
| +// addresses after resolution and before connect. multiProxy uses it, and |
| +// bounds each dial with a context deadline (multiDialTimeout); tcpProxy |
| +// uses tcpUpstreamDialer and the prober probeDialer, which carry their own |
| +// connect bounds; unix sockets, backhaul hops and tunnels use none of them. |
| func upstreamDialer() *net.Dialer { |
| return upstreamguard.Dialer(net.Dialer{}) |
| } |
| @@ -39,3 +36,18 @@ const ( |
| func tcpUpstreamDialer() *net.Dialer { |
| return upstreamguard.Dialer(net.Dialer{Timeout: tcpUpstreamDialTimeout, KeepAlive: tcpUpstreamKeepAlive}) |
| } |
| + |
| +// probeDialer is the prober's dialer when no test dialer is set: base (a |
| +// plain net.Dialer in production) with a connect bound of the probe |
| +// timeout, behind upstreamguard, so the link-local refusal holds. The |
| +// bound has to be on the dialer: the prober's http.Client.Timeout and |
| +// ResponseHeaderTimeout end the request, but net/http dials outside the |
| +// request's deadline, so without it every tick against a black-holed |
| +// upstream leaves a dial behind for the kernel's connect timeout (about |
| +// two minutes on Linux). The probe timeout is the right size because the |
| +// spec gives a whole probe, connect through body, that long (probeTimeout, |
| +// 2 s): a connect that takes longer has already failed its probe. |
| +func probeDialer(base net.Dialer, timeout time.Duration) *net.Dialer { |
| + base.Timeout = timeout |
| + return upstreamguard.Dialer(base) |
| +} |
diff --git a/internal/edge/upstreamdial_test.go b/internal/edge/upstreamdial_test.go| index 672bf1bd0..b3a533013 100644 |
| --- a/internal/edge/upstreamdial_test.go |
| +++ b/internal/edge/upstreamdial_test.go |
| @@ -3,10 +3,12 @@ package edge |
| import ( |
| "context" |
| "errors" |
| + "net" |
| "net/http" |
| "net/http/httptest" |
| "net/http/httputil" |
| "strings" |
| + "syscall" |
| "testing" |
| "time" |
| @@ -163,3 +165,57 @@ func TestTCPUpstreamDialerIsBounded(t *testing.T) { |
| t.Fatalf("dial err = %v, want upstreamguard.ErrLinkLocal", err) |
| } |
| } |
| + |
| +// TestProberDialIsBounded: a probe whose connect never completes gives up |
| +// at the probe timeout. The prober's http.Client.Timeout ends the request |
| +// but not the dial, which net/http runs outside the request's deadline, so |
| +// without a dialer Timeout a black-holed upstream leaves a dial behind on |
| +// every tick for the kernel's two minutes. The black hole is a Control |
| +// hook that holds the connect until the dial's own context ends: the dial |
| +// gives up only if the dialer carries the bound. Nothing is connected. |
| +func TestProberDialIsBounded(t *testing.T) { |
| + const bound = 200 * time.Millisecond |
| + stop := make(chan struct{}) |
| + defer close(stop) |
| + gaveUp := make(chan time.Duration, 8) |
| + base := net.Dialer{ControlContext: func(ctx context.Context, _, _ string, _ syscall.RawConn) error { |
| + start := time.Now() |
| + select { |
| + case <-ctx.Done(): |
| + gaveUp <- time.Since(start) |
| + return ctx.Err() |
| + case <-stop: |
| + return errors.New("test over") |
| + } |
| + }} |
| + ps := newProberSetDialing(localHold{probe: bound}, base) |
| + req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:9/", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if resp, err := ps.client.Do(req); err == nil { |
| + resp.Body.Close() |
| + t.Fatal("a black-holed probe answered") |
| + } |
| + select { |
| + case took := <-gaveUp: |
| + if took > bound+time.Second { |
| + t.Fatalf("the dial gave up after %s, want about the %s probe timeout", took, bound) |
| + } |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the probe's dial is still connecting 5 s after its request ended: it has no bound") |
| + } |
| + |
| + // The bound keeps the link-local refusal in front of it. |
| + d := probeDialer(net.Dialer{}, bound) |
| + if d.Timeout != bound { |
| + t.Fatalf("probe dialer Timeout %v, want %v", d.Timeout, bound) |
| + } |
| + conn, err := d.DialContext(context.Background(), "tcp", "169.254.169.254:80") |
| + if conn != nil { |
| + conn.Close() |
| + } |
| + if !errors.Is(err, upstreamguard.ErrLinkLocal) { |
| + t.Fatalf("dial err = %v, want upstreamguard.ErrLinkLocal", err) |
| + } |
| +} |