Skip to content

fix: closing a connection checkpoints its wal again - #27

Merged
pasichDev merged 2 commits into
mainfrom
fix/native-audit
Nov 10, 2026
Merged

pasichDev merged 2 commits into
mainfrom
fix/native-audit

Conversation

@pasichDev

Copy link
Copy Markdown
Owner

Merge order: before #25. Its BoundaryInputTest refuses a path with an embedded NUL on every target, which only passes on Apple targets with the second fix here.

What and why

Two defects in the connection layer, each proven by a test that failed on main:

  • A WAL database was never checkpointed on close. close() called sqlite3_interrupt before trying the connection lock, even when no other thread was using the connection. Nothing cleared that flag before sqlite3_close_v2. The checkpoint that the last connection of a WAL database runs on close stops at a pending interrupt, so -wal and -shm outlived 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 .db file alone after closing missed recent rows. Now close() interrupts only while another thread holds the connection. Once the statements are finalized, it compiles a throwaway SELECT 1, which is where SQLite drops a pending interrupt. Both parts are needed. JVM/Android (JniRawDb) and Kotlin/Native (NativeRawDb).
  • On Apple targets, a path with an embedded U+0000 opened the file named before it. "a.db\u0000-tail" opened or created a.db. The JNI binding already refused it. rawOpen now refuses it on Kotlin/Native too.

How it was tested

  • CloseTest.closingTheLastConnectionCheckpointsTheWalAndRemovesIt failed on the JVM before the fix. NativeBoundaryTest.aPathWithAnEmbeddedNulIsRefusedInsteadOfOpeningTheFileNamedBeforeIt failed on macosArm64 before the fix.
  • After: :sealite:jvmTest 163/163, :sealite:macosArm64Test 133/133, spotlessCheck, detekt
  • Platforms tested: jvm (macOS), macosArm64

Not verified: Android devices, the iOS simulator and jvmTestAsan were not run here. This PR's CI runs Android and iOS.

Checklist

  • Public API unchanged
  • No key material is logged, copied into a String or left unwiped
  • Docs and CHANGELOG-relevant behaviour described above
  • New source files carry the SPDX header (no new files)

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.
@pasichDev
pasichDev enabled auto-merge (squash) November 10, 2026 13:30
@pasichDev
pasichDev merged commit 6560154 into main Nov 10, 2026
31 of 33 checks passed
@pasichDev
pasichDev deleted the fix/native-audit branch November 10, 2026 14:08
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.

1 participant