mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Database support for cancellation
This commit is contained in:
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).
|
// documentation of this method for more information).
|
||||||
try checkForAbortedTransaction(sql: statement.sql, arguments: statement.arguments)
|
try checkForAbortedTransaction(sql: statement.sql, arguments: statement.arguments)
|
||||||
|
|
||||||
|
// Cancelled database accesses must not execute.
|
||||||
// Suspended databases must not execute statements that create the risk
|
// Suspended databases must not execute statements that create the risk
|
||||||
// of `0xdead10cc` exception (see the documentation of this method for
|
// of `0xdead10cc` exception (see the documentation of this method for
|
||||||
// more information).
|
// more information).
|
||||||
@@ -488,6 +489,19 @@ extension Database {
|
|||||||
// and throws the user-provided cancelled commit error.
|
// and throws the user-provided cancelled commit error.
|
||||||
try observationBroker?.statementDidFail(statement)
|
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 statement failure
|
||||||
throw DatabaseError(
|
throw DatabaseError(
|
||||||
resultCode: resultCode,
|
resultCode: resultCode,
|
||||||
|
|||||||
@@ -314,18 +314,29 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
/// `isRecordingSelectedRegion` is true.
|
/// `isRecordingSelectedRegion` is true.
|
||||||
var selectedRegion = DatabaseRegion()
|
var selectedRegion = DatabaseRegion()
|
||||||
|
|
||||||
/// Support for `checkForAbortedTransaction()`
|
|
||||||
var isInsideTransactionBlock = false
|
|
||||||
|
|
||||||
/// Support for `checkForSuspensionViolation(from:)`
|
|
||||||
let isSuspendedMutex = Mutex(false)
|
|
||||||
|
|
||||||
/// Support for `checkForSuspensionViolation(from:)`
|
/// Support for `checkForSuspensionViolation(from:)`
|
||||||
/// This cache is never cleared: we assume journal mode never changes.
|
/// This cache is never cleared: we assume journal mode never changes.
|
||||||
var journalModeCache: String?
|
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
|
// MARK: - Transaction Date
|
||||||
|
|
||||||
|
/// Support for `checkForAbortedTransaction()`
|
||||||
|
var isInsideTransactionBlock = false
|
||||||
|
|
||||||
enum AutocommitState {
|
enum AutocommitState {
|
||||||
case off
|
case off
|
||||||
case on
|
case on
|
||||||
@@ -637,7 +648,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
guard code == SQLITE_OK else {
|
guard code == SQLITE_OK else {
|
||||||
// So there remain some unfinalized prepared statement somewhere.
|
// So there remain some unfinalized prepared statement somewhere.
|
||||||
if let log = Self.logError {
|
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
|
// Let the user know about unfinalized statements that did
|
||||||
// prevent the connection from closing properly.
|
// prevent the connection from closing properly.
|
||||||
var stmt: SQLiteStatement? = sqlite3_next_stmt(sqliteConnection, nil)
|
var stmt: SQLiteStatement? = sqlite3_next_stmt(sqliteConnection, nil)
|
||||||
@@ -1151,12 +1162,12 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
///
|
///
|
||||||
/// Suspension ends with `resume()`.
|
/// Suspension ends with `resume()`.
|
||||||
func suspend() {
|
func suspend() {
|
||||||
let needsInterrupt = isSuspendedMutex.withLock { isSuspended in
|
let needsInterrupt = suspensionMutex.withLock { suspension in
|
||||||
if isSuspended {
|
if suspension.isSuspended {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
isSuspended = true
|
suspension.isSuspended = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1181,7 +1192,37 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
///
|
///
|
||||||
/// See suspend().
|
/// See suspend().
|
||||||
func resume() {
|
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:)`
|
/// Support for `checkForSuspensionViolation(from:)`
|
||||||
@@ -1205,15 +1246,51 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
return journalMode
|
return journalMode
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If the database is suspended, and executing the statement would lock the
|
/// Prevents a statement from running, if the database is suspended, or
|
||||||
/// database in a way that may trigger the [`0xdead10cc` exception](https://developer.apple.com/documentation/xcode/understanding-the-exception-types-in-a-crash-report),
|
/// if the current database access is cancelled by Task cancellation.
|
||||||
/// this method rollbacks the current transaction and throws `SQLITE_ABORT`.
|
|
||||||
///
|
///
|
||||||
/// 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 {
|
func checkForSuspensionViolation(from statement: Statement) throws {
|
||||||
let needsAbort = try isSuspendedMutex.withLock { isSuspended in
|
// No reason for suspension should prevent rollbacks:
|
||||||
guard isSuspended else {
|
//
|
||||||
return false
|
// - 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 {
|
if try journalMode() == "wal" && statement.isReadonly {
|
||||||
@@ -1224,7 +1301,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
// Those are not read-only:
|
// Those are not read-only:
|
||||||
// - INSERT ...
|
// - INSERT ...
|
||||||
// - BEGIN IMMEDIATE TRANSACTION
|
// - BEGIN IMMEDIATE TRANSACTION
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.releasesDatabaseLock {
|
if statement.releasesDatabaseLock {
|
||||||
@@ -1233,20 +1310,29 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
// - ROLLBACK
|
// - ROLLBACK
|
||||||
// - ROLLBACK TRANSACTION TO SAVEPOINT
|
// - ROLLBACK TRANSACTION TO SAVEPOINT
|
||||||
// - RELEASE SAVEPOINT
|
// - RELEASE SAVEPOINT
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assume statement can acquire a write lock: abort.
|
// 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,
|
// Attempt at releasing an eventual lock with ROLLBACk,
|
||||||
// as explained in Database.suspend().
|
// as explained in Database.suspend().
|
||||||
//
|
try? rollback()
|
||||||
// Use sqlite3_exec instead of `try? rollback()` in order to avoid
|
|
||||||
// an infinite loop in checkForSuspensionViolation(from:)
|
|
||||||
_ = sqlite3_exec(sqliteConnection, "ROLLBACK", nil, nil, nil)
|
|
||||||
|
|
||||||
throw DatabaseError(
|
throw DatabaseError(
|
||||||
resultCode: .SQLITE_ABORT,
|
resultCode: .SQLITE_ABORT,
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ extension String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final class Statement {
|
public final class Statement {
|
||||||
enum TransactionEffect {
|
enum TransactionEffect: Equatable {
|
||||||
case beginTransaction
|
case beginTransaction
|
||||||
case commitTransaction
|
case commitTransaction
|
||||||
case rollbackTransaction
|
case rollbackTransaction
|
||||||
|
|||||||
Reference in new issue
Block a user