Truly independent web browser
LibWeb: Let IndexedDB connections drop their finished transactions
Andreas Kling9a3a9bc48d34
diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_database.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_database.rsindex c7561f156e2..f691b04548a 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_database.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_database.rs@@ -92,6 +92,8 @@ pub(crate) struct IDBDatabase {realm: Gc<Realm>,// NOTE: We need to keep track of what transactions were created by this connection+ // NB: Only the ones that have yet to finish, which is all that closing the connection and scheduling transactions+ // look at. Keeping the finished ones would keep every transaction of a long-lived connection alive.transactions: GcRefCell<Vec<Ref<IDBTransaction>>>,}@@ -193,6 +195,17 @@ impl IDBDatabase {self.transactions.borrow_mut(vm).push(transaction);}+ pub(crate) fn remove_finished_transaction(&self, vm: &Vm, transaction: Ref<IDBTransaction>) {+ assert!(transaction.is_finished());+ let mut transactions = self.transactions.borrow_mut(vm);+ let position = transactions+ .iter()+ .position(|entry| *entry == transaction)+ .expect("a transaction finishes once, on the connection that created it");+ // NB: Keep the order, which block_on_conflicting_transactions() relies on to find earlier transactions.+ transactions.remove(position);+ }+pub(crate) fn name(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {Ok(self.name.clone())}@@ -532,6 +545,10 @@ impl IDBDatabase {vm,&blocking.values(),create_function(vm, transaction, |vm, &transaction, ()| {+ // NB: The transaction may have been aborted while it was blocked, and must not be committed after.+ if transaction.is_finished() {+ return;+ }assert!(transaction.state() != TransactionState::Active);if transaction.request_list().is_empty() {// https://w3c.github.io/IndexedDB/#transaction-commit@@ -545,7 +562,10 @@ impl IDBDatabase {// FIXME: Update if this becomes explicit:// https://github.com/w3c/IndexedDB/issues/489#issuecomment-3994928473- commit_a_transaction(vm, transaction);+ // NB: A transaction that commit() was called on while it was blocked is already committing.+ if transaction.state() != TransactionState::Committing {+ commit_a_transaction(vm, transaction);+ }return;}if !transaction.is_readonly() {diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_transaction.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_transaction.rsindex a1f9214ca11..dfcc27710ea 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_transaction.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_transaction.rs@@ -202,9 +202,16 @@ impl IDBTransaction {}pub(crate) fn set_state(&self, state: TransactionState) {+ // NB: Finishing also has to remove the transaction from its connection, which set_finished() does.+ assert!(state != TransactionState::Finished);self.state.set(state);}+ pub(crate) fn set_finished(vm: &Vm, transaction: Ref<Self>) {+ transaction.state.set(TransactionState::Finished);+ transaction.connection.remove_finished_transaction(vm, transaction);+ }+pub(crate) fn is_upgrade_transaction(&self) -> bool {self.mode == IDBTransactionMode::Versionchange}diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/algorithms.rsindex d994705cd19..c046354f2dd 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/algorithms.rs@@ -864,7 +864,7 @@ pub(crate) fn abort_a_transaction(vm: &Vm, transaction: Ref<IDBTransaction>, err}// 4. Set transaction’s state to finished.- transaction.set_state(TransactionState::Finished);+ IDBTransaction::set_finished(vm, transaction);// AD-HOC: Clear the cleanup event loop so that the microtask checkpoint doesn't set the state to inactive on a// finished transaction.@@ -1173,7 +1173,7 @@ pub(crate) fn commit_a_transaction(vm: &Vm, transaction: Ref<IDBTransaction>) {transaction.discard_mutation_logs(vm);// 2. Set transaction’s state to finished.- transaction.set_state(TransactionState::Finished);+ IDBTransaction::set_finished(vm, transaction);// 3. Fire an event named complete at transaction.transaction.event_target().dispatch_event(&CppEvent::create(diff --git a/Tests/LibWeb/Text/expected/indexeddb-abort-blocked-transaction.txt b/Tests/LibWeb/Text/expected/indexeddb-abort-blocked-transaction.txtnew file mode 100644index 00000000000..75ccb266b14--- /dev/null+++ b/Tests/LibWeb/Text/expected/indexeddb-abort-blocked-transaction.txt@@ -0,0 +1,3 @@+blocked: abort+first: complete+last: completediff --git a/Tests/LibWeb/Text/expected/indexeddb-commit-blocked-transaction.txt b/Tests/LibWeb/Text/expected/indexeddb-commit-blocked-transaction.txtnew file mode 100644index 00000000000..2f13f0aacac--- /dev/null+++ b/Tests/LibWeb/Text/expected/indexeddb-commit-blocked-transaction.txt@@ -0,0 +1,2 @@+blocked: complete+last: completediff --git a/Tests/LibWeb/Text/input/indexeddb-abort-blocked-transaction.html b/Tests/LibWeb/Text/input/indexeddb-abort-blocked-transaction.htmlnew file mode 100644index 00000000000..f13eccc08f3--- /dev/null+++ b/Tests/LibWeb/Text/input/indexeddb-abort-blocked-transaction.html@@ -0,0 +1,35 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+ // A transaction that is aborted while it waits for an earlier transaction to finish stays aborted, and is not+ // committed once the earlier transaction finishes.+ promiseTest(async () => {+ spoofCurrentURL("https://example.com/indexeddb-abort-blocked-transaction.html");++ const db = await new Promise((resolve, reject) => {+ const request = indexedDB.open("abort-blocked-transaction-" + Date.now() + Math.random(), 1);+ request.onupgradeneeded = () => request.result.createObjectStore("store");+ request.onsuccess = () => resolve(request.result);+ request.onerror = () => reject(request.error);+ });++ const first = db.transaction("store", "readwrite");+ first.objectStore("store").put("value", "key");+ first.oncomplete = () => println("first: complete");++ const blocked = db.transaction("store", "readwrite");+ blocked.oncomplete = () => println("blocked: complete");+ blocked.onabort = () => println("blocked: abort");+ blocked.abort();++ await new Promise(resolve => {+ const last = db.transaction("store", "readwrite");+ last.objectStore("store").get("key");+ last.oncomplete = () => {+ println("last: complete");+ resolve();+ };+ });+ db.close();+ });+</script>diff --git a/Tests/LibWeb/Text/input/indexeddb-commit-blocked-transaction.html b/Tests/LibWeb/Text/input/indexeddb-commit-blocked-transaction.htmlnew file mode 100644index 00000000000..8c20df30246--- /dev/null+++ b/Tests/LibWeb/Text/input/indexeddb-commit-blocked-transaction.html@@ -0,0 +1,34 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+ // A transaction that commit() is called on while it waits for an earlier transaction to finish is committed+ // once, not again when the earlier transaction finishes.+ promiseTest(async () => {+ spoofCurrentURL("https://example.com/indexeddb-commit-blocked-transaction.html");++ const db = await new Promise((resolve, reject) => {+ const request = indexedDB.open("commit-blocked-transaction-" + Date.now() + Math.random(), 1);+ request.onupgradeneeded = () => request.result.createObjectStore("store");+ request.onsuccess = () => resolve(request.result);+ request.onerror = () => reject(request.error);+ });++ const first = db.transaction("store", "readwrite");+ first.commit();++ const blocked = db.transaction("store", "readwrite");+ blocked.oncomplete = () => println("blocked: complete");+ blocked.onabort = () => println("blocked: abort");+ blocked.commit();++ await new Promise(resolve => {+ const last = db.transaction("store", "readwrite");+ last.objectStore("store").get("key");+ last.oncomplete = () => {+ println("last: complete");+ resolve();+ };+ });+ db.close();+ });+</script>