Skip to content

[AI-3960] Trim store suites' idle connections before each test - #2504

Merged
kanat merged 10 commits into
mainfrom
kanat/ai-3960-provider-app-lock-flake
Nov 26, 2026
Merged

kanat merged 10 commits into
mainfrom
kanat/ai-3960-provider-app-lock-flake

Conversation

@kanat

@kanat kanat commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Why

integration (store) failed on three unrelated PRs (runs 45709115232, 45704530765, and 45709022148 per the ticket) in TestShardedStoreSuite/TestProviderAppLocksOnOneAppNeverOverlapAcrossRouters: store: credential lock connection: FATAL: sorry, too many clients already (SQLSTATE=53300). Postgres logged the same FATAL in job 139538641250. CI's Postgres sets no max_connections (.github/workflows/integration.yml, services.postgres), so it uses the default, typically 100 (docs).

The cause is in the suite, not the lock. connect (internal/store/store.go) keeps as many idle connections as a pool may open (SetMaxIdleConns(conns)) for 5 minutes. The suite's two long-lived stores (s.store, s.other) open 40 + 40 + 16 connections each at most. After an earlier test runs many statements at once, those connections stay open into every later test. The test's 8 routers then add 3 connections each (shared, shard and lock pools) on top.

How it works

before                                   after
SetupTest                                SetupTest
 └─ empty tables                          ├─ trimIdle(s.store), trimIdle(s.other)
                                          │    idle connections past 2 per pool closed
                                          └─ empty tables
test starts with up to 40 idle            test starts with at most 2 idle
connections in s.store's shared pool      connections per pool

Connections to the suite's 3 databases, full TestShardedStoreSuite on a local Postgres:

when base 6a4c531 this PR
the test starts 54 (s.store shared pool: 40) 10 (s.store shared pool: 2)
inside the first router's lock 78 34
highest sample over the whole suite (every ~0.7 s) 94 51
the test alone (-testify.m) 4, then 28 inside the lock not measured

The lock itself is bounded: a waiter gives its connection back between tries (tryCredentialLock, credential_lock.go). Each router holds at most lockConns (16) lock connections. In the probe, the 8 routers added 8 lock connections, one each. So production does not exhaust connections through this lock, and the store code is unchanged.

keptIdle is 2, not 0. Closing every idle connection made each test dial Postgres again: the 176 ^TestAC tests took 32 s against 15 s without the change. With 2 they took 13 s against 17 s. The per-test time summed over the whole suite is unchanged: 356.7 s on base, 358.5 s on this PR.

Changes

  • StoreSuite.SetupTest and ShardedStoreSuite.SetupTest call trimIdle on the suite's stores. It drops each pool to keptIdle idle connections, then restores the limit connect set.
  • Every test that opens several routers runs through this setup and starts low too. For example: the 16-router TestCallbacksRacingWithOneStateConsumeItExactlyOnce and TestHandoffsRacingFromOneReadReplaceTheBlobExactlyOnce, the 8-router TestTwoRoutersSeedingAChangeAtOnceStoreOneRevision and TestCustomersRacingForOneProviderAppStoreOneRecord, and the s.pools(8) tests. routers = 8 and every assertion are unchanged.

Validation

  • New TestATestStartsWithoutTheIdleConnectionsAnEarlierOneLeft (both suites) and ...InTheOtherShard (sharded) fill the pools with 8 idle connections, run SetupTest, and check that each pool holds at most 2 and still keeps idle connections up to its size. They catch a setup that carries an earlier test's connections forward. Mutations: drop either trimIdle call, the trim, the restore, any pool from the list, or set keptIdle = 40. Each one fails a test (8 of 9). keptIdle = 0 survives: it changes only speed, measured above.
  • -count=50 of the test in the sharded suite passes. The touched tests pass in both suites on this branch merged with main.
  • TestShardedStoreSuite/TestEveryHotTableIsReadByIndex fails locally on base and on this branch (EXPLAIN plans on the shared local Postgres). It passed in CI job 139538641250. It is not related to this PR.
  • Not reproduced locally: the 53300 itself. The shared local Postgres allows 1000 connections. Unverified: how many connections CI held when the test failed.

🤖 Generated with Claude Code

