Files
ladybird/Tests/LibWeb/Text/input/indexeddb-close-waits-for-transaction.html
Zaggy1024 3c24a394c6 LibWeb: Refactor IndexedDB to handle requests serially
Previously, after one request was marked as processed, we would
synchronously queue another task to process the next request. This
would mean that two open requests on the same database could
interleave. This was especially problematic when one of the requests
would cause the database to upgrade, since the second open request
would begin processing before the upgradeneeded event fired, causing an
exception to be thrown in the second open().

The solution is to explicitly check for continuation conditions after
events have been fired in order to ensure that every step for the
request is completed before starting any further request processing.

For connection requests, the spec states:

> Open requests are processed in a connection queue. The queue contains
> all open requests associated with an storage key and a name. Requests
> added to the connection queue processed in order and each request
> must run to completion before the next request is processed. An open
> request may be blocked on other connections, requiring those
> connections to close before the request can complete and allow
> further requests to be processed.

For requests against a transaction, the spec states:

> Once the transaction has been started the implementation can begin
> executing the requests placed against the transaction. Requests must
> be executed in the order in which they were made against the
> transaction. Likewise, their results must be returned in the order
> the requests were placed against a specific transaction. There is no
> guarantee about the order that results from requests in different
> transactions are returned.

In the process of reworking it to use this approach, I've added a bunch
of new tests that cover things that our imported WPTs weren't checking.

With the fix for serializing connection requests, we can now fully
download the assets for the emscripten-compiled asm.js games in the
Humble Mozilla Bundle, particularly FTL: Faster Than Light.

There were no regressions in our test suite. One web platform test,
'idbindex_reverse_cursor.any.html', has one newly-failing subtest, but
the subtest was apparently only passing by chance due synchronous
execution of requests. A few web platform tests that were added in a
prior commit improved. The delete-request-queue.any.html test has
stopped crashing, and the close-in-upgrade-needed.any.html test has
stopped flaking, so they are both imported here as well.

Incidentally fixes #7512, for which a crash test has been added.
2026-03-05 17:12:55 -06:00

78 lines
2.7 KiB
HTML

<!DOCTYPE html>
<script src="include.js"></script>
<script>
asyncTest(done => {
setTimeout(() => {
spoofCurrentURL("https://example.com/indexeddb-close-waits-for-transaction.html");
// Test that closing a database connection waits for running
// transactions to finish before fully closing. This exercises
// wait_for_transactions_to_finish in close_a_database_connection.
const dbName = "test-close-waits-txn-" + Date.now() + Math.random();
const setupReq = indexedDB.open(dbName, 1);
setupReq.onupgradeneeded = (e) => {
println("setup.upgradeneeded");
const db = e.target.result;
db.createObjectStore("store");
};
setupReq.onsuccess = (e) => {
println("setup.success");
const conn = e.target.result;
// Start a readwrite transaction and do a put.
const tx = conn.transaction("store", "readwrite");
const store = tx.objectStore("store");
const putReq = store.put("value", "key");
putReq.onsuccess = () => {
println("put.success");
};
putReq.onerror = (e) => {
println("put.error: " + e.target.error.name);
};
tx.oncomplete = () => {
println("tx.complete");
};
tx.onerror = (e) => {
println("tx.error: " + e.target.error.name);
};
// Close the connection while the transaction is still running.
// The close should set the close pending flag but wait for
// the transaction to complete.
conn.close();
println("conn.close() called");
// Open a v2 upgrade to verify the connection actually closed.
// This should proceed after the transaction finishes and
// the connection fully closes.
const upgradeReq = indexedDB.open(dbName, 2);
upgradeReq.onupgradeneeded = () => {
println("upgrade.upgradeneeded");
};
upgradeReq.onsuccess = (e) => {
println("upgrade.success");
e.target.result.close();
indexedDB.deleteDatabase(dbName);
println("DONE");
done();
};
upgradeReq.onerror = (e) => {
println("upgrade.error: " + e.target.error.name);
indexedDB.deleteDatabase(dbName);
println("DONE");
done();
};
};
setupReq.onerror = (e) => {
println("setup.error: " + e.target.error.name);
println("DONE");
done();
};
}, 0);
});
</script>