From d545f8704aa9d2dd01d507239279473210f1e179 Mon Sep 17 00:00:00 2001 From: automated-signal <37887102+automated-signal@users.noreply.github.com> Date: Mon, 26 Feb 2024 18:57:20 -0600 Subject: [PATCH] Fix sequencing of database closes Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> --- ts/sql/Server.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index 928dfb2b1b..03cc4ba62c 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -642,25 +642,18 @@ async function initialize({ } async function close(): Promise { + globalReadonlyInstance?.close(); + globalReadonlyInstance = undefined; + // SQLLite documentation suggests that we run `PRAGMA optimize` right // before closing the database connection. globalWritableInstance?.pragma('optimize'); globalWritableInstance?.close(); globalWritableInstance = undefined; - globalReadonlyInstance?.close(); - globalReadonlyInstance = undefined; } async function removeDB(): Promise { - if (globalWritableInstance) { - try { - globalWritableInstance.close(); - } catch (error) { - logger.error('removeDB: Failed to close database:', error.stack); - } - globalWritableInstance = undefined; - } if (globalReadonlyInstance) { try { globalReadonlyInstance.close(); @@ -669,6 +662,14 @@ async function removeDB(): Promise { } globalReadonlyInstance = undefined; } + if (globalWritableInstance) { + try { + globalWritableInstance.close(); + } catch (error) { + logger.error('removeDB: Failed to close database:', error.stack); + } + globalWritableInstance = undefined; + } if (!databaseFilePath) { throw new Error( 'removeDB: Cannot erase database without a databaseFilePath!'