Skip to content

fix: close discarded candidate cluster to prevent VTAdmin dynamic cluster resource leak - #20881

Open
waterWang wants to merge 1 commit into
vitessio:mainfrom
waterWang:fix/vtadmin-dynamic-cluster-leak
Open

fix: close discarded candidate cluster to prevent VTAdmin dynamic cluster resource leak#20881
waterWang wants to merge 1 commit into
vitessio:mainfrom
waterWang:fix/vtadmin-dynamic-cluster-leak

Conversation

@waterWang

Copy link
Copy Markdown

Fixes #20866

Problem

When WithCluster receives a candidate cluster that matches an existing cached cluster, the candidate is discarded but never closed. Each rejected candidate leaves a schema-cache worker running and retains its client proxies and RPC pools.

In deployments using --enable-dynamic-clusters, sending N identical requests creates N schema-cache workers; only 1 is stopped when the API closes.

Root Cause

In WithCluster, when shouldAddCluster is false (candidate equals existing), the else branch only logs a message — the candidate c is never Close()d.

Fix

Close the candidate cluster in the else branch to release its schema-cache worker, client proxies, and RPC pools. This mirrors the existing cleanup pattern used when replacing an existing cluster with a new one.

Verification

  • Without the fix: 5 identical dynamic-cluster requests create 5 schema-cache workers
  • With the fix: each rejected candidate is properly closed, keeping only the cached cluster's worker

Copilot AI balanced review requested due to automatic review settings August 20, 2026 16:34
@waterWang
waterWang requested a review from mattlord as a code owner August 20, 2026 16:34
@github-actions github-actions Bot added this to the v25.0.0 milestone Aug 20, 2026
@vitess-bot vitess-bot Bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Aug 20, 2026
@vitess-bot

vitess-bot Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@github-actions github-actions Bot added the Component: VTAdmin VTadmin interface label Aug 20, 2026

Copilot AI 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.

Pull request overview

Closes discarded duplicate dynamic-cluster candidates to prevent VTAdmin resource leaks.

Changes:

  • Closes rejected candidates and logs cleanup failures.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread go/vt/vtadmin/api.go
Comment on lines +360 to +362
if err := c.Close(); err != nil {
log.Error(fmt.Sprintf("candidate cluster %s leaked resources on close: %v", id, err))
}

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b7b0c329d9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread go/vt/vtadmin/api.go
// The discarded candidate is never stored or used;
// without this Close() each rejected candidate leaves
// a schema-cache worker running.
if err := c.Close(); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep the active cluster open when the pointers alias

If a caller passes the same *cluster.Cluster already stored under id, Equal succeeds and this branch closes that active object; selectedCluster then points to the closed cluster, so subsequent requests use closed caches and proxies. WithCluster does not require a distinct allocation, so only close c when it differs from existingCluster.

AGENTS.md reference: AGENTS.md:L159-L163

Useful? React with 👍 / 👎.

Comment thread go/vt/vtadmin/api.go
// The discarded candidate is never stored or used;
// without this Close() each rejected candidate leaves
// a schema-cache worker running.
if err := c.Close(); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add a regression test that observes candidate closure

The existing repeated-request case in TestServeHTTP only checks response contents and passes identically without this change, so it does not prove that the discarded cluster's worker and proxies are closed. Add a test with observable closers or worker lifecycle state that fails on the parent commit and verifies exactly one candidate cleanup here.

AGENTS.md reference: AGENTS.md:L79-L80

Useful? React with 👍 / 👎.

Comment thread go/vt/vtadmin/api.go
// without this Close() each rejected candidate leaves
// a schema-cache worker running.
if err := c.Close(); err != nil {
log.Error(fmt.Sprintf("candidate cluster %s leaked resources on close: %v", id, err))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve fields in the new close-error log

This newly added message formats the cluster ID and error into an opaque string, so structured-log consumers cannot filter or aggregate the failure by cluster or error. Emit a fixed message with slog.String("cluster", id) and slog.Any("error", err) instead.

AGENTS.md reference: AGENTS.md:L198-L201

Useful? React with 👍 / 👎.

Comment thread go/vt/vtadmin/api.go
// The discarded candidate is never stored or used;
// without this Close() each rejected candidate leaves
// a schema-cache worker running.
if err := c.Close(); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Release clusterMu before waiting for candidate shutdown

When a discarded candidate has an in-flight schema-cache fill or one of its proxy closers stalls, c.Close() waits while clusterMu remains held from the start of WithCluster; every concurrent WithCluster and EjectDynamicCluster call then blocks behind this cleanup. Select and detach the candidate while locked, then close it after unlocking, or otherwise bound the shutdown.

AGENTS.md reference: AGENTS.md:L119-L124

Useful? React with 👍 / 👎.

@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 61.72%. Comparing base (70c7a72) to head (b7b0c32).
⚠️ Report is 495 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vtadmin/api.go 50.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (70c7a72) and HEAD (b7b0c32). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (70c7a72) HEAD (b7b0c32)
1 0
Additional details and impacted files
@@             Coverage Diff             @@
##             main   #20881       +/-   ##
===========================================
- Coverage   69.67%   61.72%    -7.96%     
===========================================
  Files        1614       76     -1538     
  Lines      216793     7022   -209771     
===========================================
- Hits       151044     4334   -146710     
+ Misses      65749     2688    -63061     
Flag Coverage Δ
partial 61.72% <50.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

Component: VTAdmin VTadmin interface NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: VTAdmin leaks equal dynamic-cluster candidates

3 participants