Repository navigation
fix: closing a connection checkpoints its wal again - #27
Merged
Merged
Conversation
close() called sqlite3_interrupt before it tried the lock, and nothing cleared the flag before sqlite3_close_v2. The checkpoint that the last connection of a WAL database runs on close stops at a pending interrupt, so the -wal and -shm files outlived the close and the rows committed since the last automatic checkpoint were not in the main database file. close() now interrupts only while another thread holds the connection, and compiles a throwaway statement once every statement is finalized: that is where SQLite drops a pending interrupt.
The native binding handed the path to sqlite3_open_v2 as a C string, which ends at the first NUL: the file named by the part before it was opened, and created. The JNI binding already refuses such a path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What and why
Two defects in the connection layer, each proven by a test that failed on
main:close()calledsqlite3_interruptbefore trying the connection lock, even when no other thread was using the connection. Nothing cleared that flag beforesqlite3_close_v2. The checkpoint that the last connection of a WAL database runs on close stops at a pending interrupt, so-waland-shmoutlived every close and the latest commits were only in-wal. No data was lost in place: the next open replays the WAL. But a copy or backup of the.dbfile alone after closing missed recent rows. Nowclose()interrupts only while another thread holds the connection. Once the statements are finalized, it compiles a throwawaySELECT 1, which is where SQLite drops a pending interrupt. Both parts are needed. JVM/Android (JniRawDb) and Kotlin/Native (NativeRawDb)."a.db\u0000-tail"opened or createda.db. The JNI binding already refused it.rawOpennow refuses it on Kotlin/Native too.How it was tested
CloseTest.closingTheLastConnectionCheckpointsTheWalAndRemovesItfailed on the JVM before the fix.NativeBoundaryTest.aPathWithAnEmbeddedNulIsRefusedInsteadOfOpeningTheFileNamedBeforeItfailed on macosArm64 before the fix.:sealite:jvmTest163/163,:sealite:macosArm64Test133/133,spotlessCheck,detektNot verified: Android devices, the iOS simulator and
jvmTestAsanwere not run here. This PR's CI runs Android and iOS.Checklist
Stringor left unwipedCHANGELOG-relevant behaviour described above