Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-api): wait for pg clients to close before terminating test backends

Alfredo Lopezd791efeb6c42
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 7bc4125fe..f0f315c32 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.687",
+ "version": "0.1.688",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.687",
+ "version": "0.1.688",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index d2e83ca3a..bf10b20e9 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.687",
+ "version": "0.1.688",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/db.listGrantsReferencingModel.realPostgres.integration.test.ts b/control-api/test/db.listGrantsReferencingModel.realPostgres.integration.test.ts
index 3974ec3ed..eb1dab8f0 100644
--- a/control-api/test/db.listGrantsReferencingModel.realPostgres.integration.test.ts
+++ b/control-api/test/db.listGrantsReferencingModel.realPostgres.integration.test.ts
@@ -3,6 +3,7 @@ import { randomBytes } from 'node:crypto'
import { Pool } from 'pg'
import { initDb } from '../src/db.js'
import { listGrantsReferencingModel } from '../src/services/pluginWorkloadSdkDb.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// FIX C (T1/T3): exercise the REAL jsonb `@>` containment of
// listGrantsReferencingModel against a real Postgres — the risky part the unit
@@ -63,7 +64,7 @@ describeRealPostgres('listGrantsReferencingModel jsonb containment on real Postg
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid)
diff --git a/control-api/test/db.pluginWorkloadSdkRuntimeContract.realPostgres.integration.test.ts b/control-api/test/db.pluginWorkloadSdkRuntimeContract.realPostgres.integration.test.ts
index 91599a8b3..028874521 100644
--- a/control-api/test/db.pluginWorkloadSdkRuntimeContract.realPostgres.integration.test.ts
+++ b/control-api/test/db.pluginWorkloadSdkRuntimeContract.realPostgres.integration.test.ts
@@ -8,6 +8,7 @@ import {
} from '../src/services/pluginWorkloadSdkFinalization.js'
import { tokenUsagePayload } from '../src/services/tracing/usageProjection.js'
import type { LlmUsageEvent } from '../src/services/usageEvents.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -157,7 +158,7 @@ describeRealPostgres('Plugin Workload SDK runtime-contract upgrade on real Postg
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid)
diff --git a/control-api/test/db.realPostgresMigration.integration.test.ts b/control-api/test/db.realPostgresMigration.integration.test.ts
index 99c70df25..7c02d431d 100644
--- a/control-api/test/db.realPostgresMigration.integration.test.ts
+++ b/control-api/test/db.realPostgresMigration.integration.test.ts
@@ -30,6 +30,7 @@ import { PostgresGovernedSessionReplayRepository } from '../src/services/tracing
import { projectAcceptedUsageEvents } from '../src/services/tracing/usageProjection.js'
import { ingestUsageEventsInTransaction } from '../src/services/usageEvents.js'
import { signRpcAccessToken } from '../src/utils/auth/rpcAuthToken.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
type PrivilegeExpectation = Record<string, Set<string>>
@@ -138,7 +139,7 @@ describeRealPostgres('control-api real Postgres migrations', () => {
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid)
diff --git a/control-api/test/gfsAuditDecisionEvidence.realPostgres.integration.test.ts b/control-api/test/gfsAuditDecisionEvidence.realPostgres.integration.test.ts
index 5a13e3d5e..56253d535 100644
--- a/control-api/test/gfsAuditDecisionEvidence.realPostgres.integration.test.ts
+++ b/control-api/test/gfsAuditDecisionEvidence.realPostgres.integration.test.ts
@@ -2,6 +2,7 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { randomBytes } from 'node:crypto'
import { Pool, type PoolClient } from 'pg'
import { CONTROL_API_MIGRATIONS, initDb } from '../src/db.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -49,7 +50,7 @@ describeRealPostgres('GFS typed audit decision evidence', () => {
}, 60_000)
afterAll(async () => {
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/gfsBlobMigration.realPostgres.integration.test.ts b/control-api/test/gfsBlobMigration.realPostgres.integration.test.ts
index c04b50425..2be7c4ea7 100644
--- a/control-api/test/gfsBlobMigration.realPostgres.integration.test.ts
+++ b/control-api/test/gfsBlobMigration.realPostgres.integration.test.ts
@@ -2,6 +2,7 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { randomBytes, randomUUID } from 'node:crypto'
import { Pool } from 'pg'
import { initDb } from '../src/db.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -35,7 +36,7 @@ describeRealPostgres('GFS immutable blob migration on real PostgreSQL', () => {
}, 60_000)
afterAll(async () => {
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/gfsBlobReconciliation.realPostgres.integration.test.ts b/control-api/test/gfsBlobReconciliation.realPostgres.integration.test.ts
index 9b66ae892..466b1103d 100644
--- a/control-api/test/gfsBlobReconciliation.realPostgres.integration.test.ts
+++ b/control-api/test/gfsBlobReconciliation.realPostgres.integration.test.ts
@@ -11,6 +11,7 @@ import type { GfsMetrics } from '../../gfs-controller/src/metrics.js'
import { BlobStore } from '../../gfs-controller/src/storage/blobStore.js'
import { resolveBlobKeyPath, resolveBlobPath } from '../../gfs-controller/src/storage/paths.js'
import { initDb } from '../src/db.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// This suite closes the reconciler's HIGH coverage gap: reconcileExpiredBlobs is
// the sole backstop for the write/copy paths' swallowed cleanup errors, yet its
@@ -101,7 +102,7 @@ describeRealPostgres('GFS blob reconciliation on real PostgreSQL + on-disk BlobS
})
afterAll(async () => {
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
await rm(blobRoot, { recursive: true, force: true }).catch(() => undefined)
if (!adminPool) return
await adminPool.query(
diff --git a/control-api/test/gfsCopy.realPostgres.integration.test.ts b/control-api/test/gfsCopy.realPostgres.integration.test.ts
index 50a7399a0..6b2de688c 100644
--- a/control-api/test/gfsCopy.realPostgres.integration.test.ts
+++ b/control-api/test/gfsCopy.realPostgres.integration.test.ts
@@ -11,6 +11,7 @@ import { PgResourceStore } from '../../gfs-controller/src/db/resourceStore.js'
import { GfsWriteService, PgTransactor } from '../../gfs-controller/src/db/writeStore.js'
import { BlobStore } from '../../gfs-controller/src/storage/blobStore.js'
import { initDb } from '../src/db.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -154,7 +155,7 @@ describeRealPostgres('GFS Copy publication on real PostgreSQL and filesystem blo
}, 60_000)
afterAll(async () => {
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
await rm(blobRoot, { recursive: true, force: true }).catch(() => undefined)
if (!adminPool) return
await adminPool.query(
diff --git a/control-api/test/gfsReaderRole.realPostgres.integration.test.ts b/control-api/test/gfsReaderRole.realPostgres.integration.test.ts
index e212bfd45..af24695ed 100644
--- a/control-api/test/gfsReaderRole.realPostgres.integration.test.ts
+++ b/control-api/test/gfsReaderRole.realPostgres.integration.test.ts
@@ -3,6 +3,7 @@ import { randomBytes } from 'node:crypto'
import { Pool } from 'pg'
import { createPermissionStoreProbe } from '../../gfs-controller/src/authz/storeProbe.js'
import { CONTROL_API_MIGRATIONS, initDb } from '../src/db.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -62,12 +63,12 @@ describeRealPostgres('GFS reader and writer login isolation', () => {
}, 60_000)
afterAll(async () => {
- await readerPool?.end()
- await writerPool?.end()
+ await endPoolAndWaitForClients(readerPool)
+ await endPoolAndWaitForClients(writerPool)
await pool?.query('ALTER ROLE gfs_controller NOLOGIN NOINHERIT').catch(() => undefined)
await pool?.query('ALTER ROLE gfs_controller_reader NOLOGIN NOINHERIT').catch(() => undefined)
await pool?.query(`DROP ROLE IF EXISTS ${quoteIdent(inheritedRole)}`).catch(() => undefined)
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/gfsReadiness.realPostgres.integration.test.ts b/control-api/test/gfsReadiness.realPostgres.integration.test.ts
index c91d4fe91..5609137a1 100644
--- a/control-api/test/gfsReadiness.realPostgres.integration.test.ts
+++ b/control-api/test/gfsReadiness.realPostgres.integration.test.ts
@@ -4,6 +4,7 @@ import { Pool } from 'pg'
import type { PoolClient } from 'pg'
import { createPermissionStoreProbe } from '../../gfs-controller/src/authz/storeProbe.js'
import { CONTROL_API_MIGRATIONS, assertDbReady, initDb } from '../src/db.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -59,7 +60,7 @@ describeRealPostgres('GFS Phase 0 real PostgreSQL readiness', () => {
}, 60_000)
afterAll(async () => {
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/gfsStructuralIntegrity.realPostgres.integration.test.ts b/control-api/test/gfsStructuralIntegrity.realPostgres.integration.test.ts
index f3eb83983..7601f7646 100644
--- a/control-api/test/gfsStructuralIntegrity.realPostgres.integration.test.ts
+++ b/control-api/test/gfsStructuralIntegrity.realPostgres.integration.test.ts
@@ -11,6 +11,7 @@ import {
import { initDb } from '../src/db.js'
import type { GfsCaller } from '../src/routes/gfs/grants.js'
import { applyResourcePatch } from '../src/routes/gfs/resources.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -153,7 +154,7 @@ describeRealPostgres('GFS Phase 0 real PostgreSQL integrity', () => {
}, 60_000)
afterAll(async () => {
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/gfsUploadFinalizer.realPostgres.integration.test.ts b/control-api/test/gfsUploadFinalizer.realPostgres.integration.test.ts
index ec09b7c18..8445ff051 100644
--- a/control-api/test/gfsUploadFinalizer.realPostgres.integration.test.ts
+++ b/control-api/test/gfsUploadFinalizer.realPostgres.integration.test.ts
@@ -15,6 +15,7 @@ import type {
UploadSessionRow,
} from '../../gfs-controller/src/upload/uploadSession.js'
import { initDb } from '../src/db.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -117,7 +118,7 @@ describeRealPostgres('GFS upload finalizer on real PostgreSQL + BlobStore', () =
await pool
?.query('DELETE FROM gfs_upload_sessions WHERE drive = $1', [drive])
.catch(() => undefined)
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
await rm(storageRoot, { recursive: true, force: true }).catch(() => undefined)
await rm(blobRoot, { recursive: true, force: true }).catch(() => undefined)
if (!adminPool) return
diff --git a/control-api/test/gfsUploadSession.realPostgres.integration.test.ts b/control-api/test/gfsUploadSession.realPostgres.integration.test.ts
index 5af683795..0a8c370fa 100644
--- a/control-api/test/gfsUploadSession.realPostgres.integration.test.ts
+++ b/control-api/test/gfsUploadSession.realPostgres.integration.test.ts
@@ -19,6 +19,7 @@ import {
type UploadSessionServiceDeps,
} from '../../gfs-controller/src/upload/uploadSession.js'
import { initDb } from '../src/db.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -162,7 +163,7 @@ describeRealPostgres('GFS Upload v2 session engine on real PostgreSQL', () => {
await pool
?.query('DELETE FROM gfs_upload_sessions WHERE drive = $1', [drive])
.catch(() => undefined)
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
await rm(tempRoot, { recursive: true, force: true }).catch(() => undefined)
if (!adminPool) return
await adminPool.query(
diff --git a/control-api/test/oauth.dcrCredential.installIdentity.realPostgres.integration.test.ts b/control-api/test/oauth.dcrCredential.installIdentity.realPostgres.integration.test.ts
index 7338b0709..f2b9b22e1 100644
--- a/control-api/test/oauth.dcrCredential.installIdentity.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.dcrCredential.installIdentity.realPostgres.integration.test.ts
@@ -45,6 +45,7 @@ import {
dcrPilot,
makeDiscoveryTransport,
} from './fixtures/remoteOAuthDiscovery.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
@@ -115,7 +116,7 @@ describeRealPostgres(
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.deleteOAuthGrantsForServer.realPostgres.integration.test.ts b/control-api/test/oauth.deleteOAuthGrantsForServer.realPostgres.integration.test.ts
index 9f44ae575..b3f2274c4 100644
--- a/control-api/test/oauth.deleteOAuthGrantsForServer.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.deleteOAuthGrantsForServer.realPostgres.integration.test.ts
@@ -11,6 +11,7 @@ import {
oauthGrantExists,
upsertOAuthGrant,
} from '../src/oauth/store.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
/**
* R1-L3 (T4) — the server-teardown purge (DEC-R2) observed as SURVIVING ROWS, not
@@ -54,7 +55,7 @@ describeRealPostgres('deleteOAuthGrantsForServer — full server-scoped wipe (re
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.dynamicClientStore.installIdentity.realPostgres.integration.test.ts b/control-api/test/oauth.dynamicClientStore.installIdentity.realPostgres.integration.test.ts
index 463e04f86..4661bc785 100644
--- a/control-api/test/oauth.dynamicClientStore.installIdentity.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.dynamicClientStore.installIdentity.realPostgres.integration.test.ts
@@ -17,6 +17,7 @@ import {
upsertDynamicClient,
} from '../src/oauth/dynamicClientStore.js'
import { deriveOAuthEncryptionKey } from '../src/oauth/encryption.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
/**
* Install-identity store primitives, observed as SURVIVING ROWS and returned
@@ -135,7 +136,7 @@ describeRealPostgres('dynamicClientStore install identity (real Postgres)', () =
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.legacyGrantLaneFence.realPostgres.integration.test.ts b/control-api/test/oauth.legacyGrantLaneFence.realPostgres.integration.test.ts
index 2796e439d..d99b48b2f 100644
--- a/control-api/test/oauth.legacyGrantLaneFence.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.legacyGrantLaneFence.realPostgres.integration.test.ts
@@ -53,6 +53,7 @@ import {
remoteDiscovery,
remoteOAuth,
} from './fixtures/legacyOAuthGrant.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
@@ -92,7 +93,7 @@ describeRealPostgres(
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.mcpDisconnect.realPostgres.integration.test.ts b/control-api/test/oauth.mcpDisconnect.realPostgres.integration.test.ts
index c3a03147e..3e5ff9e8d 100644
--- a/control-api/test/oauth.mcpDisconnect.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.mcpDisconnect.realPostgres.integration.test.ts
@@ -12,6 +12,7 @@ import {
oauthGrantExists,
upsertOAuthGrant,
} from '../src/oauth/store.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
// T1/T4 — the disconnect composition (buildMcpServerGrantKey → deleteOAuthGrant)
@@ -72,7 +73,7 @@ describeRealPostgres('mcp-server grant disconnect composition (real Postgres)',
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.mcpServerOAuthTeardown.realPostgres.integration.test.ts b/control-api/test/oauth.mcpServerOAuthTeardown.realPostgres.integration.test.ts
index 28262eb4e..b68a8f570 100644
--- a/control-api/test/oauth.mcpServerOAuthTeardown.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.mcpServerOAuthTeardown.realPostgres.integration.test.ts
@@ -19,6 +19,7 @@ import {
} from '../src/oauth/store.js'
import { rootLogger } from '../src/observability/logger.js'
import { makeDcrTransport } from './fixtures/remoteOAuthDiscovery.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
/**
* R3-H5 (T1/T3/T4) — the uninstall OAuth teardown fenced by the CR uid, observed as
@@ -59,7 +60,7 @@ describeRealPostgres('teardownMcpServerOAuthState — fenced by cr_uid (real Pos
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
@@ -274,7 +275,7 @@ describeRealPostgres('grant cr_uid sealing (D-T3, real Postgres)', () => {
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.proactiveRefresh.realPostgres.integration.test.ts b/control-api/test/oauth.proactiveRefresh.realPostgres.integration.test.ts
index cbfe2a003..da7a98e09 100644
--- a/control-api/test/oauth.proactiveRefresh.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.proactiveRefresh.realPostgres.integration.test.ts
@@ -32,6 +32,7 @@ import {
} from '../src/oauth/store.js'
import { getAccessToken } from '../src/oauth/tokenHelper.js'
import { type McpServerResource, normalizeMcpServerOwnerDecl } from '../src/routes/mcpOauth.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
@@ -71,7 +72,7 @@ describeRealPostgres('oauth proactive refresh — enumeration + claim (real Post
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.reactiveRefresh.installIdentity.realPostgres.integration.test.ts b/control-api/test/oauth.reactiveRefresh.installIdentity.realPostgres.integration.test.ts
index fcd00fd40..05808df92 100644
--- a/control-api/test/oauth.reactiveRefresh.installIdentity.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.reactiveRefresh.installIdentity.realPostgres.integration.test.ts
@@ -26,6 +26,7 @@ import { resultToOutcome } from '../src/oauth/proactiveRefreshPolicy.js'
import { getAccessTokenReactive } from '../src/oauth/reactiveTokenHelper.js'
import { getOAuthGrant, upsertOAuthGrant } from '../src/oauth/store.js'
import { type McpServerResource, normalizeMcpServerOwnerDecl } from '../src/routes/mcpOauth.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
@@ -85,7 +86,7 @@ describeRealPostgres(
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.reactiveRefreshLock.realPostgres.integration.test.ts b/control-api/test/oauth.reactiveRefreshLock.realPostgres.integration.test.ts
index d29448b5b..a358c6862 100644
--- a/control-api/test/oauth.reactiveRefreshLock.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.reactiveRefreshLock.realPostgres.integration.test.ts
@@ -36,6 +36,7 @@ import {
} from '../src/oauth/store.js'
import { getAccessToken } from '../src/oauth/tokenHelper.js'
import { type McpServerResource, normalizeMcpServerOwnerDecl } from '../src/routes/mcpOauth.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
@@ -74,7 +75,7 @@ describeRealPostgres('oauth reactive refresh — row-lock serialization (real Po
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.store.installIdentityFence.realPostgres.integration.test.ts b/control-api/test/oauth.store.installIdentityFence.realPostgres.integration.test.ts
index 3d14de756..3b4f91725 100644
--- a/control-api/test/oauth.store.installIdentityFence.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.store.installIdentityFence.realPostgres.integration.test.ts
@@ -34,6 +34,7 @@ import {
oauthGrantExists,
upsertOAuthGrant,
} from '../src/oauth/store.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
@@ -116,7 +117,7 @@ describeRealPostgres('grant readers fenced by the live installation uid (real Po
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.store.sharedGrants.realPostgres.integration.test.ts b/control-api/test/oauth.store.sharedGrants.realPostgres.integration.test.ts
index 1a3de3ba8..3976e885e 100644
--- a/control-api/test/oauth.store.sharedGrants.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.store.sharedGrants.realPostgres.integration.test.ts
@@ -12,6 +12,7 @@ import {
refreshOAuthGrantTokens,
upsertOAuthGrant,
} from '../src/oauth/store.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// T1/T5 — shared-identity grant semantics derived from the REAL producer
// (real ON CONFLICT DO NOTHING + CHECK constraints), not a hand-built fixture.
@@ -48,7 +49,7 @@ describeRealPostgres('oauth store — shared grants (real Postgres)', () => {
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/oauth.store.userGrantsListing.realPostgres.integration.test.ts b/control-api/test/oauth.store.userGrantsListing.realPostgres.integration.test.ts
index 09f0bc614..6d56d6e9f 100644
--- a/control-api/test/oauth.store.userGrantsListing.realPostgres.integration.test.ts
+++ b/control-api/test/oauth.store.userGrantsListing.realPostgres.integration.test.ts
@@ -11,6 +11,7 @@ import {
listUserOAuthGrants,
upsertOAuthGrant,
} from '../src/oauth/store.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// T1/T3/T4/T5 (spec 04 U1/U3) — the owner-generalised grant listing exercised
// against grants written by the REAL producer (`upsertOAuthGrant`), never a
@@ -54,7 +55,7 @@ describeRealPostgres('listUserOAuthGrants / listUserGrantsForServer (real Postgr
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/pluginWorkloadSdkGrantUpdateNotify.realPostgres.integration.test.ts b/control-api/test/pluginWorkloadSdkGrantUpdateNotify.realPostgres.integration.test.ts
index 0fbb3eb9e..6394b2452 100644
--- a/control-api/test/pluginWorkloadSdkGrantUpdateNotify.realPostgres.integration.test.ts
+++ b/control-api/test/pluginWorkloadSdkGrantUpdateNotify.realPostgres.integration.test.ts
@@ -2,6 +2,7 @@ import { afterAll, beforeAll, expect, vi, describe as vitestDescribe } from 'vit
import { it } from 'vitest'
import { randomBytes, randomUUID } from 'node:crypto'
import { Pool, type PoolClient } from 'pg'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// issue #375 M3 (jozer review): controllable PASSTHROUGH seam. Everything stays
// real (the actual append implementation runs) until a test arms `failNext`,
@@ -111,9 +112,9 @@ describeRealPostgres(
/* teardown */
}
listenClient?.release()
- await listenPool?.end()
- await db?.pool.end()
- await db?.rateLimitPool.end()
+ await endPoolAndWaitForClients(listenPool)
+ await endPoolAndWaitForClients(db?.pool)
+ await endPoolAndWaitForClients(db?.rateLimitPool)
if (previousPgConnectionString === undefined) {
delete process.env.CONTROL_API_PG_CONNECTION_STRING
} else {
diff --git a/control-api/test/pluginWorkloadSdkPolicyReview.realPostgres.integration.test.ts b/control-api/test/pluginWorkloadSdkPolicyReview.realPostgres.integration.test.ts
index 31129675c..2e2464eae 100644
--- a/control-api/test/pluginWorkloadSdkPolicyReview.realPostgres.integration.test.ts
+++ b/control-api/test/pluginWorkloadSdkPolicyReview.realPostgres.integration.test.ts
@@ -6,6 +6,7 @@ import {
addPluginWorkloadSdkPolicyReviewProvenance,
repairPluginWorkloadSdkLegacyGrantPolicies,
} from '../src/services/pluginWorkloadSdkSchema.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -33,7 +34,7 @@ describeRealPostgres('Plugin Workload SDK policy review provenance on real Postg
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid)
diff --git a/control-api/test/pluginWorkloadSdkSteplessQuota.realPostgres.integration.test.ts b/control-api/test/pluginWorkloadSdkSteplessQuota.realPostgres.integration.test.ts
index bbcc366de..35808cd86 100644
--- a/control-api/test/pluginWorkloadSdkSteplessQuota.realPostgres.integration.test.ts
+++ b/control-api/test/pluginWorkloadSdkSteplessQuota.realPostgres.integration.test.ts
@@ -3,6 +3,7 @@ import { randomBytes, randomUUID } from 'node:crypto'
import { Pool } from 'pg'
import type { PluginWorkloadSdkFamily } from '../src/services/pluginWorkloadSdkDb.js'
import type { McpHostAccessClaims } from '../src/utils/auth/mcpHostJwtToken.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
/**
* Stepless / SDK-only quota regression on real PostgreSQL (issue #348,
@@ -109,8 +110,8 @@ describeRealPostgres(
})
afterAll(async () => {
- await db?.pool.end()
- await db?.rateLimitPool.end()
+ await endPoolAndWaitForClients(db?.pool)
+ await endPoolAndWaitForClients(db?.rateLimitPool)
if (previousPgConnectionString === undefined) {
delete process.env.CONTROL_API_PG_CONNECTION_STRING
} else {
diff --git a/control-api/test/routes.externalGfsRateLimitFailClosed.realPostgres.integration.test.ts b/control-api/test/routes.externalGfsRateLimitFailClosed.realPostgres.integration.test.ts
index 8e11bfa78..b4e6c9bcd 100644
--- a/control-api/test/routes.externalGfsRateLimitFailClosed.realPostgres.integration.test.ts
+++ b/control-api/test/routes.externalGfsRateLimitFailClosed.realPostgres.integration.test.ts
@@ -2,6 +2,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { randomBytes, randomUUID } from 'node:crypto'
import { Pool, type PoolClient } from 'pg'
import request from 'supertest'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// The external GFS limiter's Postgres backend, against real PostgreSQL at the
// production pool bounds: the CORE_POOL_* and RATE_LIMIT_POOL_* variables are
@@ -118,8 +119,8 @@ describeRealPostgres('external GFS rate limiter backend (real PostgreSQL)', () =
if (value === undefined) delete process.env[key]
else process.env[key] = value
}
- await corePool?.end().catch(() => {})
- await limiterPool?.end().catch(() => {})
+ await endPoolAndWaitForClients(corePool).catch(() => {})
+ await endPoolAndWaitForClients(limiterPool).catch(() => {})
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/routes.externalGfsRateLimitStress.realPostgres.integration.test.ts b/control-api/test/routes.externalGfsRateLimitStress.realPostgres.integration.test.ts
index 65d56cd43..79a59d167 100644
--- a/control-api/test/routes.externalGfsRateLimitStress.realPostgres.integration.test.ts
+++ b/control-api/test/routes.externalGfsRateLimitStress.realPostgres.integration.test.ts
@@ -4,6 +4,7 @@ import { readFileSync } from 'node:fs'
import http from 'node:http'
import type { AddressInfo } from 'node:net'
import { Pool } from 'pg'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// Concurrent users at the external GFS rate limits, through the production
// Express app against real PostgreSQL. Config, the core and limiter pools, both limiters,
@@ -226,8 +227,8 @@ describeRealPostgres('external GFS rate limits under concurrent load (real Postg
if (value === undefined) delete process.env[key]
else process.env[key] = value
}
- await corePool?.end().catch(() => {})
- await limiterPool?.end().catch(() => {})
+ await endPoolAndWaitForClients(corePool).catch(() => {})
+ await endPoolAndWaitForClients(limiterPool).catch(() => {})
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/routes.llmModelGrantRace.realPostgres.integration.test.ts b/control-api/test/routes.llmModelGrantRace.realPostgres.integration.test.ts
index a07bc463c..0a319eb20 100644
--- a/control-api/test/routes.llmModelGrantRace.realPostgres.integration.test.ts
+++ b/control-api/test/routes.llmModelGrantRace.realPostgres.integration.test.ts
@@ -2,6 +2,7 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi }
import { randomBytes } from 'node:crypto'
import { Pool } from 'pg'
import request from 'supertest'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// R1-H3 fase 2 (llm-model reductor ↔ grant upsert) — regression test for the
// TOCTOU race the per-MODEL advisory lock closes on the grant seam (mini-spec
@@ -168,9 +169,9 @@ describeRealPostgres('llm-model reductor ↔ grant upsert serialization (R1-H3 f
afterAll(async () => {
if (previousPgEnv === undefined) delete process.env.CONTROL_API_PG_CONNECTION_STRING
else process.env.CONTROL_API_PG_CONNECTION_STRING = previousPgEnv
- await racePool?.end().catch(() => {})
- await corePool?.end().catch(() => {})
- await limiterPool?.end().catch(() => {})
+ await endPoolAndWaitForClients(racePool).catch(() => {})
+ await endPoolAndWaitForClients(corePool).catch(() => {})
+ await endPoolAndWaitForClients(limiterPool).catch(() => {})
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/routes.llmModelHostRace.realPostgres.integration.test.ts b/control-api/test/routes.llmModelHostRace.realPostgres.integration.test.ts
index df7adaaeb..8e7df1231 100644
--- a/control-api/test/routes.llmModelHostRace.realPostgres.integration.test.ts
+++ b/control-api/test/routes.llmModelHostRace.realPostgres.integration.test.ts
@@ -2,6 +2,7 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi }
import { randomBytes } from 'node:crypto'
import { Pool } from 'pg'
import request from 'supertest'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// R1-H3 fase 1 (host↔model) — regression test for the TOCTOU race the advisory
// lock closes (mini-spec §7 / T3 / T4). It drives the race through the REAL route
@@ -173,8 +174,8 @@ describeRealPostgres('llm-model reductor ↔ host referencer serialization (R1-H
if (previousPgEnv === undefined) delete process.env.CONTROL_API_PG_CONNECTION_STRING
else process.env.CONTROL_API_PG_CONNECTION_STRING = previousPgEnv
// Drain the module pool's connections first so DROP DATABASE has no live users.
- await corePool?.end().catch(() => {})
- await limiterPool?.end().catch(() => {})
+ await endPoolAndWaitForClients(corePool).catch(() => {})
+ await endPoolAndWaitForClients(limiterPool).catch(() => {})
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/routes.mcpOauth.grantsExists.realPostgres.integration.test.ts b/control-api/test/routes.mcpOauth.grantsExists.realPostgres.integration.test.ts
index 46d5dfa9a..26baaf9cf 100644
--- a/control-api/test/routes.mcpOauth.grantsExists.realPostgres.integration.test.ts
+++ b/control-api/test/routes.mcpOauth.grantsExists.realPostgres.integration.test.ts
@@ -11,6 +11,7 @@ import {
upsertOAuthGrant,
} from '../src/oauth/store.js'
import { resolveBatchGrantExistence } from '../src/routes/mcpOauth.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
// T1 — the batch grant-existence resolver (mini-spec 13 §4.1) exercised against
@@ -51,7 +52,7 @@ describeRealPostgres('resolveBatchGrantExistence (real Postgres)', () => {
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/routes.mcpOauth.legacyGrantLaneFence.realPostgres.integration.test.ts b/control-api/test/routes.mcpOauth.legacyGrantLaneFence.realPostgres.integration.test.ts
index 815764a15..ff1277bf9 100644
--- a/control-api/test/routes.mcpOauth.legacyGrantLaneFence.realPostgres.integration.test.ts
+++ b/control-api/test/routes.mcpOauth.legacyGrantLaneFence.realPostgres.integration.test.ts
@@ -37,6 +37,7 @@ import {
legacyConsent,
reinstallServer,
} from './fixtures/legacyOAuthGrant.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
// The broker reads grants through the app's shared pool; point it at this suite's
@@ -114,7 +115,7 @@ describeRealPostgres('legacy grants seen by the broker, gate and sweep (real Pos
afterAll(async () => {
testDb.pool = undefined
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/routes.mcpServerUninstall.repairRequired.realPostgres.integration.test.ts b/control-api/test/routes.mcpServerUninstall.repairRequired.realPostgres.integration.test.ts
index 451a8c932..bde468a6c 100644
--- a/control-api/test/routes.mcpServerUninstall.repairRequired.realPostgres.integration.test.ts
+++ b/control-api/test/routes.mcpServerUninstall.repairRequired.realPostgres.integration.test.ts
@@ -5,6 +5,7 @@ import { Pool } from 'pg'
import request from 'supertest'
import type { DbClient } from '../src/db.js'
import type { K8sGateway } from '../src/k8s.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
/**
@@ -82,7 +83,7 @@ describeRealPostgres('generic McpServer uninstall — repair_required (real Post
afterAll(async () => {
dbHolder.target = null
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/routes.rateLimitProcessMemory.realPostgres.integration.test.ts b/control-api/test/routes.rateLimitProcessMemory.realPostgres.integration.test.ts
index 99ea343a0..6041980ae 100644
--- a/control-api/test/routes.rateLimitProcessMemory.realPostgres.integration.test.ts
+++ b/control-api/test/routes.rateLimitProcessMemory.realPostgres.integration.test.ts
@@ -2,6 +2,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { randomBytes, randomUUID } from 'node:crypto'
import { Pool, type PoolClient } from 'pg'
import request from 'supertest'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// rateLimitMiddleware's 'process-memory' mode against real PostgreSQL: with
// every limiter pool connection held, a real route's limiter cannot count in
@@ -115,8 +116,8 @@ describeRealPostgres('rateLimitMiddleware process-memory mode (real PostgreSQL)'
if (value === undefined) delete process.env[key]
else process.env[key] = value
}
- await corePool?.end().catch(() => {})
- await limiterPool?.end().catch(() => {})
+ await endPoolAndWaitForClients(corePool).catch(() => {})
+ await endPoolAndWaitForClients(limiterPool).catch(() => {})
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/routes.registryUninstall.realPostgres.integration.test.ts b/control-api/test/routes.registryUninstall.realPostgres.integration.test.ts
index 03718ee53..112f117a7 100644
--- a/control-api/test/routes.registryUninstall.realPostgres.integration.test.ts
+++ b/control-api/test/routes.registryUninstall.realPostgres.integration.test.ts
@@ -5,6 +5,7 @@ import { Pool } from 'pg'
import request from 'supertest'
import type { DbClient } from '../src/db.js'
import type { K8sGateway } from '../src/k8s.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
/**
@@ -138,7 +139,7 @@ describeRealPostgres('registry uninstall tears OAuth grants down (real Postgres)
afterAll(async () => {
dbHolder.target = null
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/services.access.mcpConnectors.realPostgres.integration.test.ts b/control-api/test/services.access.mcpConnectors.realPostgres.integration.test.ts
index 84fe8874b..403c18b45 100644
--- a/control-api/test/services.access.mcpConnectors.realPostgres.integration.test.ts
+++ b/control-api/test/services.access.mcpConnectors.realPostgres.integration.test.ts
@@ -7,6 +7,7 @@ import { initDb } from '../src/db.js'
import { deriveOAuthEncryptionKey } from '../src/oauth/encryption.js'
import { bootstrapSharedOAuthGrant, upsertOAuthGrant } from '../src/oauth/store.js'
import { resolveConnectorsForAgents } from '../src/services/access/mcpInvocable.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
// T1 — the connectors classifier exercised end-to-end against grant state built
@@ -45,7 +46,7 @@ describeRealPostgres('resolveConnectorsForAgents tri-state (real Postgres)', ()
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/services.access.mcpInvocable.grantGate.realPostgres.integration.test.ts b/control-api/test/services.access.mcpInvocable.grantGate.realPostgres.integration.test.ts
index 868aa44b0..0dde8b506 100644
--- a/control-api/test/services.access.mcpInvocable.grantGate.realPostgres.integration.test.ts
+++ b/control-api/test/services.access.mcpInvocable.grantGate.realPostgres.integration.test.ts
@@ -7,6 +7,7 @@ import { initDb } from '../src/db.js'
import { deriveOAuthEncryptionKey } from '../src/oauth/encryption.js'
import { bootstrapSharedOAuthGrant, upsertOAuthGrant } from '../src/oauth/store.js'
import { resolveInvocableMcpServersForContexts } from '../src/services/access/mcpInvocable.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
// T1 — the rpc-proxy grant-presence gate exercised end-to-end against grant
@@ -44,7 +45,7 @@ describeRealPostgres('mcpInvocable grant-presence gate (real Postgres)', () => {
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts b/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts
index be5db320f..666e4b364 100644
--- a/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts
+++ b/control-api/test/services.adminAuthDelete.realPostgres.integration.test.ts
@@ -3,6 +3,7 @@ import { randomBytes, randomUUID } from 'node:crypto'
import { Pool } from 'pg'
import { initDb, pool } from '../src/db.js'
import { deleteControlAdmin } from '../src/services/adminAuthService.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import './realPostgres.requirement.ts'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
@@ -74,7 +75,7 @@ describeRealPostgres('deleteControlAdmin on real PostgreSQL', () => {
afterAll(async () => {
querySpy?.mockRestore()
connectSpy?.mockRestore()
- await testPool?.end()
+ await endPoolAndWaitForClients(testPool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts b/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts
index c332562ca..17210d380 100644
--- a/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts
+++ b/control-api/test/services.controlAdminReplaceInviter.realPostgres.integration.test.ts
@@ -23,6 +23,7 @@ import {
createSilentInvitationForTeams,
} from '../src/services/directory/membership.js'
import { retireDesktopUser } from '../src/services/directory/users.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import './realPostgres.requirement.ts'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
@@ -181,7 +182,7 @@ describeRealPostgres('control admin replace-inviter invitations on real PostgreS
afterAll(async () => {
querySpy?.mockRestore()
connectSpy?.mockRestore()
- await testPool?.end()
+ await endPoolAndWaitForClients(testPool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts b/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts
index 1f451658a..9d6d3b350 100644
--- a/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts
+++ b/control-api/test/services.directory.userRetirement.realPostgres.integration.test.ts
@@ -4,6 +4,7 @@ import { Pool } from 'pg'
import { initDb, pool } from '../src/db.js'
import { retireDesktopUser } from '../src/services/directory/users.js'
import { gfsDesktopOperatorLinkService } from '../src/services/gfsDesktopOperatorLinkService.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -56,7 +57,7 @@ describeRealPostgres('retireDesktopUser on real PostgreSQL', () => {
afterAll(async () => {
corePoolConnectSpy?.mockRestore()
- await testPool?.end()
+ await endPoolAndWaitForClients(testPool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/services.llmCatalogSync.realPostgres.integration.test.ts b/control-api/test/services.llmCatalogSync.realPostgres.integration.test.ts
index bb3998d82..b9b8d5478 100644
--- a/control-api/test/services.llmCatalogSync.realPostgres.integration.test.ts
+++ b/control-api/test/services.llmCatalogSync.realPostgres.integration.test.ts
@@ -5,6 +5,7 @@ import type { ImageInputCapability } from '@clerum/llm-providers'
import { initDb } from '../src/db.js'
import { type CatalogSyncResult, syncDiscoveredModels } from '../src/services/llmCatalogSync.js'
import { loadStub, trimSnapshot, withModalities } from './helpers/modelsDevFixtures.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import './realPostgres.requirement.ts'
// R1-M2: run `syncDiscoveredModels` against a REAL PostgreSQL. The in-memory
@@ -211,7 +212,7 @@ describeRealPostgres('syncDiscoveredModels on real PostgreSQL (#654 image_input)
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid)
diff --git a/control-api/test/services.mcpSecretRollbackPermit.realPostgres.integration.test.ts b/control-api/test/services.mcpSecretRollbackPermit.realPostgres.integration.test.ts
index 7ede25132..c6645c1aa 100644
--- a/control-api/test/services.mcpSecretRollbackPermit.realPostgres.integration.test.ts
+++ b/control-api/test/services.mcpSecretRollbackPermit.realPostgres.integration.test.ts
@@ -8,6 +8,7 @@ import {
issueMcpSecretRollbackPermit,
releaseMcpSecretRollbackPermitClaim,
} from '../src/services/mcpSecretRollbackPermitService.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -43,14 +44,11 @@ describeRealPostgres('MCP Secret rollback permits on real PostgreSQL', () => {
}, 60_000)
afterAll(async () => {
- if (pool) {
- // A concurrent consume test opens several pg clients. pg@8 may emit a
- // late idle-client error while their sockets finish closing, so retain a
- // pool-level listener through teardown and wait for the backend rows to
- // disappear before using the administrative fallback below.
- pool.on('error', () => {})
- await pool.end()
- }
+ // A concurrent consume test opens several pg clients. Wait for every one
+ // of them to close before touching their backends (#946), then wait for
+ // the backend rows to disappear before using the administrative fallback
+ // below.
+ await endPoolAndWaitForClients(pool)
if (!adminPool) return
let activeConnections = 0
for (let attempt = 0; attempt < 50; attempt += 1) {
diff --git a/control-api/test/services.oauthProactiveRefreshCron.installIdentity.realPostgres.integration.test.ts b/control-api/test/services.oauthProactiveRefreshCron.installIdentity.realPostgres.integration.test.ts
index 7fd0c2705..6366278a1 100644
--- a/control-api/test/services.oauthProactiveRefreshCron.installIdentity.realPostgres.integration.test.ts
+++ b/control-api/test/services.oauthProactiveRefreshCron.installIdentity.realPostgres.integration.test.ts
@@ -36,6 +36,7 @@ import {
type PilotFixture,
makeDiscoveryTransport,
} from './fixtures/remoteOAuthDiscovery.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
import { MockGateway } from './mockGateway.js'
const VALIDATED_IP = '93.184.216.34'
@@ -121,7 +122,7 @@ describeRealPostgres('proactive refresh sweep — installation identity (real Po
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/services.pluginWorkloadSdkFinalization.realPostgres.integration.test.ts b/control-api/test/services.pluginWorkloadSdkFinalization.realPostgres.integration.test.ts
index 82d293d6b..ebf26bba9 100644
--- a/control-api/test/services.pluginWorkloadSdkFinalization.realPostgres.integration.test.ts
+++ b/control-api/test/services.pluginWorkloadSdkFinalization.realPostgres.integration.test.ts
@@ -12,6 +12,7 @@ import {
type PromptBridgeFinalizationInput,
finalizePromptBridgeInTransaction,
} from '../src/services/pluginWorkloadSdkFinalization.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -70,7 +71,7 @@ describeRealPostgres('Plugin Workload SDK finalization on real PostgreSQL', () =
})
afterAll(async () => {
- await dbPool?.end()
+ await endPoolAndWaitForClients(dbPool)
if (adminPool) {
await adminPool.query(
`SELECT pg_terminate_backend(pid)
diff --git a/control-api/test/services.rateLimiter.realPostgres.integration.test.ts b/control-api/test/services.rateLimiter.realPostgres.integration.test.ts
index 4d086c91f..775a2e770 100644
--- a/control-api/test/services.rateLimiter.realPostgres.integration.test.ts
+++ b/control-api/test/services.rateLimiter.realPostgres.integration.test.ts
@@ -6,6 +6,7 @@ import {
acquireRateLimitConcurrencyLease,
checkAndIncrementWithQuery,
} from '../src/services/rateLimiterService.js'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const describeRealPostgres = adminUrl ? describe : describe.skip
@@ -40,7 +41,7 @@ describeRealPostgres('rate limiter atomicity on real PostgreSQL', () => {
afterAll(async () => {
await pool?.query('DELETE FROM rate_limit_buckets WHERE bucket_key = $1', [bucketKey])
- await pool?.end()
+ await endPoolAndWaitForClients(pool)
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity
diff --git a/control-api/test/services.rateLimiterPoolSaturation.realPostgres.integration.test.ts b/control-api/test/services.rateLimiterPoolSaturation.realPostgres.integration.test.ts
index b9e2c325c..4ce70fc79 100644
--- a/control-api/test/services.rateLimiterPoolSaturation.realPostgres.integration.test.ts
+++ b/control-api/test/services.rateLimiterPoolSaturation.realPostgres.integration.test.ts
@@ -3,6 +3,7 @@ import { createHash, randomBytes } from 'node:crypto'
import { performance } from 'node:perf_hooks'
import { Pool, type PoolClient } from 'pg'
import request from 'supertest'
+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
// The limiter's upserts run on their own pool (db.ts `rateLimitPool`, max 6,
// 5 s acquire), separate from the core pool (max 10, 2 s acquire).
@@ -138,8 +139,8 @@ describeRealPostgres('rate limiter under pool saturation', () => {
if (value === undefined) delete process.env[key]
else process.env[key] = value
}
- await corePool?.end().catch(() => {})
- await limiterPool?.end().catch(() => {})
+ await endPoolAndWaitForClients(corePool).catch(() => {})
+ await endPoolAndWaitForClients(limiterPool).catch(() => {})
if (!adminPool) return
await adminPool.query(
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity