diff --git a/CHANGELOG.md b/CHANGELOG.md index f32e39e4efe..78c96235a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ * [BUGFIX] Ring: Fix `DoBatch` counting a key more than once when more replicas than the quorum succeed, which in rare cases could hide a write error from a key that did not reach quorum. A distributor push now always waits until every series reaches quorum, so it returns the error of a series that misses quorum, and takes as long as its slowest series to reach quorum. #7975 * [BUGFIX] Compactor: Stop downloading the parquet files and the `parquet-converter-mark.json` and `parquet-no-convert-mark.json` markers written by the parquet converter under a block's prefix. Compaction never reads them, so they only used local disk and bandwidth until the compaction job ended. #7972 * [BUGFIX] Ring: Fix shuffle-sharding subrings treating instances with a stale heartbeat as healthy when using the DynamoDB KV store. #7976 +* [BUGFIX] Ingester Client: Fix a gRPC connection leak when `-distributor.use-stream-push` is enabled and the push streams to an ingester cannot be opened. #7969 ## 1.22.0 2026-11-19 diff --git a/pkg/ingester/client/client.go b/pkg/ingester/client/client.go index 0604b0de7f7..f522a224a87 100644 --- a/pkg/ingester/client/client.go +++ b/pkg/ingester/client/client.go @@ -168,6 +168,11 @@ func MakeIngesterClient(addr string, cfg Config, useStreamConnection bool) (Heal streamCtx, streamCancel := context.WithCancel(context.Background()) err = c.Run(make(chan *streamWriteJob, INGESTER_CLIENT_STREAM_WORKER_COUNT), streamCtx, streamCancel) if err != nil { + // The client is discarded, so nothing will ever call Close() on it. Undo what + // has already been set up: cancelling streamCtx stops the job goroutines of the + // workers that did start, and closing conn stops its reconnect loop. + streamCancel() + _ = conn.Close() return nil, err } } @@ -210,7 +215,9 @@ func (c *closableHealthAndIngesterClient) Run(streamPushChan chan *streamWriteJo c.streamCtx = streamCtx c.streamCancel = streamCancel - var workerErr error + // All the workers below run concurrently, so the error they report has to be + // stored through something that is safe for concurrent writes. + var workerErr atomic.Error var wg sync.WaitGroup // Sanitize addr: colons (from host:port) are not allowed in tenant IDs. sanitizedAddr := strings.ReplaceAll(c.addr, ":", "-") @@ -220,12 +227,12 @@ func (c *closableHealthAndIngesterClient) Run(streamPushChan chan *streamWriteJo workerCtx := user.InjectOrgID(streamCtx, workerName) err := c.worker(workerCtx) if err != nil { - workerErr = err + workerErr.Store(err) } }) } wg.Wait() - return workerErr + return workerErr.Load() } func (c *closableHealthAndIngesterClient) worker(ctx context.Context) error { diff --git a/pkg/ingester/client/client_test.go b/pkg/ingester/client/client_test.go index 65b46d6ca24..6f719227d7a 100644 --- a/pkg/ingester/client/client_test.go +++ b/pkg/ingester/client/client_test.go @@ -3,8 +3,11 @@ package client import ( "context" "fmt" + "net" "net/http/httptest" + "runtime" "strconv" + "strings" "testing" "time" @@ -17,6 +20,7 @@ import ( "github.com/cortexproject/cortex/pkg/cortexpb" "github.com/cortexproject/cortex/pkg/util" + "github.com/cortexproject/cortex/pkg/util/flagext" ) // TestMarshall is useful to try out various optimisation on the unmarshalling code. @@ -393,3 +397,53 @@ func TestClosableHealthAndIngesterClient_ShouldNotPanicWhenClose(t *testing.T) { time.Sleep(100 * time.Millisecond) } + +// grpcGoroutines counts the running goroutines that have a gRPC frame on their stack. +// A ClientConn that is still open keeps at least its reconnect goroutine around, so +// this is enough to tell a closed connection from a leaked one. +func grpcGoroutines() int { + buf := make([]byte, 1<<20) + for { + n := runtime.Stack(buf, true) + if n < len(buf) { + buf = buf[:n] + break + } + buf = make([]byte, 2*len(buf)) + } + + count := 0 + for stack := range strings.SplitSeq(string(buf), "\n\n") { + if strings.Contains(stack, "google.golang.org/grpc") { + count++ + } + } + return count +} + +// TestMakeIngesterClient_StreamSetupFailureClosesConnection covers the stream-push +// factory used by the distributor and the querier: when the push streams cannot be +// opened, MakeIngesterClient returns an error and the client is discarded, so nothing +// will ever call Close() on it and it has to clean up after itself. +func TestMakeIngesterClient_StreamSetupFailureClosesConnection(t *testing.T) { + // Reserve a port and release it, so grpc.NewClient still succeeds (it connects + // lazily) while every PushStream worker fails to establish a transport. + l, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + addr := l.Addr().String() + require.NoError(t, l.Close()) + + var cfg Config + flagext.DefaultValues(&cfg) + + baseline := grpcGoroutines() + + c, err := MakeIngesterClient(addr, cfg, true) + require.Error(t, err, "opening the push streams should fail against an address that refuses connections") + require.Nil(t, c) + + require.Eventually(t, func() bool { + return grpcGoroutines() <= baseline + }, 30*time.Second, 50*time.Millisecond, + "gRPC goroutines stayed above the %d at the start of the test, so the connection of the discarded client was left open", baseline) +}