Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.
TypeScript
fix(db): reset the migration client's session before it re-enters the pool (#12)
Xialie Zhuang05b0ffca40de
diff --git a/server/src/__tests__/migrate-client-release.test.ts b/server/src/__tests__/migrate-client-release.test.tsnew file mode 100644index 0000000..3d9af99--- /dev/null+++ b/server/src/__tests__/migrate-client-release.test.ts@@ -0,0 +1,58 @@+/**+ * ensureSchema runs its DDL on a client borrowed from the SHARED pool, after+ * disabling that session's statement_timeout / idle_in_transaction_session_timeout+ * and setting a 5s lock_timeout. Those are session-scoped, `pg-pool`'s release()+ * does not reset session state, and node-postgres only sends the pool's timeouts+ * in the connection's startup packet — so whatever the migration leaves behind+ * rides that connection for the rest of the process's life.+ *+ * Run: node --import tsx --test server/src/__tests__/migrate-client-release.test.ts+ */+import { test } from 'node:test'+import assert from 'node:assert/strict'+import { releaseMigrationClient } from '../db/migrate.js'++function fakeClient(opts: { failReset?: boolean } = {}) {+ const calls: string[] = []+ return {+ calls,+ query: async (sql: string) => {+ calls.push(sql)+ if (opts.failReset) throw new Error('connection terminated unexpectedly')+ return {}+ },+ release: () => { calls.push('<release>') },+ }+}++test('the migration client is reset before it re-enters the pool', () => {+ const c = fakeClient()+ return releaseMigrationClient(c).then(() => {+ assert.deepEqual(c.calls, ['RESET ALL', '<release>'])+ })+})++test('the reset happens BEFORE release, never after', async () => {+ // Order is the whole point: a client released first is already back in the+ // pool and may have been handed to another request by the time RESET lands.+ const c = fakeClient()+ await releaseMigrationClient(c)+ const reset = c.calls.indexOf('RESET ALL')+ const released = c.calls.indexOf('<release>')+ assert.notEqual(reset, -1, 'the session was never reset')+ assert.notEqual(released, -1, 'the client was never released')+ assert.ok(reset < released, 'reset must precede release')+})++test('a failed reset still returns the slot to the pool', async () => {+ // Losing a pool slot forever is worse than a stale session; the pool discards+ // a broken connection on its own.+ const c = fakeClient({ failReset: true })+ await releaseMigrationClient(c)+ assert.ok(c.calls.includes('<release>'))+})++test('releaseMigrationClient does not reject when the reset fails', async () => {+ const c = fakeClient({ failReset: true })+ await assert.doesNotReject(() => releaseMigrationClient(c))+})diff --git a/server/src/db/migrate.ts b/server/src/db/migrate.tsindex 91c9e21..61836ba 100644--- a/server/src/db/migrate.ts+++ b/server/src/db/migrate.ts@@ -2101,10 +2101,38 @@ export async function ensureSchema(): Promise<void> {// lock). Best-effort + non-fatal — see the helper.await buildConcurrentIndexes(client)} finally {- client.release()+ await releaseMigrationClient(client)}}+/** The subset of a pooled client this helper needs — structural so a test can+ * hand it a fake without a live Postgres. */+type MigrationClient = { query(sql: string): Promise<unknown>; release(): void }++/**+ * Hand the migration client back to the shared pool with its SESSION state wiped.+ *+ * ensureSchema deliberately turns OFF this session's `statement_timeout` and+ * `idle_in_transaction_session_timeout` and sets `lock_timeout = '5s'`. All three+ * are SESSION-scoped, and this client is an ordinary member of the 20-slot pool:+ * `pg-pool`'s `release()` does not reset session state, and node-postgres sends+ * the pool's timeouts only in the connection's STARTUP PACKET, so they are never+ * re-applied on checkout. Without this reset, one pooled connection spends the+ * rest of the process's life with the pool's runaway-query guards disabled — the+ * very protection whose absence once let a single un-indexed query hold all 20+ * slots and 503 the API (see db/pool.ts) — and with a 5s `lock_timeout` that+ * aborts ordinary writes with `55P03` instead of waiting. Same "re-enter the pool+ * with no leftover state" reasoning as the advisory unlock above.+ *+ * A failed RESET must never strand the slot, so we release either way; a+ * genuinely broken connection is discarded by the pool on its own.+ */+export async function releaseMigrationClient(client: MigrationClient): Promise<void> {+ try { await client.query('RESET ALL') }+ catch { /* connection already unusable — releasing still returns the slot */ }+ client.release()+}+/*** Cheap probe: are the schema's most-recently-added objects already present?*