Database support for cancellation

This commit is contained in:
Gwendal Roué committed 2024-07-27 16:37:34 +02:00
1 parent fa92ba6cd5
commit 4ddf4bca5f
3 files changed
+127 -27

No files matched your search

@@ -436,6 +436,7 @@ extension Database {
// documentation of this method for more information).
try checkForAbortedTransaction(sql: statement.sql, arguments: statement.arguments)
// Cancelled database accesses must not execute.
// Suspended databases must not execute statements that create the risk
// of `0xdead10cc` exception (see the documentation of this method for
// more information).
@@ -488,6 +489,19 @@ extension Database {
// and throws the user-provided cancelled commit error.
try observationBroker?.statementDidFail(statement)
if #available(iOS 13, macOS 10.15, tvOS 13, *) {
switch ResultCode(rawValue: resultCode) {
case .SQLITE_INTERRUPT, .SQLITE_ABORT:
if suspensionMutex.load().isCancelled {
// The only error that a user sees when a Task is cancelled
// is CancellationError.
throw CancellationError()
}
default:
break
}
}
// Throw statement failure
throw DatabaseError(
resultCode: resultCode,
+112 -26
View File
@@ -314,18 +314,29 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
/// `isRecordingSelectedRegion` is true.
var selectedRegion = DatabaseRegion()
/// Support for `checkForAbortedTransaction()`
var isInsideTransactionBlock = false
/// Support for `checkForSuspensionViolation(from:)`
let isSuspendedMutex = Mutex(false)
/// Support for `checkForSuspensionViolation(from:)`
/// This cache is never cleared: we assume journal mode never changes.
var journalModeCache: String?
// MARK: - Suspension
struct Suspension {
/// If true, the database is suspended and should not acquire any
/// write lock in order to avoid the 0xDEAD10CC exception.
var isSuspended: Bool
/// If true, the database access has been cancelled.
var isCancelled: Bool
}
/// Support for `checkForSuspensionViolation(from:)`
let suspensionMutex = Mutex(Suspension(isSuspended: false, isCancelled: false))
// MARK: - Transaction Date
/// Support for `checkForAbortedTransaction()`
var isInsideTransactionBlock = false
enum AutocommitState {
case off
case on
@@ -637,7 +648,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
guard code == SQLITE_OK else {
// So there remain some unfinalized prepared statement somewhere.
if let log = Self.logError {
if code == SQLITE_BUSY {
if ResultCode(rawValue: code).primaryResultCode == .SQLITE_BUSY {
// Let the user know about unfinalized statements that did
// prevent the connection from closing properly.
var stmt: SQLiteStatement? = sqlite3_next_stmt(sqliteConnection, nil)
@@ -1151,12 +1162,12 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
///
/// Suspension ends with `resume()`.
func suspend() {
let needsInterrupt = isSuspendedMutex.withLock { isSuspended in
if isSuspended {
let needsInterrupt = suspensionMutex.withLock { suspension in
if suspension.isSuspended {
return false
}
isSuspended = true
suspension.isSuspended = true
return true
}
@@ -1181,7 +1192,37 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
///
/// See suspend().
func resume() {
isSuspendedMutex.store(false)
suspensionMutex.withLock {
$0.isSuspended = false
}
}
/// Cancels the current database access. All statements but ROLLBACK
/// will throw `CancellationError`, until `uncancel()` is called.
///
/// This method can be called from any thread.
@available(iOS 13, macOS 10.15, tvOS 13, *)
func cancel() {
let needsInterrupt = suspensionMutex.withLock { suspension in
if suspension.isCancelled {
return false
}
suspension.isCancelled = true
return true
}
if needsInterrupt {
interrupt()
}
}
/// Undo `cancel()`.
@available(iOS 13, macOS 10.15, tvOS 13, *)
func uncancel() {
suspensionMutex.withLock {
$0.isCancelled = false
}
}
/// Support for `checkForSuspensionViolation(from:)`
@@ -1205,15 +1246,51 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
return journalMode
}
/// If the database is suspended, and executing the statement would lock the
/// database in a way that may trigger the [`0xdead10cc` exception](https://developer.apple.com/documentation/xcode/understanding-the-exception-types-in-a-crash-report),
/// this method rollbacks the current transaction and throws `SQLITE_ABORT`.
/// Prevents a statement from running, if the database is suspended, or
/// if the current database access is cancelled by Task cancellation.
///
/// See `suspend()` and ``Configuration/observesSuspensionNotifications``.
/// Transaction rollbacks are always allowed. For other statements:
///
/// - When database access is cancelled, this method
/// throws `CancellationError`.
///
/// - When database is suspensed, and if the statement would lock the
/// database in a way that may trigger the 0xDEAD10CC exception, this
/// method rollbacks the current transaction and throws `SQLITE_ABORT`.
///
/// See `cancel()`, `suspend()` and
/// ``Configuration/observesSuspensionNotifications``.
func checkForSuspensionViolation(from statement: Statement) throws {
let needsAbort = try isSuspendedMutex.withLock { isSuspended in
guard isSuspended else {
return false
// No reason for suspension should prevent rollbacks:
//
// - A rollback releases the write lock when the database
// is interrupted, when preventing 0xDEAD10CC.
//
// - A rollback properly closes a transaction that fails because
// it runs in a Task that was cancelled.
//
// Finally, a rollback must be run by GRDB, not by a direct call
// to `sqlite3_exec`, so that transaction observers are
// properly notified.
if statement.transactionEffect == .rollbackTransaction {
return
}
// How should we interrupt the statement?
enum Interrupt {
case abort // Rollback and throw SQLITE_ABORT
case cancel // Throw CancellationError
}
let interrupt: Interrupt? = try suspensionMutex.withLock { suspension in
// Check for cancellation first, so that the only error that
// a user sees when a Task is cancelled is CancellationError.
if suspension.isCancelled {
return .cancel
}
guard suspension.isSuspended else {
return nil
}
if try journalMode() == "wal" && statement.isReadonly {
@@ -1224,7 +1301,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
// Those are not read-only:
// - INSERT ...
// - BEGIN IMMEDIATE TRANSACTION
return false
return nil
}
if statement.releasesDatabaseLock {
@@ -1233,20 +1310,29 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
// - ROLLBACK
// - ROLLBACK TRANSACTION TO SAVEPOINT
// - RELEASE SAVEPOINT
return false
return nil
}
// Assume statement can acquire a write lock: abort.
return true
return .abort
}
if needsAbort {
switch interrupt {
case nil:
break
case .cancel:
if #available(iOS 13, macOS 10.15, tvOS 13, *) {
throw CancellationError()
} else {
// GRDB bug: cancellation is a Swift concurrency feature
fatalError("Can't cancel without support for Swift concurrency")
}
case .abort:
// Attempt at releasing an eventual lock with ROLLBACk,
// as explained in Database.suspend().
//
// Use sqlite3_exec instead of `try? rollback()` in order to avoid
// an infinite loop in checkForSuspensionViolation(from:)
_ = sqlite3_exec(sqliteConnection, "ROLLBACK", nil, nil, nil)
try? rollback()
throw DatabaseError(
resultCode: .SQLITE_ABORT,
+1 -1
View File
@@ -35,7 +35,7 @@ extension String {
}
public final class Statement {
enum TransactionEffect {
enum TransactionEffect: Equatable {
case beginTransaction
case commitTransaction
case rollbackTransaction