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 each client the pool holds at end(), by identity (R4-M1 R4-L12)

Alfredo Lopez6cc23f526420
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 255a7e696..7bc4125fe 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.686",
+ "version": "0.1.687",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.686",
+ "version": "0.1.687",
"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 c80efb5a9..d2e83ca3a 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.686",
+ "version": "0.1.687",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/helpers.realPostgresTeardown.test.ts b/control-api/test/helpers.realPostgresTeardown.test.ts
index b391044df..8dfd7fb5e 100644
--- a/control-api/test/helpers.realPostgresTeardown.test.ts
+++ b/control-api/test/helpers.realPostgresTeardown.test.ts
@@ -6,14 +6,25 @@ import { Pool } from 'pg'
import type { PoolClient } from 'pg'
import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
-// pg-pool resolves end() while each client.end() is still in flight and emits
-// 'remove' from that client's end callback. The fake keeps that ordering: end()
-// resolves at once and the test decides when each client finishes closing.
-class FakePool extends EventEmitter {
- readonly end = vi.fn(async () => {})
- constructor(readonly totalCount: number) {
+// pg-pool resolves end() once its `_clients` list is empty, while each
+// client.end() it started is still in flight; a pg Client sets `_ended` and
+// emits 'end' when its connection has closed. The fakes keep that ordering:
+// end() resolves at once and the test decides when each client finishes
+// closing.
+class FakeClient extends EventEmitter {
+ constructor(public _ended = false) {
super()
}
+
+ finishClosing(): void {
+ this._ended = true
+ this.emit('end')
+ }
+}
+
+class FakePool {
+ readonly end = vi.fn(async () => {})
+ constructor(readonly _clients: unknown) {}
}
async function settledAfterMicrotasks(promise: Promise<void>): Promise<boolean> {
@@ -26,35 +37,51 @@ async function settledAfterMicrotasks(promise: Promise<void>): Promise<boolean>
}
describe('endPoolAndWaitForClients', () => {
- it('resolves only after every client open at end() has emitted remove', async () => {
- const pool = new FakePool(2)
+ it('resolves only after every client held at end() has ended', async () => {
+ const alreadyEnded = new FakeClient(true)
+ const a = new FakeClient()
+ const b = new FakeClient()
+ const pool = new FakePool([alreadyEnded, a, b])
const done = endPoolAndWaitForClients(pool as unknown as Pool)
expect(await settledAfterMicrotasks(done)).toBe(false)
expect(pool.end).toHaveBeenCalledTimes(1)
+ expect(a.listenerCount('end')).toBe(1)
+ expect(b.listenerCount('end')).toBe(1)
+ expect(alreadyEnded.listenerCount('end')).toBe(0)
- pool.emit('remove', {})
+ a.finishClosing()
expect(await settledAfterMicrotasks(done)).toBe(false)
- pool.emit('remove', {})
+ b.finishClosing()
await expect(done).resolves.toBeUndefined()
- expect(pool.listenerCount('remove')).toBe(0)
+ expect(a.listenerCount('end')).toBe(0)
+ expect(b.listenerCount('end')).toBe(0)
})
it('resolves after end() when the pool holds no client', async () => {
- const pool = new FakePool(0)
+ const pool = new FakePool([])
await expect(endPoolAndWaitForClients(pool as unknown as Pool)).resolves.toBeUndefined()
expect(pool.end).toHaveBeenCalledTimes(1)
- expect(pool.listenerCount('remove')).toBe(0)
})
it('rejects when end() rejects', async () => {
- const pool = new FakePool(1)
+ const client = new FakeClient()
+ const pool = new FakePool([client])
pool.end.mockRejectedValueOnce(new Error('Called end on pool more than once'))
await expect(endPoolAndWaitForClients(pool as unknown as Pool)).rejects.toThrow(
'Called end on pool more than once'
)
- expect(pool.listenerCount('remove')).toBe(0)
+ expect(pool.end).toHaveBeenCalledTimes(1)
+ expect(client.listenerCount('end')).toBe(0)
+ })
+
+ it('throws before end() when pg-pool no longer keeps _clients as an array', async () => {
+ const pool = new FakePool(new Set([new FakeClient()]))
+ await expect(endPoolAndWaitForClients(pool as unknown as Pool)).rejects.toThrow(
+ 'pg-pool internals changed: _clients is not an array'
+ )
+ expect(pool.end).not.toHaveBeenCalled()
})
it('does nothing for a pool that was never created', async () => {
diff --git a/control-api/test/helpers/realPostgresTeardown.ts b/control-api/test/helpers/realPostgresTeardown.ts
index 4e71fe6a2..776885187 100644
--- a/control-api/test/helpers/realPostgresTeardown.ts
+++ b/control-api/test/helpers/realPostgresTeardown.ts
@@ -1,40 +1,73 @@
import type { Pool } from 'pg'
/**
- * Ends a pg pool and resolves only after every client it held has closed its
- * connection.
+ * The part of a pg Client this helper reads. `_ended` and the `end` event are
+ * pg 8.x internals and public API respectively; see the comment below.
+ */
+interface ClosingClient {
+ readonly _ended: boolean
+ once(event: 'end', listener: () => void): unknown
+ off(event: 'end', listener: () => void): unknown
+}
+
+/**
+ * Ends a pg pool and resolves only after every client it held at end() has
+ * closed its connection.
*
* pg-pool's end() resolves as soon as its client list is empty, while each
* client.end() it started is still in flight. A real-Postgres teardown that
* then runs pg_terminate_backend can reach a client that is still closing; the
* 57P01 it receives is re-emitted on the pool and, with no pool `error`
- * listener, fails the run as an unhandled error (#946). pg-pool emits `remove`
- * from each client's end callback, so waiting for one `remove` per client open
- * at end() closes that window without listening for errors.
+ * listener, fails the run as an unhandled error (#946).
+ *
+ * The helper snapshots the clients the pool holds before end() and waits for
+ * each one by identity. Counting pool `remove` events against `totalCount` is
+ * wrong both ways (pg-pool 3.13.0, `pg-pool/index.js`):
+ * - a client still connecting is in `_clients` (:242), but a failed connect
+ * (:271-274) or a rejected onConnect (:294-295) drops it without `_remove`,
+ * so no `remove` is ever emitted and the wait never ends (R4-M1);
+ * - a client removed before end() (release(true), expiry, idle timeout) is no
+ * longer counted, yet `_remove` (:172-188) still emits its `remove` when its
+ * connection closes, which stands in for a client still closing (R4-L12).
+ *
+ * pg 8.20.0 `pg/lib/client.js` sets `_ended` (:184) and emits `end` (:202-204)
+ * from the connection's `end` handler (:179), which `pg/lib/connection.js`
+ * emits on the socket's `close` (:60-62). That handler runs for a connection
+ * that closed and for a connect that failed (:191-196), so every client in the
+ * snapshot either has `_ended` set already or emits `end` exactly once.
+ * `_clients` is private to pg-pool: if it is not an array the helper throws,
+ * before ending the pool, rather than wait on something it cannot read.
+ *
+ * Scope: a client the pool had already removed before end() is not in the
+ * snapshot and is not waited for; a teardown that needs it closed must end it
+ * before calling this helper.
*
* A pool that was never created (setup failed first) is left alone, as the
* `pool?.end()` it replaces did.
*/
export async function endPoolAndWaitForClients(pool: Pool | null | undefined): Promise<void> {
if (!pool) return
- const open = pool.totalCount
- let removed = 0
- let onRemove: (() => void) | undefined
- const closed = new Promise<void>(resolve => {
- if (open === 0) {
- resolve()
- return
- }
- onRemove = () => {
- removed += 1
- if (removed === open) resolve()
- }
- pool.on('remove', onRemove)
- })
+ const clients = (pool as unknown as { _clients: unknown })._clients
+ if (!Array.isArray(clients)) {
+ throw new Error('pg-pool internals changed: _clients is not an array')
+ }
+ const listeners: Array<[ClosingClient, () => void]> = []
+ const closed = (clients as ClosingClient[]).map(
+ client =>
+ new Promise<void>(resolve => {
+ if (client._ended) {
+ resolve()
+ return
+ }
+ const onEnd = (): void => resolve()
+ listeners.push([client, onEnd])
+ client.once('end', onEnd)
+ })
+ )
try {
await pool.end()
- await closed
+ await Promise.all(closed)
} finally {
- if (onRemove) pool.off('remove', onRemove)
+ for (const [client, onEnd] of listeners) client.off('end', onEnd)
}
}