Files
orca/cloud/apps/relay/src/postgres-checked-out-client-error.test.ts
T
Jinwoo Hong 8f117e19b1 fix(relay): destroy a pooled client whose session the server ended (#29573)
* fix(relay): destroy a pooled client whose session the server ended, and mark work that never ran

pg keeps calling a client queryable until its socket's end arrives, so a client released after
57P01 (pg_terminate_backend, shutdown) or a dropped socket went back to the pool and failed the
next caller's statement with 'Connection terminated unexpectedly'. Such clients are now released
with the error, so pg-pool discards them.

Also marks failures where no statement of the operation reached the server (acquire, a failed
BEGIN, a client dead at checkout) so a caller can tell work safe to re-run.

* fix(relay): skip ROLLBACK on an ended session, drop unused nothing-ran marking

Adds 57P04/57P05 to the session-ended codes and covers 08xxx, a dropped
socket, an ordinary 23505, and a real pg_terminate_backend.

Claude-Session: f2fe8cb5
2026-10-16 11:46:22 -07:00

73 lines
2.7 KiB
TypeScript

import { EventEmitter } from 'node:events'
import { describe, expect, it, vi } from 'vitest'
import { PostgresDatabase } from './database.js'
// Stands in for a pg client between acquire and release. pg-pool assigns
// `release` per checkout, which is the property the guard wraps.
class FakePoolClient extends EventEmitter {
readonly released: Array<Error | boolean | undefined> = []
readonly statements: string[] = []
constructor(private readonly respond: (sql: string) => { rows: unknown[]; rowCount: number }) {
super()
}
query = vi.fn((sql: string) => {
this.statements.push(sql)
return Promise.resolve(this.respond(sql))
})
release = (error?: Error | boolean): void => {
this.released.push(error)
}
}
function poolOf(client: FakePoolClient) {
return { totalCount: 1, idleCount: 0, waitingCount: 0, connect: async () => client }
}
describe('checked-out PostgreSQL client failure handling', () => {
it('absorbs the error, rejects the transaction, and releases the client as failed', async () => {
const terminated = Object.assign(new Error('Connection terminated unexpectedly'), {
code: '57P01'
})
let listenersWhileCheckedOut = 0
const client: FakePoolClient = new FakePoolClient((sql) => {
if (sql !== 'SELECT 1') return { rows: [], rowCount: 0 }
listenersWhileCheckedOut = client.listenerCount('error')
// Cloud SQL terminating the session: the client emits `error` and the
// in-flight statement rejects with the same failure.
expect(() => client.emit('error', terminated)).not.toThrow()
throw terminated
})
const warning = vi.spyOn(console, 'warn').mockImplementation(() => {})
const database = new PostgresDatabase(poolOf(client) as never)
await expect(
database.transaction(async (transaction) => await transaction.query('SELECT 1'))
).rejects.toBe(terminated)
expect(listenersWhileCheckedOut).toBe(1)
expect(client.listenerCount('error')).toBe(0)
expect(client.released).toEqual([terminated])
expect(client.statements).toEqual(['BEGIN', 'SELECT 1'])
expect(warning).toHaveBeenCalledWith(
'[orca-relay] checked-out PostgreSQL client failed: 57P01 Connection terminated unexpectedly'
)
warning.mockRestore()
})
it('releases a healthy client back to the pool with no error', async () => {
const client = new FakePoolClient(() => ({ rows: [{ one: 1 }], rowCount: 1 }))
const database = new PostgresDatabase(poolOf(client) as never)
await expect(
database.transaction(async (transaction) => await transaction.query('SELECT 1'))
).resolves.toEqual([{ one: 1 }])
expect(client.released).toEqual([undefined])
expect(client.listenerCount('error')).toBe(0)
})
})