1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-10 01:16:39 +02:00

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
This commit is contained in:
Ivar Conradi Østhus 2024-03-07 11:58:05 +01:00 committed by GitHub
parent 2cd80d31f8
commit 9cb116af39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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();
}
};