From eaecbf63fcc29a4c7a08e54e9fe34da9938c968c Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Thu, 14 Nov 2024 17:47:06 +0100 Subject: [PATCH 1/2] fix: race conditions on db open and close --- examples/SampleApp/src/hooks/useChatClient.ts | 2 +- package/src/store/SqliteClient.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/SampleApp/src/hooks/useChatClient.ts b/examples/SampleApp/src/hooks/useChatClient.ts index 982a85b8f3..36d68319d5 100644 --- a/examples/SampleApp/src/hooks/useChatClient.ts +++ b/examples/SampleApp/src/hooks/useChatClient.ts @@ -181,7 +181,7 @@ export const useChatClient = () => { }; const logout = async () => { - SqliteClient.resetDB(); + await SqliteClient.resetDB(); setChatClient(null); chatClient?.disconnectUser(); await AsyncStore.removeItem('@stream-rn-sampleapp-login-config'); diff --git a/package/src/store/SqliteClient.ts b/package/src/store/SqliteClient.ts index 9b33106e24..b248febd42 100644 --- a/package/src/store/SqliteClient.ts +++ b/package/src/store/SqliteClient.ts @@ -66,6 +66,7 @@ export class SqliteClient { throw new Error('DB is not open or initialized.'); } this.db.close(); + this.db = undefined; } catch (e) { this.logger?.('error', `Error closing database ${SqliteClient.dbName}`, { error: e, @@ -200,7 +201,10 @@ export class SqliteClient { static resetDB = async () => { this.logger?.('info', `resetDB`); - SqliteClient.dropTables(); + await SqliteClient.dropTables(); + if (this.db) { + SqliteClient.closeDB(); + } await SqliteClient.initializeDatabase(); }; } From d353d3236ae789d20d135b090df3e4db70f80b59 Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Thu, 14 Nov 2024 17:50:14 +0100 Subject: [PATCH 2/2] fix: move dropTables into check as well --- package/src/store/SqliteClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/src/store/SqliteClient.ts b/package/src/store/SqliteClient.ts index b248febd42..270a7309f6 100644 --- a/package/src/store/SqliteClient.ts +++ b/package/src/store/SqliteClient.ts @@ -201,8 +201,8 @@ export class SqliteClient { static resetDB = async () => { this.logger?.('info', `resetDB`); - await SqliteClient.dropTables(); if (this.db) { + await SqliteClient.dropTables(); SqliteClient.closeDB(); } await SqliteClient.initializeDatabase();