Skip to content

accelerate-v0.6.403

@kanat kanat tagged this 28 Nov 16:07
When a node failed to reach a peer once, it kept refusing that peer for
up to 2 minutes, even after the peer was back at the same address. This
PR makes the node open a new connection to a peer whose last dial
failed, so a peer that is back is reached on the next request. The old
connection is closed only when the last call using it has ended, so
calls already on it finish normally. This is the cause of the flaky
`TestForwardedSessionSuite/TestAnOperationThatTakesNoBodyHereIsCarriedWithTheOneSent`
(AI-4350): it got an instant 503 because its test server was given a
port an earlier test had left dead.

## Why

The node keeps one gRPC connection per peer address for as long as it
runs. After a failed dial, gRPC puts that connection into a reconnect
wait that grows to 2 minutes, and until the wait runs out it fails every
call at once, without dialling. So a node that came back at the same
address (a restarted container, a reused pod IP) got "the node running
this session could not be reached" on every request carried to it until
then.

The API suite hits this because five of its tests hold a session on a
port nothing listens on and send a request there, which leaves that
port's connection waiting. When the OS later gives the same port to
another test's server, the request to it fails in milliseconds. In the
CI failure the test took 5.00 s, which is the 5 s wait for the carried
request and nothing more, so the 503 itself came at once.

## How it works

```
Forwarder.peer(address)          every caller gets the connection and must release it when its call ends
├─ no connection yet            -> open one (as before)
├─ connection is idle/connecting/ready
│                               -> reuse it, count one more call on it
└─ connection's last dial failed (TRANSIENT_FAILURE)
                                -> mark it retired, open a new one
                                   ├─ no call uses the old one -> close it now
                                   └─ calls still use it       -> the last call to end closes it
                                   the new connection dials now:
                                   ├─ peer is back -> answered
                                   └─ peer is down -> 503 at once, as before
```

| Case | Before | After | Test |
| --- | --- | --- | --- |
| Peer back at an address that failed | 503 until the reconnect wait
runs out (up to 2 min) | answered on the next request |
`TestANodeBackAtAnAddressThatFailedIsReached` |
| Peer still down | 503 at once | 503 at once, one dial per request |
same test, first call |
| Many calls at once to a peer that is down | (in the first version of
this PR, some failed as Canceled because the connection was closed under
them) | all fail as Unavailable |
`TestCallsAtOnceToANodeThatIsDownAllFailAsUnavailable` |
| Connection replaced while a call is on it | (in the first version of
this PR, the call was cut off) | the call finishes; the old connection
closes after it |
`TestACallInFlightOutlivesTheReplacementOfItsConnection` |
| Healthy connection, call in flight | reused | reused, the call is not
cut off | `TestACallInFlightOutlivesTheNextCallToTheSameNode` |
| Many calls to a healthy peer | one connection | still one connection |
`TestCallsToAHealthyNodeShareOneConnection` |

Resetting the backoff (`ResetConnectBackoff`) is not enough: gRPC's
pick_first stays in TRANSIENT_FAILURE until it is READY (gRFC A62), so a
call made right after the reset still fails. I tried it and the probe
failed 5 of 5 times.

Reviewed in 2 rounds: round 1 was NO-GO because a replaced connection
was closed while other calls still used it; round 2 is GO.

This is a product fix, not a test-only change, so the forwarding path
needs the usual review.

<details><summary>Evidence, repro counts, mutations and
commands</summary>

Sources (grpc-go v1.83.2): `backoff/backoff.go:51` (`MaxDelay: 120 *
time.Second`), `picker_wrapper.go:170-176` (a fail-fast call ends with
Unavailable when the picker errors),
`balancer/pickfirst/pickfirst.go:359-360` (stays in TRANSIENT_FAILURE
until READY). Tests that leave a dead port's connection waiting in the
suite are the ones that use `heldWhereNothingAnswers` in
`forward_test.go`. CI run 46072756923 attempt 1: `forward_test.go:574`
503 and `:752` nothing carried, test time 5.00 s.

Repro counts (first version of the fix, 2d05ddd61; the later change only
touches when the old connection is closed):

| Probe | Base 92cb0f5ee | Head |
| --- | --- | --- |
| `TestForwardSuite/TestANodeBackAtAnAddressThatFailedIsReached`,
`-count 20 -race`, macOS | 20/20 fail | 0/20 |
| same, Linux `golang:1.27.4`, `--cpus 2`, `-count 10 -cpu 1,2` | 20/20
fail | node suite `-count 5 -race -cpu 1,2` passes |
| suite-level probe in `ForwardedSessionSuite` (dead port, then a server
on that port, then `POST /interrupt`), `-count 10`, not committed |
10/10 fail (503 in 0.01 s) | 0/10 |

Round 1 found that the first version closed the replaced connection at
once. On that version the two new tests fail: 126 of 1600 concurrent
calls ended Unavailable and 1474 ended Canceled, and a call in flight
got Unavailable ("closing transport ... use of closed network
connection").

Mutations on head bf4ce1ce7 (delta round: 11 mutants, 0 survived; `go
test -count=1 -run TestForwardSuite ./internal/node/`):

| Mutation | Test that failed |
| --- | --- |
| close a failed connection at once, no check for calls using it |
`TestACallInFlightOutlivesTheReplacementOfItsConnection`,
`TestCallsAtOnceToANodeThatIsDownAllFailAsUnavailable` |
| no `release` when a call ends |
`TestACallInFlightOutlivesTheReplacementOfItsConnection`,
`TestANodeBackAtAnAddressThatFailedIsReached` |
| `release` never closes |
`TestACallInFlightOutlivesTheReplacementOfItsConnection` |
| an unused failed connection is not closed in `peer` |
`TestANodeBackAtAnAddressThatFailedIsReached` |
| a reused connection is not counted |
`TestACallInFlightOutlivesTheReplacementOfItsConnection`,
`TestCallsAtOnceToANodeThatIsDownAllFailAsUnavailable` |
| the old connection is never marked retired |
`TestACallInFlightOutlivesTheReplacementOfItsConnection` |
| a new connection starts with no calls counted | the in-flight,
back-at-address and calls-at-once tests |
| a new connection for every call |
`TestACallInFlightOutlivesTheReplacementOfItsConnection`,
`TestCallsToAHealthyNodeShareOneConnection` |
| never replace |
`TestACallInFlightOutlivesTheReplacementOfItsConnection`,
`TestANodeBackAtAnAddressThatFailedIsReached` |
| reuse only a READY connection |
`TestACallInFlightOutlivesTheReplacementOfItsConnection`,
`TestCallsToAHealthyNodeShareOneConnection` |
| `release` closes any retired connection, even with calls left |
`TestCallsAtOnceToANodeThatIsDownAllFailAsUnavailable` |

Suites run on head (all pass):

- `go test -race -count=50 -run TestForwardSuite ./internal/node/`
- `go test -race -cpu 1 -count=50 -run TestForwardSuite
./internal/node/`
- `go test -cpu 1,2 -count=30 ./internal/node/` (whole package)
- `go vet ./...`, also with `-tags integration`
- `go test -tags integration -p 1 ./internal/api/ -run
'^(TestForwardedSessionSuite|TestForwardedDispatchSuite|TestShardedDispatchSuite|TestHomeShardDownDispatchSuite|TestNodeRetrySuite)$'`,
plus `TestForwardedSessionSuite -count=4 -cpu 1,2`

</details>

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Assets 2
Loading