Conversation
On iOS, the SQLite C library defaults `busy_timeout` to 0 ms. When multiple connections access the same underlying database concurrently (such as an app and an app extension sharing an App Group container), any lock contention immediately throws `SQLiteException: Error code: 5, message: database is locked`. Configure a default busy timeout of 5 seconds for SQLite connections on iOS: - In `SqliteStorage`, accept a `busyTimeout: Duration = 5.seconds` parameter, validate that it is non-negative, and execute `PRAGMA busy_timeout` in the `init` block. Provide backward-compatible constructors. - In `IosStorage`, delegate `busyTimeout` configuration to `SqliteStorage`, and provide secondary constructors with `(storageFileUrl, excludeFromBackup)` and `(storageFileUrl, excludeFromBackup, busyTimeoutMs)` overloads for Swift/Objective-C compatibility. - In `Storage`, update KDoc to document concurrency and multi-instance safety guarantees across processes. - In `IosStorageTest`, add unit test `testBusyTimeout()` reproducing the zero-timeout failure under lock contention, verifying that the default 5-second timeout waits and succeeds, and verifying that exceeding the timeout fails. Add `testSqliteStorageBusyTimeout()` verifying standalone `SqliteStorage` instances. - In `AndroidStorageTest`, add `testBusyTimeout()` verifying that Android SQLite automatically configures a busy timeout (>= 2000 ms) and handles contention without error. Fixes #1979. Test: Ran ./gradlew :multipaz:jvmTest Test: Ran ./gradlew :multipaz:iosSimulatorArm64Test Test: Ran ./gradlew :multipaz:connectedDebugAndroidTest (on Pixel 8 Pro) Test: Ran ./gradlew detekt Test: Ran xcodebuild -project samples/SwiftTestApp/SwiftTestApp.xcodeproj -scheme SwiftTestApp -sdk iphonesimulator build Signed-off-by: David Zeuthen <zeuthen@gmail.com>
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.
On iOS, the SQLite C library defaults
busy_timeoutto 0 ms. When multiple connections access the same underlying database concurrently (such as an app and an app extension sharing an App Group container), any lock contention immediately throwsSQLiteException: Error code: 5, message: database is locked.Configure a default busy timeout of 5 seconds for SQLite connections on iOS:
SqliteStorage, accept abusyTimeout: Duration = 5.secondsparameter, validate that it is non-negative, and executePRAGMA busy_timeoutin theinitblock. Provide backward-compatible constructors.IosStorage, delegatebusyTimeoutconfiguration toSqliteStorage, and provide secondary constructors with(storageFileUrl, excludeFromBackup)and(storageFileUrl, excludeFromBackup, busyTimeoutMs)overloads for Swift/Objective-C compatibility.Storage, update KDoc to document concurrency and multi-instance safety guarantees across processes.IosStorageTest, add unit testtestBusyTimeout()reproducing the zero-timeout failure under lock contention, verifying that the default 5-second timeout waits and succeeds, and verifying that exceeding the timeout fails. AddtestSqliteStorageBusyTimeout()verifying standaloneSqliteStorageinstances.AndroidStorageTest, addtestBusyTimeout()verifying that Android SQLite automatically configures a busy timeout (>= 2000 ms) and handles contention without error.Fixes #1979.
Test: Ran ./gradlew :multipaz:jvmTest
Test: Ran ./gradlew :multipaz:iosSimulatorArm64Test
Test: Ran ./gradlew :multipaz:connectedDebugAndroidTest (on Pixel 8 Pro)
Test: Ran ./gradlew detekt
Test: Ran xcodebuild -project samples/SwiftTestApp/SwiftTestApp.xcodeproj -scheme SwiftTestApp -sdk iphonesimulator build