Skip to content

Fix journal_mode = WAL race condition #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 29, 2024
Merged

Conversation

rkistner
Copy link
Contributor

Checking journal_mode in tests could reproduce the issue quite consistently - journal_mode would be the default delete instead of wal 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.

@rkistner rkistner marked this pull request as ready for review October 29, 2024 11:48
@rkistner rkistner merged commit 5f702c5 into main Oct 29, 2024
3 checks passed
@rkistner rkistner deleted the fix-wal-race-condition branch October 29, 2024 11:48
@rkistner rkistner mentioned this pull request Oct 30, 2024
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.

2 participants