mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-16 03:31:44 +01:00
perf(db): reuse dedicated connections instead of opening one per call (#21567)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The server uses `withDedicatedDbConnection` (packages/db) when an action must hold one database session outside the main pool: advisory transaction locks, lock keepalive, and `pg_settings` reads for workspace lifecycle, finalization, source locks, skill caches and status cards > - Each call built a new postgres.js client, opened a new physical connection (TCP, TLS and startup through the pooler), built a new drizzle client with the full schema, and then ended all of it > - On staging the dedicated application name shows about 21 connection setups per second, and the main thread is pinned (event-loop utilization 0.965) > - This pull request keeps a small set of idle single-connection clients per owner and reuses them > - The benefit is that a dedicated action costs about 0.2 ms of main-thread time instead of 4 to 8 ms, with the same exclusive-session guarantee ## Linked Issues or Issue Description **What existing behavior does this improve?** `withDedicatedDbConnection` opens and closes one new database client and one new physical connection for every call. **Subsystem affected** Database client (`packages/db`) and the server services that take dedicated advisory-lock sessions. **Current behavior** Each call builds a new postgres.js client, opens a new connection (TCP, TLS and startup through the pooler), and builds a new drizzle client with the full schema. On staging (Neon statement statistics, 326 s window) `SET application_name='paperclip-workspace-finalization-lock'` ran 21.4 times per second. Only this factory uses that application name. In a 30 s staging CPU profile, drizzle `relations.ts` was 2.2% inclusive, `n:net` 5.8% and postgres.js construction 0.3%. Part of that is this per-call setup. **Proposed behavior** A caller still gets exclusive use of one session for the whole action. The session and its drizzle client are reused by later actions, so the per-call cost is the action's own queries, not a full connection setup. **Reason and benefit** The server main thread is saturated on staging (event-loop utilization 0.965). Reuse removes about 5 ms of main-thread work per call. **Breaking changes** None. The exported function keeps its signature. `closeDedicatedDbConnections` and `DEDICATED_CONNECTION_IDLE_MAX` are new exports. ## What Changed - `packages/db/src/client.ts`: each `createDb` owner now has one set of dedicated single-connection clients, shared with every private client derived from that owner (so nested dedicated actions use the same set). - An action takes an idle client (last in, first out) or opens a new one. It never waits. Callers hold advisory locks and nest dedicated actions, so a waiting queue could deadlock. - On success the client returns to the idle set if fewer than `DEDICATED_CONNECTION_IDLE_MAX` (4) are idle. Otherwise it is closed. - When the action throws, the client is closed and never reused. This covers a failed or cancelled transaction, a broken socket, and an interrupted lock wait. - When an action ends the client itself (the source-lock cancel path calls `$client.end`), the client is never reused. - Idle clients keep the owner's idle timeout and max lifetime, so postgres.js still closes idle physical connections after `DATABASE_IDLE_TIMEOUT_SECONDS`. - New `closeDedicatedDbConnections(db, timeoutSeconds)` closes the idle set and stops retention. Ending the owner `$client` does the same. A later action still works with a connection that closes on release. - `server/src/index.ts`: shutdown closes the dedicated set next to the other private pools. - `doc/DATABASE.md`: documents the idle cap, the "not an admission limit" rule, and the transaction-scoped session state contract. - Session state audit: every current caller uses `pg_advisory_xact_lock` / `pg_try_advisory_xact_lock` and `set_config(..., true)`. No caller uses session advisory locks, `SET` without `LOCAL`, `LISTEN`, or temporary tables. A reset round trip per call is therefore not added. ## Verification - New `packages/db/src/dedicated-connection-pool.test.ts` (embedded PostgreSQL, 10 tests). - Red on `origin/master`: 200 sequential calls used 200 distinct sessions (`expected 200 to be 1`). All 10 tests failed. - Green with this change, 3 runs in a row: 200 sequential calls use 1 session; 7 concurrent callers each hold a different session for the whole transaction, and afterwards at most 4 stay open and later calls reuse them; a failed transaction's connection is closed; a terminated socket during the action is not reused; an idle connection terminated by the server reconnects; a client the action ended is not reused; transaction-local settings do not leak; shutdown and owner `$client.end` close every retained connection; nested actions do not block. - Focused suites pass: packages/db `work-signals`, `prepared-queries-postgres`, `response-capture-postgres`, `checkpoint-pool`, `client`, `client-options`, `client-teardown-registry`, `postgres-connection-recovery`, `client-disconnect-replay` (132 tests). Server suites that use dedicated connections: `skill-package-store`, `status-card-evaluation-work` (updated: it now proves advisory locks are released, since sessions are reused), `agent-instruction-working-copies`, `workspace-recovery-retry-copyback`, `native-execution-lease-batch`, `company-skills-empty-runtime`, `decisions-service`, `legacy-controller-lease`, `server-startup-feedback-export` (467 tests). - `pnpm verify --explain` selects 2779 files for a `packages/db/src/client.ts` change. Per AGENTS.md I ran the focused checks above and let CI give the broad coverage. - Benchmark: 200 calls of `withDedicatedDbConnection(db, d => d.transaction(tx => tx.execute(select pg_try_advisory_xact_lock(42))))` against local embedded PostgreSQL 18, 10 warm-up calls, 5 alternating before/after process pairs per mode. Main-thread time is `eventLoopUtilization().active`. | Mode | Variant | Wall ms/call (median, range) | Main-thread ms/call (median, range) | | --- | --- | --- | --- | | TLS | before | 35.55 (32.93–37.52) | 5.18 (4.68–7.95) | | TLS | after | 0.19 (0.17–0.21) | 0.18 (0.17–0.20) | | Plain | before | 18.39 (17.91–20.53) | 3.65 (3.58–4.30) | | Plain | after | 0.17 (0.13–0.18) | 0.13 (0.12–0.14) | At the staging rate of about 21 calls per second, this removes roughly 100 ms of main-thread work per second (TLS figures; staging CPUs may be slower). ## Risks - A retained connection that the server or network drops while idle: postgres.js reconnects on the next query when it has seen the close (tested). If the drop and the reuse race, the first query of that action fails like a main-pool query would. Before this change, a dedicated action always started on a fresh connection. - Through a transaction-mode pooler, `SET application_name=...` is a server-side parameter switch, so its count may not drop as much as the client-side connection count. Measure the deployed effect with pooler client connections and main-thread CPU, not only that statement. - Concurrency above 4 opens extra connections exactly as before; they close on release. Total retained connections per owner grow by at most 4 idle clients, which still close after the idle timeout. - An action that sets session-level state would now leak it to the next action. The contract is documented on `withDedicatedDbConnection` and in `doc/DATABASE.md`; no current caller does this. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used in Claude Code with tool use (shell, file edits, test runs). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c36b96ae00
commit
3ae7aba643
8 files changed
+285
-15
No files matched your search
@@ -916,6 +916,20 @@ DATABASE_MAX_LIFETIME_SECONDS=1800 # recycle a pooled connection after this lo
|
||||
DATABASE_APPLICATION_NAME=paperclip # application_name in pg_stat_activity; default: paperclip
|
||||
```
|
||||
|
||||
`withDedicatedDbConnection` gives an action exclusive use of one session outside
|
||||
the main pool (application name `paperclip-workspace-finalization-lock`). Each
|
||||
`createDb` owner keeps at most `DEDICATED_CONNECTION_IDLE_MAX` (4) of these
|
||||
single-connection clients idle for reuse; they follow the same idle timeout and
|
||||
max lifetime as the main pool. This is not an admission limit: callers hold
|
||||
advisory locks and nest dedicated actions, so an action that finds no idle
|
||||
client opens one and closes it on release when four are already retained. A
|
||||
connection whose action throws is closed, never reused. Actions must keep
|
||||
session state transaction-scoped (`pg_advisory_xact_lock`,
|
||||
`set_config(..., true)`). Like the main pool, a retained session sees an
|
||||
`ALTER DATABASE` or `ALTER ROLE ... SET` change only after it reconnects; a test
|
||||
that changes such a setting calls `closeDedicatedDbConnections` first. Shutdown
|
||||
and ending the owner client close the idle set.
|
||||
|
||||
An explicit `DATABASE_PREPARED_STATEMENTS=true` enables named prepared statements
|
||||
for parameterized ORM queries, transactions, reserved connections, and dedicated
|
||||
clients. Postgres.js manages the statement cache. This does not cache query
|
||||
|
||||
+66
-10
@@ -271,9 +271,37 @@ export function postgresJsOptions(options: DatabaseClientOptions): Record<string
|
||||
// A long advisory-lock transaction must not borrow the normal pool that its
|
||||
// work needs for progress writes. Keep connection configuration private to the
|
||||
// originating Db lifetime; callers receive neither URLs nor credentials.
|
||||
const dedicatedDbFactories = new WeakMap<object, () => Db>();
|
||||
const ownedDatabaseListenerFactories = new WeakMap<object, () => Db>();
|
||||
|
||||
export const DEDICATED_CONNECTION_IDLE_MAX = 4;
|
||||
|
||||
type DedicatedConnectionSet = {
|
||||
create(): Db;
|
||||
idle: Db[];
|
||||
ended: WeakSet<Db>;
|
||||
closed: boolean;
|
||||
};
|
||||
const dedicatedConnectionSets = new WeakMap<object, DedicatedConnectionSet>();
|
||||
|
||||
function trackClientEnd(client: Db, onEnd: (timeoutSeconds: number | undefined) => Promise<void> | void): void {
|
||||
const sql = client.$client;
|
||||
const end = sql.end;
|
||||
sql.end = async (options?: { timeout?: number }) => {
|
||||
await Promise.all([onEnd(options?.timeout), end(options)]);
|
||||
};
|
||||
}
|
||||
|
||||
async function endDedicatedConnections(clients: Db[], timeoutSeconds: number): Promise<void> {
|
||||
await Promise.all(clients.map(client => client.$client.end({ timeout: timeoutSeconds }).catch(() => undefined)));
|
||||
}
|
||||
|
||||
export async function closeDedicatedDbConnections(db: Db, timeoutSeconds: number): Promise<void> {
|
||||
const set = dedicatedConnectionSets.get(db);
|
||||
if (!set) return;
|
||||
set.closed = true;
|
||||
await endDedicatedConnections(set.idle.splice(0), timeoutSeconds);
|
||||
}
|
||||
|
||||
export interface OwnedDatabaseListener {
|
||||
listen: Db["$client"]["listen"];
|
||||
close(): Promise<void>;
|
||||
@@ -290,11 +318,26 @@ export function createOwnedDatabaseListener(db: Db): OwnedDatabaseListener {
|
||||
}
|
||||
|
||||
export async function withDedicatedDbConnection<T>(db: Db, action: (dedicated: Db) => Promise<T>): Promise<T> {
|
||||
const factory = dedicatedDbFactories.get(db);
|
||||
if (!factory) throw new Error("dedicated_connection_requires_create_db");
|
||||
const dedicated = factory();
|
||||
try { return await action(dedicated); }
|
||||
finally { await dedicated.$client.end({ timeout: 1 }); }
|
||||
const set = dedicatedConnectionSets.get(db);
|
||||
if (!set) throw new Error("dedicated_connection_requires_create_db");
|
||||
let dedicated: Db | undefined;
|
||||
while (!dedicated && set.idle.length) {
|
||||
const candidate = set.idle.pop()!;
|
||||
if (!set.ended.has(candidate)) dedicated = candidate;
|
||||
}
|
||||
dedicated ??= set.create();
|
||||
let completed = false;
|
||||
try {
|
||||
const result = await action(dedicated);
|
||||
completed = true;
|
||||
return result;
|
||||
} finally {
|
||||
if (completed && !set.closed && !set.ended.has(dedicated) && set.idle.length < DEDICATED_CONNECTION_IDLE_MAX) {
|
||||
set.idle.push(dedicated);
|
||||
} else if (!set.ended.has(dedicated)) {
|
||||
await dedicated.$client.end({ timeout: 1 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// postgres.js pipelines a query onto a busy connection when none is idle, so
|
||||
@@ -420,7 +463,7 @@ export function createDb(url: string, options?: DatabaseClientOptions) {
|
||||
return createDbClient(url, resolved);
|
||||
}
|
||||
|
||||
function createDbClient(url: string, resolved: DatabaseClientOptions, owner?: object) {
|
||||
function createDbClient(url: string, resolved: DatabaseClientOptions, owner?: object, dedicatedSet?: DedicatedConnectionSet) {
|
||||
const sql = postgres(url, postgresJsOptions(resolved));
|
||||
if (resolved.prepare === true) installPreparedQueries(sql);
|
||||
const key = hostPortKeyOrNull(url);
|
||||
@@ -431,9 +474,22 @@ function createDbClient(url: string, resolved: DatabaseClientOptions, owner?: ob
|
||||
// that know the complete operation is idempotent.
|
||||
const db = installDatabaseWorkSignals(drizzleWithLazyTransactionQueries(sql, schema), owner);
|
||||
installDatabaseResponseCapture(db, schema);
|
||||
dedicatedDbFactories.set(db, () => createDbClient(url, {
|
||||
...resolved, maxConnections: 1, applicationName: "paperclip-workspace-finalization-lock",
|
||||
}, db));
|
||||
const ownerSet = owner ? dedicatedConnectionSets.get(owner) : undefined;
|
||||
if (dedicatedSet) {
|
||||
trackClientEnd(db, () => { dedicatedSet.ended.add(db); });
|
||||
dedicatedConnectionSets.set(db, dedicatedSet);
|
||||
} else if (ownerSet) {
|
||||
dedicatedConnectionSets.set(db, ownerSet);
|
||||
} else {
|
||||
const set: DedicatedConnectionSet = {
|
||||
create: () => createDbClient(url, {
|
||||
...resolved, maxConnections: 1, applicationName: "paperclip-workspace-finalization-lock",
|
||||
}, db, set),
|
||||
idle: [], ended: new WeakSet(), closed: false,
|
||||
};
|
||||
dedicatedConnectionSets.set(db, set);
|
||||
trackClientEnd(db, timeoutSeconds => closeDedicatedDbConnections(db, timeoutSeconds ?? 1));
|
||||
}
|
||||
ownedDatabaseListenerFactories.set(db, () => createDbClient(url, {
|
||||
...resolved, maxConnections: 1,
|
||||
}, db));
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
import {
|
||||
DEDICATED_CONNECTION_IDLE_MAX,
|
||||
closeDedicatedDbConnections,
|
||||
createDb,
|
||||
withDedicatedDbConnection,
|
||||
type Db,
|
||||
} from "./client.js";
|
||||
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./test-embedded-postgres.js";
|
||||
|
||||
const support = await getEmbeddedPostgresTestSupport();
|
||||
const APPLICATION_NAME = "paperclip-workspace-finalization-lock";
|
||||
|
||||
type Session = { pid: number; backendStart: string };
|
||||
|
||||
async function session(db: Pick<Db, "execute">): Promise<Session> {
|
||||
const [row] = await db.execute<{ pid: number; backend_start: string }>(sql`select pg_backend_pid() as pid,
|
||||
(select backend_start::text from pg_stat_activity where pid = pg_backend_pid()) as backend_start`);
|
||||
return { pid: Number(row.pid), backendStart: String(row.backend_start) };
|
||||
}
|
||||
|
||||
const key = (value: Session) => `${value.pid}@${value.backendStart}`;
|
||||
|
||||
async function eventually<T>(read: () => Promise<T>, accept: (value: T) => boolean): Promise<T> {
|
||||
const deadline = Date.now() + 5_000;
|
||||
for (;;) {
|
||||
const value = await read();
|
||||
if (accept(value) || Date.now() > deadline) return value;
|
||||
await new Promise(resolve => setTimeout(resolve, 25));
|
||||
}
|
||||
}
|
||||
|
||||
describe.skipIf(!support.supported)("withDedicatedDbConnection connection reuse", () => {
|
||||
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
||||
let observer: Db;
|
||||
const owners: Db[] = [];
|
||||
|
||||
beforeAll(async () => {
|
||||
temporary = await startEmbeddedPostgresTestDatabase("dedicated-pool-");
|
||||
observer = createDb(temporary.connectionString, { maxConnections: 1, applicationName: "dedicated-pool-observer" });
|
||||
}, 90_000);
|
||||
afterEach(async () => {
|
||||
for (const db of owners.splice(0)) {
|
||||
await closeDedicatedDbConnections(db, 1);
|
||||
await db.$client.end({ timeout: 1 });
|
||||
}
|
||||
});
|
||||
afterAll(async () => {
|
||||
await observer?.$client.end({ timeout: 1 });
|
||||
await temporary?.cleanup();
|
||||
});
|
||||
|
||||
function owner() {
|
||||
const db = createDb(temporary.connectionString, { maxConnections: 2 });
|
||||
owners.push(db);
|
||||
return db;
|
||||
}
|
||||
|
||||
async function openDedicatedSessions(): Promise<Session[]> {
|
||||
const rows = await observer.execute<{ pid: number; backend_start: string }>(sql`select pid, backend_start::text as backend_start
|
||||
from pg_stat_activity where application_name = ${APPLICATION_NAME} and datname = current_database()`);
|
||||
return rows.map(row => ({ pid: Number(row.pid), backendStart: String(row.backend_start) }));
|
||||
}
|
||||
|
||||
it("reuses a bounded set of physical connections across 200 sequential calls", async () => {
|
||||
const db = owner();
|
||||
const seen = new Set<string>();
|
||||
for (let i = 0; i < 200; i++) {
|
||||
seen.add(key(await withDedicatedDbConnection(db, dedicated => dedicated.transaction(tx => session(tx)))));
|
||||
}
|
||||
expect(seen.size).toBe(1);
|
||||
expect(seen.size).toBeLessThanOrEqual(DEDICATED_CONNECTION_IDLE_MAX);
|
||||
});
|
||||
|
||||
it("gives concurrent callers exclusive sessions and keeps at most the cap open afterwards", async () => {
|
||||
const db = owner();
|
||||
const concurrency = DEDICATED_CONNECTION_IDLE_MAX + 3;
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>(resolve => { release = resolve; });
|
||||
let arrived = 0;
|
||||
let allArrived!: () => void;
|
||||
const everyoneHolding = new Promise<void>(resolve => { allArrived = resolve; });
|
||||
const held = Array.from({ length: concurrency }, () => withDedicatedDbConnection(db, dedicated =>
|
||||
dedicated.transaction(async tx => {
|
||||
const mine = await session(tx);
|
||||
await tx.execute(sql`select pg_advisory_xact_lock(${mine.pid})`);
|
||||
if (++arrived === concurrency) allArrived();
|
||||
await gate;
|
||||
expect(await session(tx)).toEqual(mine);
|
||||
return mine;
|
||||
})));
|
||||
await everyoneHolding;
|
||||
release();
|
||||
const sessions = await Promise.all(held);
|
||||
expect(new Set(sessions.map(key)).size).toBe(concurrency);
|
||||
expect((await eventually(openDedicatedSessions, open => open.length <= DEDICATED_CONNECTION_IDLE_MAX)).length)
|
||||
.toBeLessThanOrEqual(DEDICATED_CONNECTION_IDLE_MAX);
|
||||
const retained = new Set(sessions.map(key));
|
||||
for (let i = 0; i < 20; i++) {
|
||||
expect(retained.has(key(await withDedicatedDbConnection(db, session)))).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("discards a connection whose action failed mid-transaction", async () => {
|
||||
const db = owner();
|
||||
let failed: Session | undefined;
|
||||
await expect(withDedicatedDbConnection(db, dedicated => dedicated.transaction(async tx => {
|
||||
failed = await session(tx);
|
||||
await tx.execute(sql`select 1 / 0`);
|
||||
}))).rejects.toThrow();
|
||||
expect(failed).toBeDefined();
|
||||
const next = await withDedicatedDbConnection(db, session);
|
||||
expect(key(next)).not.toBe(key(failed!));
|
||||
const open = await eventually(openDedicatedSessions, rows => !rows.map(key).includes(key(failed!)));
|
||||
expect(open.map(key)).not.toContain(key(failed!));
|
||||
});
|
||||
|
||||
it("discards a connection whose socket broke during the action", async () => {
|
||||
const db = owner();
|
||||
let broken: Session | undefined;
|
||||
await expect(withDedicatedDbConnection(db, async dedicated => {
|
||||
broken = await session(dedicated);
|
||||
await observer.execute(sql`select pg_terminate_backend(${broken.pid})`);
|
||||
await session(dedicated);
|
||||
})).rejects.toThrow();
|
||||
const next = await withDedicatedDbConnection(db, session);
|
||||
expect(next.pid).not.toBe(broken!.pid);
|
||||
});
|
||||
|
||||
it("recovers when an idle retained connection is terminated between calls", async () => {
|
||||
const db = owner();
|
||||
const first = await withDedicatedDbConnection(db, session);
|
||||
await observer.execute(sql`select pg_terminate_backend(${first.pid})`);
|
||||
await eventually(openDedicatedSessions, rows => !rows.some(row => row.pid === first.pid));
|
||||
await new Promise(resolve => setTimeout(resolve, 50));
|
||||
const second = await withDedicatedDbConnection(db, session);
|
||||
expect(second.pid).not.toBe(first.pid);
|
||||
});
|
||||
|
||||
it("never reuses a connection the action ended itself", async () => {
|
||||
const db = owner();
|
||||
const first = await withDedicatedDbConnection(db, async dedicated => {
|
||||
const value = await session(dedicated);
|
||||
await dedicated.$client.end({ timeout: 0 });
|
||||
return value;
|
||||
});
|
||||
const second = await withDedicatedDbConnection(db, session);
|
||||
expect(second.pid).not.toBe(first.pid);
|
||||
});
|
||||
|
||||
it("keeps transaction-local settings out of the next action", async () => {
|
||||
const db = owner();
|
||||
await withDedicatedDbConnection(db, dedicated => dedicated.transaction(tx =>
|
||||
tx.execute(sql`select set_config('statement_timeout', '1234', true), set_config('lock_timeout', '567', true)`)));
|
||||
const [row] = await withDedicatedDbConnection(db, dedicated => dedicated.execute<{ statement_timeout: string; lock_timeout: string }>(
|
||||
sql`select current_setting('statement_timeout') as statement_timeout, current_setting('lock_timeout') as lock_timeout`));
|
||||
expect(row).toEqual({ statement_timeout: "0", lock_timeout: "0" });
|
||||
});
|
||||
|
||||
it("closes every retained connection on shutdown and stays usable without retaining more", async () => {
|
||||
const db = owner();
|
||||
await Promise.all(Array.from({ length: DEDICATED_CONNECTION_IDLE_MAX }, () =>
|
||||
withDedicatedDbConnection(db, dedicated => dedicated.transaction(async tx => {
|
||||
await tx.execute(sql`select pg_sleep(0.05)`);
|
||||
}))));
|
||||
expect((await openDedicatedSessions()).length).toBeGreaterThan(0);
|
||||
await closeDedicatedDbConnections(db, 1);
|
||||
expect(await eventually(openDedicatedSessions, rows => rows.length === 0)).toEqual([]);
|
||||
await withDedicatedDbConnection(db, session);
|
||||
expect(await eventually(openDedicatedSessions, rows => rows.length === 0)).toEqual([]);
|
||||
});
|
||||
|
||||
it("closes retained connections when the owner client ends", async () => {
|
||||
const db = createDb(temporary.connectionString, { maxConnections: 1 });
|
||||
await withDedicatedDbConnection(db, session);
|
||||
expect((await openDedicatedSessions()).length).toBe(1);
|
||||
await db.$client.end({ timeout: 1 });
|
||||
expect(await eventually(openDedicatedSessions, rows => rows.length === 0)).toEqual([]);
|
||||
});
|
||||
|
||||
it("shares one retained set with nested dedicated actions without blocking them", async () => {
|
||||
const db = owner();
|
||||
const [outer, inner] = await withDedicatedDbConnection(db, async dedicated => {
|
||||
const mine = await session(dedicated);
|
||||
const nested = await withDedicatedDbConnection(dedicated, session);
|
||||
return [mine, nested];
|
||||
});
|
||||
expect(inner.pid).not.toBe(outer.pid);
|
||||
const reused = new Set<number>();
|
||||
for (let i = 0; i < 10; i++) reused.add((await withDedicatedDbConnection(db, session)).pid);
|
||||
expect([...reused].every(pid => pid === outer.pid || pid === inner.pid)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,8 @@
|
||||
export {
|
||||
createDb,
|
||||
withDedicatedDbConnection,
|
||||
closeDedicatedDbConnections,
|
||||
DEDICATED_CONNECTION_IDLE_MAX,
|
||||
createOwnedDatabaseListener,
|
||||
type OwnedDatabaseListener,
|
||||
livenessDb,
|
||||
|
||||
@@ -7,7 +7,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { context, SpanStatusCode, trace } from "@opentelemetry/api";
|
||||
import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
|
||||
import { InMemorySpanExporter, SimpleSpanProcessor, TracerProvider } from "@opentelemetry/sdk-trace";
|
||||
import { companies, companySkills, companySkillVersions, companySkillRepositorySnapshots, createDb,
|
||||
import { closeDedicatedDbConnections, companies, companySkills, companySkillVersions, companySkillRepositorySnapshots, createDb,
|
||||
EMBEDDED_POSTGRES_TEST_TIMEOUT_MS } from "@paperclipai/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { createRunPreparationTiming, withRunExecutionTiming } from "../services/execution-preparation-timing.js";
|
||||
@@ -133,6 +133,7 @@ describeDatabase("runtime skill observability", () => {
|
||||
const name = new URL(database.connectionString).pathname.slice(1);
|
||||
assert.match(name, /^[a-zA-Z0-9_-]+$/);
|
||||
await db.$client.unsafe(`ALTER DATABASE "${name}" SET lock_timeout TO '100ms'`);
|
||||
await closeDedicatedDbConnections(db, 1);
|
||||
const entered = deferred(), release = deferred();
|
||||
const holder = withSkillVersionCacheLock(db, f.companyId, async () => { entered.resolve(); await release.promise; });
|
||||
try {
|
||||
|
||||
@@ -546,8 +546,8 @@ const support = external ? { supported: true } : await getEmbeddedPostgresTestSu
|
||||
try {
|
||||
const created = await card("reactive", { ...policy(), debounceSeconds: 1 });
|
||||
const countLocks = async () => Number((await db.execute<{ count: string }>(sql`
|
||||
select count(*) from pg_stat_activity where datname = current_database()
|
||||
and application_name = 'paperclip-workspace-finalization-lock'`))[0]!.count);
|
||||
select count(*) from pg_locks l join pg_database d on d.oid = l.database
|
||||
where l.locktype = 'advisory' and d.datname = current_database()`))[0]!.count);
|
||||
const baseline = await countLocks();
|
||||
await issueService(db).create(companyId, { title: "Pool-one blocker", status: "blocked", priority: "medium" });
|
||||
const service = statusCardEvaluationService(db, { wakeup });
|
||||
@@ -573,7 +573,7 @@ const support = external ? { supported: true } : await getEmbeddedPostgresTestSu
|
||||
expect(await service.next()).toBeNull();
|
||||
expect(idleWorkSnapshot().active).toBe(0);
|
||||
await vi.waitFor(async () => expect(await countLocks()).toBe(baseline));
|
||||
// Failure exits also release the physical connection and advisory lock.
|
||||
// Failure exits also release the advisory lock.
|
||||
await db.update(statusCards).set({ queries: [{ scope: "invalid" } as never], nextEvalAt: new Date() }).where(eq(statusCards.id, created.id));
|
||||
await service.run(new AbortController().signal);
|
||||
expect((await statusCardService(db).getById(created.id))!.nextEvalReason).toBe("retry");
|
||||
|
||||
+2
-1
@@ -131,6 +131,7 @@ import {
|
||||
closeLivenessDb,
|
||||
closeCheckpointDb,
|
||||
closeFileStorageDb,
|
||||
closeDedicatedDbConnections,
|
||||
} from "@paperclipai/db";
|
||||
import detectPort from "detect-port";
|
||||
import { createApp } from "./app.js";
|
||||
@@ -752,7 +753,7 @@ async function startServerWithDatabaseTeardown(
|
||||
const closeDatabaseClients = async () => {
|
||||
setSentryRunLinkResolver(null);
|
||||
const clients = pluginMigrationDb === db ? [db] : [db, pluginMigrationDb];
|
||||
await Promise.all([...clients.map((client) => endDatabaseClient(client, 5)), closeLivenessDb(db, 5), closeCheckpointDb(db, 5), closeFileStorageDb(db, 5)]);
|
||||
await Promise.all([...clients.map((client) => endDatabaseClient(client, 5)), closeLivenessDb(db, 5), closeCheckpointDb(db, 5), closeFileStorageDb(db, 5), closeDedicatedDbConnections(db, 5)]);
|
||||
};
|
||||
startupDatabase.close = closeDatabaseClients;
|
||||
startupTimeline.mark("database");
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
agentWakeupRequests,
|
||||
agents,
|
||||
companies,
|
||||
closeDedicatedDbConnections,
|
||||
completionContracts,
|
||||
createDb,
|
||||
environmentLeases,
|
||||
@@ -1050,6 +1051,7 @@ describe("native workspace finalization recovery", () => {
|
||||
const rows = await db.execute(sql`select current_user as name`);
|
||||
const role = String(rows[0].name);
|
||||
await db.execute(sql`alter role ${sql.identifier(role)} set idle_in_transaction_session_timeout = '1s'`);
|
||||
await closeDedicatedDbConnections(db, 1);
|
||||
try {
|
||||
const result = await withNativeWorkspaceFinalizationOwnership({ db, companyId, runId: seeded.runId }, async ownership => {
|
||||
await new Promise(resolve => setTimeout(resolve, 3200));
|
||||
|
||||
Reference in new issue
Block a user