Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-4910]): release failed transaction clients

[._.]/ Adam Eivy8198f758d13c
diff --git a/server/lib/db.js b/server/lib/db.js
index 8360980374..ca5d6d5525 100644
--- a/server/lib/db.js
+++ b/server/lib/db.js
@@ -251,18 +251,19 @@ export async function withTransaction(fn) {
// client.query accepts either a SQL string or a { text, values } config —
// GUARDED_CLIENT_HANDLER reads the SQL out of both.
const guardedClient = new Proxy(client, GUARDED_CLIENT_HANDLER);
- await client.query('BEGIN');
- let result;
+ let began = false;
try {
- result = await fn(guardedClient);
+ await client.query('BEGIN');
+ began = true;
+ const result = await fn(guardedClient);
await client.query('COMMIT');
+ return result;
} catch (err) {
- await client.query('ROLLBACK');
+ if (began) await client.query('ROLLBACK');
throw err;
} finally {
client.release();
}
- return result;
}
/**
diff --git a/server/lib/db.transaction.test.js b/server/lib/db.transaction.test.js
new file mode 100644
index 0000000000..a670c835fe
--- /dev/null
+++ b/server/lib/db.transaction.test.js
@@ -0,0 +1,80 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const pool = vi.hoisted(() => ({
+ client: null,
+ connect: vi.fn(),
+ on: vi.fn(),
+ query: vi.fn(),
+}));
+
+vi.mock('pg', () => ({
+ default: {
+ Pool: vi.fn(function Pool() { return pool; }),
+ },
+}));
+
+import { withTransaction } from './db.js';
+
+function makeClient(query) {
+ return {
+ query,
+ release: vi.fn(),
+ };
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ pool.connect.mockImplementation(async () => pool.client);
+});
+
+describe('withTransaction', () => {
+ it('releases the checked-out client when BEGIN fails', async () => {
+ const beginError = new Error('connection reset during BEGIN');
+ const client = makeClient(vi.fn().mockRejectedValueOnce(beginError));
+ pool.client = client;
+
+ await expect(withTransaction(vi.fn())).rejects.toBe(beginError);
+
+ expect(client.query.mock.calls).toEqual([['BEGIN']]);
+ expect(client.release).toHaveBeenCalledOnce();
+ });
+
+ it('rolls back and releases the client when the handler throws', async () => {
+ const handlerError = new Error('handler failed');
+ const client = makeClient(vi.fn().mockResolvedValue({}));
+ pool.client = client;
+
+ await expect(withTransaction(async () => { throw handlerError; })).rejects.toBe(handlerError);
+
+ expect(client.query.mock.calls).toEqual([['BEGIN'], ['ROLLBACK']]);
+ expect(client.release).toHaveBeenCalledOnce();
+ });
+
+ it('commits, releases, and returns the handler result on success', async () => {
+ const client = makeClient(vi.fn().mockResolvedValue({}));
+ pool.client = client;
+
+ await expect(withTransaction(async () => 'saved')).resolves.toBe('saved');
+
+ expect(client.query.mock.calls).toEqual([['BEGIN'], ['COMMIT']]);
+ expect(client.release).toHaveBeenCalledOnce();
+ });
+
+ it('keeps savepoint queries available to transaction handlers', async () => {
+ const client = makeClient(vi.fn().mockResolvedValue({}));
+ pool.client = client;
+
+ await withTransaction(async (transaction) => {
+ await transaction.query('SAVEPOINT nested_work');
+ await transaction.query('ROLLBACK TO SAVEPOINT nested_work');
+ });
+
+ expect(client.query.mock.calls).toEqual([
+ ['BEGIN'],
+ ['SAVEPOINT nested_work'],
+ ['ROLLBACK TO SAVEPOINT nested_work'],
+ ['COMMIT'],
+ ]);
+ expect(client.release).toHaveBeenCalledOnce();
+ });
+});