kanat and others added 4 commits November 25, 2026 17:30
The suite's stores kept up to a pool's size of idle connections from an
earlier test, so TestProviderAppLocksOnOneAppNeverOverlapAcrossRouters
started in the sharded suite beside 40 of them and its 8 routers went past
CI Postgres's max_connections (AI-3960).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r-app-lock-flake

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closing every idle connection made each test dial Postgres again: 176
tests of the sharded suite took twice as long locally. Two per pool
keeps the usual test warm and still drops a burst's connections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kanat kanat changed the title [AI-3960] Start each store test without idle connections [AI-3960] Trim the store suites' idle connections before each test Nov 25, 2026
@kanat kanat changed the title [AI-3960] Trim the store suites' idle connections before each test [AI-3960] Trim store suites' idle connections before each test Nov 25, 2026

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: NO-GO

Reviewed e031bb5e9. The current main (51578aa) is already merged into it. CI: the PR is a draft, so go and integration (store) are skipped on this head (#2130). They run at gh pr ready.

Cause and fix: confirmed. A scratch probe ran the target's 8 routers and lock right after one burst test (TestCodesReservedAtOnceFromManyNumbersStopAtTheLinesCount) in the sharded suite:

at start routers open inside the lock s.store shared pool
base 51578aa 43 59 67 40
this PR 5 21 29 2

On base, one earlier burst carries 38 connections into the test. CI's 100 can be reached because s.store and s.other can carry up to 2 x (40+40+16) between tests.

CI headroom: no second source. integration.yml gives each job its own Postgres service with the default max_connections (100). It runs the store group with -p 1, so only internal/store is connected while it runs. One full internal/store run on this head, sampling every connection of the process to the server: DataMove 15, ServingRole 29, StoreSuite 45, ShardedStoreSuite 52. The 52 is a burst inside one test on s.store's shared pool, and the next SetupTest trims it. The target test sampled at most 35. Every TearDownSuite closes its stores. One connection to StoreSuite's database stays open through the sharded suite. With the trim, the target starts from at most 6 pools x 2 = 12 carried connections, plus 8 x 3 for its routers.

Hard rule: holds. The diff is store_test.go only. The target test and its siblings are untouched: routers = 8, same assertions. Restoring the idle limit affects no pool test: only the two new tests read or set the suite stores' pool settings. Every other Stats()/SetMaxOpenConns test opens its own store.

Mutations: 5 of my own plus the author's 9. Surviving: the restore value (R1.1, inline). Two survivors are accepted: keptIdle = 0, which only changes speed and was measured by the author, and trimming after emptyTables, which behaves the same.

Only TestEveryHotTableIsReadByIndex failed locally, in both suites. These are EXPLAIN plans on the shared local Postgres; this is a known base failure.

s.LessOrEqual(s.store.shard.Stats().OpenConnections, 2, "keptIdle")
s.LessOrEqual(s.store.locks.Stats().OpenConnections, 2, "keptIdle")
s.warm(s.store, 8)
s.Equal(8, s.store.locks.Stats().Idle, "and keeps them again, up to its size")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[Should fix] R1.1 "and keeps them again, up to its size" warms only 8 connections. The locks pool's size is 16 (lockConns) and the shared pool's is 40 (maxConns). So a trimIdle that restores any limit of 8 or more still passes. I checked db.SetMaxIdleConns(8) in place of the restore: both suites pass. That mutant leaves the suite's pools at 8 idle connections. It brings back the re-dialing that keptIdle exists to avoid.

Fix: after SetupTest, fill each pool to its own size, then assert Idle equals that size. I checked this row in a scratch copy:

for db, size := range map[*bun.DB]int{s.store.global: maxConns, s.store.locks: lockConns} {
	held := make([]*sql.Conn, size)
	for i := range held {
		conn, err := db.DB.Conn(s.ctx)
		s.Require().NoError(err)
		held[i] = conn
	}
	for _, conn := range held {
		s.Require().NoError(conn.Close())
	}
	s.Equal(size, db.Stats().Idle, "and keeps them again, up to its size")
}

Results: it passes on this head in both suites. It fails with SetMaxIdleConns(8) (40 vs 8, 16 vs 8) and with SetMaxIdleConns(lockConns) (40 vs 16). SetMaxIdleConns(maxConns) behaves the same as the real code, because database/sql clamps idle to MaxOpen.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 44ac740: TestATestStartsWithoutTheIdleConnectionsAnEarlierOneLeft (both suites) now fills every pool to its size (maxConns, lockConns) and asserts all idle. Mutants SetMaxIdleConns(8) and SetMaxIdleConns(lockConns) in trimIdle each fail it in StoreSuite and ShardedStoreSuite; head passes.

kanat and others added 2 commits November 25, 2026 18:36
The test filled 8 connections, so a restore to 8 idle passed; it now fills the
shared pools to maxConns and the locks pool to lockConns and asserts all idle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: GO

Delta round 2 of #2504 (AI-3960), head d1f3b5b (fix 44ac740 + clean merge of origin/main 7db619e). Test-only delta: store_test.go, +71/-2 vs origin/main.

PRIOR:

  • R1.1 (store_test.go idle-restore test filled only 8 connections) — fixed in 44ac740. warm() fills each pool to MaxOpenConnections; the test asserts Idle == maxConns (shared pools) and lockConns (locks pool), in StoreSuite and in the sharded other-shard test. Mutants re-run on head: SetMaxIdleConns(8) in trimIdle fails the setup tests in both suites (16 vs 8, 40 vs 8); SetMaxIdleConns(lockConns) fails them too (40 vs 16). Head passes both suites.

Checks on head (ledger details: reviewer-2.md):

  • setup tests + TestProviderAppLocksOnOneAppNeverOverlapAcrossRouters, both suites: PASS; -race -count=20: PASS, no data race; -cpu 1 -count=50: PASS.
  • go vet and go vet -tags integration on internal/store: clean; gofmt clean.
  • CI: draft; integration (store) and go skipping (#2130).

NEW FINDINGS: none.

@kanat
kanat marked this pull request as ready for review November 25, 2026 23:48
kanat and others added 2 commits November 25, 2026 19:08
Filling every pool of every store at once held 40 shared + 16 lock connections
per store and shard, past CI Postgres's max_connections of 100 (R2.1). Each pool
is now filled, checked, trimmed and refilled alone, then trimmed back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kanat

kanat commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

R2.1 (CI job 139589626332: setup tests filled every pool of every store at once, 40 shared + 16 lock connections each, past CI Postgres's max_connections of 100) is fixed in a422946. Each setup test now fills one pool, checks it, trims it, refills it, and trims it back before the next pool. Pool peak measured with a pg_stat_activity sampler (0.2 s, both suites, setup tests + the three target tests): d1f3b5b max 102, new head max 52 (also 52 after merging main 9d3c24a). CI runs one DB set per suite with -p 1. Both R1.1 mutants (SetMaxIdleConns(8), SetMaxIdleConns(lockConns) in the restore) still fail the setup tests in both suites. Head is adadd32.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: GO

Delta round 3, reviewed head adadd32 (fix a422946 + merge of origin/main 9d3c24a). After that review, origin/main 5a584ab was merged as 0e4f9b0; delta-kind.sh adadd3260 0e4f9b05c origin/main prints trivial (patch unchanged), so this verdict covers 0e4f9b0 too. The store runs below are on adadd32.

PRIOR

  • R1.1 (setup tests must fill each pool to its size): fixed. Mutants on the trimPool restore line, run on the setup tests of both suites: SetMaxIdleConns(8) fails StoreSuite and ShardedStoreSuite OneLeft and ShardedStoreSuite OneLeftInTheOtherShard; SetMaxIdleConns(lockConns) fails the same three. Restored, file clean.
  • R2.1 (CI connection limit, "too many clients"): fixed. pg_stat_activity sampled every ~0.1 s on the five target tests in both suites: max 52. -race -count=20 on the same tests: max 53, no DATA RACE, ok. ProviderAppLocks -cpu 1 -count=50 (sharded): ok. -count=50 (sharded): ok, max 51. CI's Postgres keeps the default max_connections of 100.

NEW FINDINGS
None.

UNVERIFIED

  • Full StoreSuite, ShardedStoreSuite and the whole internal/store package x3 were not rerun this round; reviewer-1 ran the full package at e031bb5 (only the known EXPLAIN failure).
  • The base-only merge 0e4f9b0 brings store test changes that I did not run.
  • CI on the new head was still running when read.

Details: /Users/kanat/.claude/ship-task/Vision-Agents/AI-3960/reviewer-3.md

@kanat
kanat merged commit 85c03bf into main Nov 26, 2026
13 checks passed
@kanat
kanat deleted the kanat/ai-3960-provider-app-lock-flake branch November 26, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant