From 9cb116af3944afbc270e70cacbe5cbbe5bb6eb0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 7 Mar 2024 11:58:05 +0100 Subject: [PATCH] fix: only release migration lock if acquired (#6454) We should not try to release the migration lock if where unable to acquire it. By trying to close it when we have not successfully connected to the database we end up hanging for a while before the process is eventually killed. I did not add a better error-message, as Unleash now gives a better error stack and crashes immediate if you start without a database password. We should still consider if you need to specify db credentials or not. Technically it is possible to have a postgres without a password (but it is likely not common). Closes: #6408 --- src/lib/util/db-lock.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/lib/util/db-lock.ts b/src/lib/util/db-lock.ts index 152e4b8650..030d887af2 100644 --- a/src/lib/util/db-lock.ts +++ b/src/lib/util/db-lock.ts @@ -24,10 +24,13 @@ export const withDbLock = const client = new Client({ ...dbConfig, }); + let lockAcquired = false; try { await client.connect(); // wait to obtain a lock await client.query('SELECT pg_advisory_lock($1)', [config.lockKey]); + lockAcquired = true; + const promise = fn(...args); const timeoutPromise = new Promise((_, reject) => setTimeout( @@ -41,9 +44,11 @@ export const withDbLock = config.logger.error(`Locking error: ${e.message}`); throw e; } finally { - await client.query('SELECT pg_advisory_unlock($1)', [ - config.lockKey, - ]); + if (lockAcquired) { + await client.query('SELECT pg_advisory_unlock($1)', [ + config.lockKey, + ]); + } await client.end(); } };