Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Keep a search's JIT-off and statement-timeout reads inside its connection budget

garrytan72b90e499dd5
diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.ts
index 6060cfe2c8..0b485334e4 100644
--- a/src/core/postgres-engine.ts
+++ b/src/core/postgres-engine.ts
@@ -2998,11 +2998,11 @@ export class PostgresEngine implements BrainEngine {
throwIfRequestAborted();
this.checkoutGauge.acquire('raw');
try {
- if (opts?.jitOff && await needsJitOff(this.sql, this._pageTransaction)) return await runJitOffStatement<T>(this.sql, this._pageTransaction, sql, params, opts, (c, s, p, o) => this.runUnsafe(c, s, p, o), () => this.checkoutGauge.checkedOut());
- // #6278: a transaction-local statement_timeout, which holds through a transaction-mode pooler (postgres-engine/bounded-statement.ts).
- if (opts?.timeoutMs !== undefined && !this._pageTransaction) {
- return await runBoundedStatement<T>(this.sql, sql, params, { ...opts, timeoutMs: opts.timeoutMs }, () => this.checkoutGauge.checkedOut());
- }
+ if (opts?.jitOff || (opts?.timeoutMs !== undefined && !this._pageTransaction)) return await holdSearchConnection(async () => {
+ if (opts.jitOff && await needsJitOff(this.sql, this._pageTransaction)) return await runJitOffStatement<T>(this.sql, this._pageTransaction, sql, params, opts, (c, s, p, o) => this.runUnsafe(c, s, p, o), () => this.checkoutGauge.checkedOut());
+ // #6278: a transaction-local statement_timeout, which holds through a transaction-mode pooler (postgres-engine/bounded-statement.ts).
+ return opts.timeoutMs !== undefined && !this._pageTransaction ? await runBoundedStatement<T>(this.sql, sql, params, { ...opts, timeoutMs: opts.timeoutMs }, () => this.checkoutGauge.checkedOut()) : await retryReadOnlyAutocommit(this.sql, c => this.runUnsafe<T>(c, sql, params, opts));
+ });
return await retryReadOnlyAutocommit(this.sql, c => this.runUnsafe<T>(c, sql, params, opts));
} finally {
this.checkoutGauge.release('raw');
diff --git a/src/core/search/connection-budget.ts b/src/core/search/connection-budget.ts
index babd35e8bd..741fd75efa 100644
--- a/src/core/search/connection-budget.ts
+++ b/src/core/search/connection-budget.ts
@@ -4,12 +4,14 @@
* connections those reads hold at once, so a burst of searches shares the
* read pool instead of each one taking it whole.
*
- * The engine's two connection seams (PostgresEngine#transactionOn for a
- * top-level transaction, runUnsafe for a statement) wait for a slot inside a
- * budget scope and hold it until the statement or the transaction's COMMIT
- * settles. Work inside a held slot runs outside the scope, so statements on
- * the transaction's own connection never wait on a second slot. Outside a
- * scope, and on PGLite, nothing changes.
+ * The engine's connection seams (PostgresEngine#transactionOn for a
+ * top-level transaction; executeRaw, including its JIT-off and
+ * statement-timeout envelopes that reserve their own connection, and
+ * runUnsafe for a statement) wait for a slot inside a budget scope and hold
+ * it until the statement or the transaction's COMMIT settles. Work inside a
+ * held slot runs outside the scope, so statements on the transaction's own
+ * connection never wait on a second slot. Outside a scope, and on PGLite,
+ * nothing changes.
*/
import { AsyncLocalStorage } from 'node:async_hooks';
import { throwIfRequestAborted } from '../request-signal.ts';
diff --git a/test/e2e/search-connection-budget-postgres.test.ts b/test/e2e/search-connection-budget-postgres.test.ts
index a16c12affc..274d2db3a1 100644
--- a/test/e2e/search-connection-budget-postgres.test.ts
+++ b/test/e2e/search-connection-budget-postgres.test.ts
@@ -19,6 +19,7 @@ import { createServer, connect as netConnect, type Server, type Socket } from 'n
import { PostgresEngine } from '../../src/core/postgres-engine.ts';
import { dispatchToolCall } from '../../src/mcp/dispatch.ts';
import { SEARCH_CONNECTION_LIMIT, withSearchConnectionBudget } from '../../src/core/search/connection-budget.ts';
+import { drainBackgroundWorkBeforeDisconnect } from '../../src/core/background-work.ts';
import { isolatedPersistencePostgres } from '../helpers/persistence-postgres.ts';
import { seedPage } from '../helpers/keyword-paging-fixture.ts';
@@ -99,9 +100,20 @@ run('per-search connection budget on Postgres', () => {
expect(proxy.peak).toBe(SEARCH_CONNECTION_LIMIT);
});
+ test('reads that reserve their own connection (JIT off, a statement timeout) wait for a slot too', async () => {
+ proxy.reset();
+ await withSearchConnectionBudget(() => Promise.all([
+ ...reads(),
+ engine.executeRaw('SELECT pg_sleep(0.05)', [], { jitOff: true }),
+ engine.executeRaw('SELECT pg_sleep(0.05)', [], { timeoutMs: 10_000 }),
+ ]));
+ expect(proxy.peak).toBe(SEARCH_CONNECTION_LIMIT);
+ });
+
for (const op of ['search', 'query'] as const) {
test(`a ${op} op holds at most 4 connections at once`, async () => {
await dispatchToolCall(engine, op, { query: 'quasar survey' }, { remote: false });
+ await drainBackgroundWorkBeforeDisconnect();
proxy.reset();
const result = await dispatchToolCall(engine, op, { query: 'quasar survey' }, { remote: false });
expect(result.isError, JSON.stringify(result).slice(0, 2000)).toBeFalsy();