Skip to content

smartconnpool: reject SetCapacity on a closed pool + shutdown stress tests#20158

Draft
arthurschreiber wants to merge 3 commits into
mainfrom
arthur/smartconnpool-extra-stress-tests
Draft

smartconnpool: reject SetCapacity on a closed pool + shutdown stress tests#20158
arthurschreiber wants to merge 3 commits into
mainfrom
arthur/smartconnpool-extra-stress-tests

Conversation

@arthurschreiber
Copy link
Copy Markdown
Member

@arthurschreiber arthurschreiber commented May 21, 2026

What's this?

Adds the remaining smartconnpool stress/regression tests from the connection pool stall / correctness audit, plus the small fix one of them surfaced.

Fix: SetCapacity on a closed pool

A SetCapacity call queued on capacityMu while CloseWithContext holds it would acquire the mutex right after Close released it and silently store the requested capacity into pool.capacity. The pool then sat in a IsOpen() == false / Capacity() > 0 state — closed, but with capacity still pointing at the racing caller's last value.

Guarded SetCapacity with the same close.Load() == nil check that reopen() already uses, returning ErrConnPoolClosed when the pool is not open.

The same guard also rejects pre-Open calls, which were already a no-op in practice — open() unconditionally stores config.maxCapacity into pool.capacity — but quietly accepted the value. One test (primeTxPoolWithConnection) was relying on this no-op; it now sets capacity after Open, which is what it always intended.

Tests

  • TestStressFull — broad shutdown stress
  • TestStressRefreshReopenSetCapacityClose — surfaces the SetCapacity/Close race fixed here
  • TestCloseWithContextAfterIdleCleanupPopDoesNotLeak — covered by smartconnpool: unblock Close and preserve Setting on reopen #20122's fix
  • TestSetCapacityRejectedOnClosedPool — pins the new contract directly (pre-Open, open, and closed)

Related Issue(s)

Builds on #20122 and #20157, both of which have merged.

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

SetCapacity now returns ErrConnPoolClosed instead of silently writing into pool.capacity when the pool is closed (or has never been opened). The only production callers — the /debug/env HTTP setters in tabletserver.go — are guarded by tsv.isOpen and only run when the QueryEngine pools are open, so this is not a user-visible change there.

AI Disclosure

Tests written by GPT-5 with human direction; the SetCapacity guard and the test updates were written by Claude Code with direction from Arthur.

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Copilot AI review requested due to automatic review settings May 21, 2026 12:01
@github-actions github-actions Bot added this to the v25.0.0 milestone May 21, 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 May 21, 2026
@vitess-bot
Copy link
Copy Markdown
Contributor

vitess-bot Bot commented May 21, 2026

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR expands the smartconnpool test suite with additional shutdown/stress regression coverage intended to exercise close/drain behavior under concurrency, refresh-driven reopens, and capacity churn (as part of the shutdown audit work referenced by #20122 and #20157).

Changes:

  • Added a comprehensive pool stress test that mixes Get/Recycle/Taint, refresh checks, and capacity updates while asserting no leaks after shutdown (TestStressFull).
  • Added a multi-cycle stress regression that targets close behavior under aggressive refresh-triggered reopen plus concurrent SetCapacity churn (TestStressRefreshReopenSetCapacityClose).
  • Added a focused regression test ensuring CloseWithContext does not hand an “idle-cleanup popped” connection to a waiter once capacity reaches 0 (TestCloseWithContextAfterIdleCleanupPopDoesNotLeak).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
go/pools/smartconnpool/stress_test.go Adds new stress/regression tests exercising shutdown semantics under concurrent traffic, refresh reopens, and capacity changes.
go/pools/smartconnpool/pool_test.go Adds a targeted regression test around close + waiters when an idle-cleanup connection is returned after capacity hits 0.

@arthurschreiber arthurschreiber changed the title smartconnpool: add remaining shutdown stress tests smartconnpool: add more shutdown stress tests May 21, 2026
arthurschreiber and others added 2 commits May 27, 2026 17:02
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>

# Conflicts:
#	go/pools/smartconnpool/pool_test.go
#	go/pools/smartconnpool/stress_test.go
A SetCapacity call queued on capacityMu while CloseWithContext holds it
would run right after Close released the mutex and silently write the
requested capacity into pool.capacity. The pool ended up closed
(IsOpen() == false) with a non-zero Capacity(), as the
TestStressRefreshReopenSetCapacityClose regression test demonstrates.

Guard SetCapacity with the same close-pointer check that reopen()
already uses, returning ErrConnPoolClosed when the pool is not open.

A pre-Open SetCapacity call in primeTxPoolWithConnection was already
a no-op today (pool.open() unconditionally stores config.maxCapacity
into pool.capacity), so it's moved after Open to actually pin the
intended capacity of 1.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Copilot AI review requested due to automatic review settings May 27, 2026 17:23
@arthurschreiber arthurschreiber changed the title smartconnpool: add more shutdown stress tests smartconnpool: reject SetCapacity on a closed pool + shutdown stress tests May 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.44%. Comparing base (70c7a72) to head (d7b87ae).
⚠️ Report is 278 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main   #20158       +/-   ##
===========================================
+ Coverage   69.67%   77.44%    +7.77%     
===========================================
  Files        1614      110     -1504     
  Lines      216793    16654   -200139     
===========================================
- Hits       151044    12898   -138146     
+ Misses      65749     3756    -61993     
Flag Coverage Δ
partial 77.44% <100.00%> (?)

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

☔ View full report in Codecov by Sentry.
📢 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

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.

2 participants