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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: drain nested database work before restoring a backup (#8623)
[._.]/ Adam Eivy684c8f97abd0
diff --git a/server/lib/db.js b/server/lib/db.jsindex 51e373d553..4e71c7bd69 100644--- a/server/lib/db.js+++ b/server/lib/db.js@@ -213,8 +213,7 @@ function maintenanceError() {}async function databaseOperation(fn) {- if (databaseContext.getStore()?.active) return fn();- if (maintenanceActive) throw maintenanceError();+ if (maintenanceActive && !databaseContext.getStore()?.active) throw maintenanceError();const context = { active: true };const pending = databaseContext.run(context, async () => fn());activeOperations.add(pending);@@ -232,7 +231,10 @@ export async function withDatabaseMaintenance(fn) {maintenanceActive = true;const context = { active: true };try {- await Promise.allSettled([...activeOperations]);+ // An admitted transaction can start nested work while draining. Track it+ // separately and repeat until that work also settles, even when its parent+ // did not await it. Async descendants lose admission when their operation ends.+ while (activeOperations.size) await Promise.allSettled([...activeOperations]);return await databaseContext.run(context, fn);} finally {context.active = false;diff --git a/server/lib/db.transaction.test.js b/server/lib/db.transaction.test.jsindex b6d275cb41..586673a275 100644--- a/server/lib/db.transaction.test.js+++ b/server/lib/db.transaction.test.js@@ -112,6 +112,33 @@ describe('withTransaction', () => {describe('database restore admission', () => {+ it('drains detached nested work that outlives its admitted parent', async () => {+ pool.client = makeClient(vi.fn().mockResolvedValue({}));+ let releaseQuery;+ pool.query.mockImplementationOnce(() => new Promise(resolve => { releaseQuery = resolve; }));+ let nestedQuery;+ let releaseParent;+ let enteredParent;+ const entered = new Promise(resolve => { enteredParent = resolve; });+ const parent = withTransaction(async () => {+ enteredParent();+ await new Promise(resolve => { releaseParent = resolve; });+ nestedQuery = query('SELECT 1');+ });+ await entered;+ let enteredMaintenance = false;+ const maintenance = withDatabaseMaintenance(async () => { enteredMaintenance = true; });+ releaseParent();+ await parent;+ await Promise.resolve();+ await Promise.resolve();+ expect(enteredMaintenance).toBe(false);+ releaseQuery();+ await nestedQuery;+ await maintenance;+ expect(enteredMaintenance).toBe(true);+ });+it('drains whole transactions, rejects new work, permits recovery, and releases after failure', async () => {pool.client = makeClient(vi.fn().mockResolvedValue({}));let finishTransaction;