Fix journal_mode = WAL race condition #53
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checking journal_mode in tests could reproduce the issue quite consistently -
journal_mode
would be the defaultdelete
instead ofwal
as it should be.The issue appeared to be related to opening the write and read connections for the new database at the same time. The error is never actually reported (return code is ignored), but I've seen similar issues with Flutter where it would return a DATABASE_LOCKED or similar error in the initial PRAGMA statements.
The workaround here is to run the initial PRAGMA statements in the main thread, right after opening the database. This ensures the write connection is opened first, initializing the database, before the read connections are opened.
This also sets
PRAGMA busy_timeout
which did appear to help when executing concurrently, but did not cover all cases.This also fixes some potential lock issues around
workQueue
being checked without locking first. I'm not aware of any actual issues this caused, but in theory this should be more stable.