From 3fe63c328f1dbbfa3092e9aed5f8fe94838684d5 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 9 Nov 2022 14:34:40 +0100 Subject: [PATCH] fix(indexeddb): Call `IDBDatabase.close` manually. Surprisingly, `indexed_db_futures::IdbDatabase` is not closed when dropped. Hopefully, there is a [`IdbDatabase::close(&self)`][close] method, which calls `web_sys::IdbDatabase.close`, aka [`IDBDatabase.close`][websys-close], so let's use it! `IDBDatabase.close` returns immediately and closes the connection in a separate thread. The connection isn't actually closed until all transactions created using this connection are complete. No new transactions can be created for this connection once this method is called. Methods that create transactions throw an exception if a closing operation is pending. [close]: https://github.com/Alorel/rust-indexed-db/blob/8c106eb418aecdba2f3fde80d91a4673a875fdf6/src/idb_database.rs#L73-L77 [websys-close]: https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close --- crates/matrix-sdk-indexeddb/src/crypto_store.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store.rs b/crates/matrix-sdk-indexeddb/src/crypto_store.rs index fbd0caf58..7d88aa659 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store.rs @@ -283,6 +283,10 @@ impl IndexeddbCryptoStore { } }; + // Must release the database access manually as it's not done when + // dropping it. + db.close(); + IndexeddbCryptoStore::open_with_store_cipher(prefix, Some(store_cipher.into())).await } @@ -943,6 +947,14 @@ impl IndexeddbCryptoStore { } } +impl Drop for IndexeddbCryptoStore { + fn drop(&mut self) { + // Must release the database access manually as it's not done when + // dropping it. + self.inner.close(); + } +} + #[cfg(target_arch = "wasm32")] #[async_trait(?Send)] impl CryptoStore for IndexeddbCryptoStore {