Skip to content

Web: Flush IndexedDB after transactions and RETURNING statements - #3865

Merged
simolus3 merged 1 commit into
simolus3:developfrom
sawongam:fix-indexeddb-flush-after-commit
Sep 16, 2026
Merged

simolus3 merged 1 commit into
simolus3:developfrom
sawongam:fix-indexeddb-flush-after-commit

Conversation

@sawongam

Copy link
Copy Markdown
Contributor

Fixes #3864

When using IndexedDB storage, _WasmDelegate only flushes if isInTransaction is false. The problem is that the COMMIT goes through runCustom while that flag is still true, and the flag only gets reset in _release() after the commit finished. Since the file system is opened with writeAutomatically: false, nothing written in a transaction ends up in IndexedDB until some unrelated write happens later. If the page is reloaded before that, the transaction is gone.

I ran into this in our app where the first-run setup creates the admin account in a transaction. On the deployed web build, refreshing right after setup always sent us back to setup because the account table was empty.

This PR checks database.autocommit instead, which is already true once the COMMIT (or ROLLBACK) statement has run, and is false for savepoints inside an outer transaction.

While looking at this I noticed runSelect never flushes either, so writes with a RETURNING clause (insertReturning etc.) had the same problem even outside of a transaction. I added an override for that too. flush() returns right away when there's no pending work, so plain selects shouldn't be affected.

I added a web integration test that inserts in a transaction and with insertReturning, reloads the page without any delay and checks both rows are still there. I ran the Firefox / dart2js tests locally, the new test fails on develop and passes with this change. I haven't been able to run the Chrome ones locally.

The COMMIT statement runs while isInTransaction is still set, so changes
made in a transaction were never flushed. Check sqlite3's autocommit state
instead, and also flush after selects since they can write too.

@simolus3 simolus3 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

Comment thread drift/lib/wasm.dart
Comment on lines +379 to +380
// Selects can write too (e.g. with a RETURNING clause). Flushing without
// pending writes is cheap.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I have thought about checking CommonPreparedStatement.isReadOnly for this, but since this doesn't do anything apart from checking the work list in the VFS, it should be fine.

@simolus3
simolus3 merged commit 27004b3 into simolus3:develop Sep 16, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Web] IndexedDB: writes inside a transaction are not flushed on commit and are lost on reload

2 participants