Skip to content

Ingester client: release the gRPC connection when stream setup fails - #7969

Open
arpitjain099 wants to merge 2 commits into
cortexproject:masterfrom
arpitjain099:fix/ingester-client-leak-on-stream-setup-failure
Open

arpitjain099 wants to merge 2 commits into
cortexproject:masterfrom
arpitjain099:fix/ingester-client-leak-on-stream-setup-failure

Conversation

@arpitjain099

Copy link
Copy Markdown

What this PR does:

With -distributor.use-stream-push enabled, MakeIngesterClient opens the gRPC connection before it starts the push-stream workers. If starting them fails it returns the error without closing that connection or cancelling the stream context. ring/client.Pool only records a client after the factory succeeds, so nothing is left holding the connection and nothing can ever close it, and its reconnect loop keeps dialing the ingester address until the process exits. The failures cluster while an ingester's address is still in the ring but already unreachable, so each rollout leaks a burst of them, and a querier reaches the same factory from the read path.

The error path now cancels the stream context and closes the connection. Run also collected the workers' error in a plain variable written by all 100 of them, which go test -race reports, so it is now an atomic.Error as in pkg/ring/batch.go.

The new test drives MakeIngesterClient against an address that refuses connections and fails on master.

I used Claude Code while preparing this, and I have reviewed and tested the change myself.

Which issue(s) this PR fixes:
Fixes #7759

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

@arpitjain099
arpitjain099 requested a review from a team as a code owner November 25, 2026 11:06
@arpitjain099
arpitjain099 force-pushed the fix/ingester-client-leak-on-stream-setup-failure branch from a3072da to e3bef13 Compare November 25, 2026 11:08
SungJin1212
SungJin1212 previously approved these changes Nov 26, 2026

@SungJin1212 SungJin1212 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is LGTM, I left an one nit.

Comment thread CHANGELOG.md Outdated
* [BUGFIX] Querier: Increment `cortex_query_evictions_total` and deregister the victim before cancelling it in the query evictor, fixing double-counted evictions of still-unwinding queries. #7616
* [BUGFIX] Ring: Ignore READONLY ingesters when assigning tokens so the active series remain balanced. #7946
* [BUGFIX] Distributor: Fix a `panic: send on closed channel` crash during shutdown when `-distributor.num-push-workers` or `-distributor.num-query-workers` is greater than 0. #7964
* [BUGFIX] Ingester Client: Fix a gRPC connection leak when `-distributor.use-stream-push` is enabled. A client whose push streams could not be opened was discarded without closing its connection or cancelling its stream context, and because the client pool only records a client once the factory has succeeded, nothing could close it afterwards. Each failure left a connection reconnecting to the ingester address for the lifetime of the process, and the failures cluster while an address is still in the ring but already unreachable. The error the stream workers report is also no longer written to a plain variable from all of them concurrently. #7969

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rewrite a changelog? It's feel like an internal implementation, not the user-facing changes..

Suggested change
* [BUGFIX] Ingester Client: Fix a gRPC connection leak when `-distributor.use-stream-push` is enabled. A client whose push streams could not be opened was discarded without closing its connection or cancelling its stream context, and because the client pool only records a client once the factory has succeeded, nothing could close it afterwards. Each failure left a connection reconnecting to the ingester address for the lifetime of the process, and the failures cluster while an address is still in the ring but already unreachable. The error the stream workers report is also no longer written to a plain variable from all of them concurrently. #7969
* [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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And can you fix a CI lint?

@arpitjain099 arpitjain099 Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SungJin1212 Taken verbatim, thank you. Yours is better scoped than mine, which described the mechanism rather than the user-facing effect.

@arpitjain099 arpitjain099 Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SungJin1212 Fixed. It was stringsseq at client_test.go:416, now ranging over strings.SplitSeq. The package passes locally, 22 tests, and the lint job should confirm it.

@SungJin1212
SungJin1212 dismissed their stale review November 26, 2026 02:10

Approved by mistake.

@siddarth2810 siddarth2810 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@sandy2008 sandy2008 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closing the connection on the Run error releases the started workers and the reconnect loop. The new test fails on master and passes here with -race. CHANGELOG.md needs a rebase. LGTM.

@SungJin1212

Copy link
Copy Markdown
Member

@arpitjain099
Could you get a rebase?

arpitjain099 and others added 2 commits December 1, 2026 06:10
MakeIngesterClient returned the error from Run without closing the connection
or cancelling the stream context, so a discarded client left a connection
reconnecting to the ingester address for the lifetime of the process. Nothing
could reach it, because ring/client.Pool only records a client once the factory
has succeeded.

Run also stored the workers' error in a plain variable written by all of them
concurrently, which the race detector reports.

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Arpit Jain <[email protected]>
@arpitjain099
arpitjain099 force-pushed the fix/ingester-client-leak-on-stream-setup-failure branch from f4b1509 to 18681d4 Compare December 1, 2026 11:15
@arpitjain099

Copy link
Copy Markdown
Author

@SungJin1212 Rebased on master and resolved the CHANGELOG conflict, so the entry now sits after #7976 in the unreleased block. Re-ran the new test with -race afterwards, and it still fails on master and passes here. Thanks @sandy2008 for catching the conflict.

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MakeIngesterClient leaks grpc.ClientConn and goroutines on failure

4 participants