Async database accesses check for cancellation before returning

This commit is contained in:
Gwendal Roué committed 2024-09-01 15:35:37 +02:00
1 parent cb1bd83777
commit 5b4b8a9065
3 files changed
+143 -5

No files matched your search

+11 -5
View File
@@ -396,15 +396,21 @@ extension CancellableDatabaseAccess: DatabaseCancellable {
}
}
defer {
withLock { state in
return try throwingFirstError {
try work()
} finally: {
let cancelled = withLock { state in
if case .cancelled = state {
db.uncancel()
return true
} else {
state = .expired
return false
}
state = .expired
}
if cancelled {
throw CancellationError()
}
}
return try work()
}
}
+70
View File
@@ -381,6 +381,41 @@ class DatabaseReaderTests : GRDBTestCase {
#endif
}
@available(iOS 13, macOS 10.15, tvOS 13, *)
func test_read_is_cancelled_by_Task_cancellation_performed_after_database_access() async throws {
func test(_ dbReader: some DatabaseReader) async throws {
let semaphore = AsyncSemaphore(value: 0)
let cancelledTaskMutex = Mutex<Task<Void, any Error>?>(nil)
let task = Task {
await semaphore.wait()
try await dbReader.read { db in
try XCTUnwrap(cancelledTaskMutex.load()).cancel()
}
}
cancelledTaskMutex.store(task)
semaphore.signal()
do {
try await task.value
XCTFail("Expected error")
} catch {
XCTAssert(error is CancellationError)
}
// Database access is restored after cancellation (no error is thrown)
try await dbReader.read { db in
try db.execute(sql: "SELECT 0")
}
}
try await test(makeDatabaseQueue())
try await test(makeDatabasePool())
try await test(makeDatabasePool().makeSnapshot())
#if SQLITE_ENABLE_SNAPSHOT || (!GRDBCUSTOMSQLITE && !GRDBCIPHER)
try await test(makeDatabasePool().makeSnapshotPool())
#endif
}
@available(iOS 13, macOS 10.15, tvOS 13, *)
func test_statement_execution_from_read_is_cancelled_by_Task_cancellation_performed_after_database_access() async throws {
func test(_ dbReader: some DatabaseReader) async throws {
@@ -493,6 +528,41 @@ class DatabaseReaderTests : GRDBTestCase {
#endif
}
@available(iOS 13, macOS 10.15, tvOS 13, *)
func test_unsafeRead_is_cancelled_by_Task_cancellation_performed_after_database_access() async throws {
func test(_ dbReader: some DatabaseReader) async throws {
let semaphore = AsyncSemaphore(value: 0)
let cancelledTaskMutex = Mutex<Task<Void, any Error>?>(nil)
let task = Task {
await semaphore.wait()
try await dbReader.unsafeRead { db in
try XCTUnwrap(cancelledTaskMutex.load()).cancel()
}
}
cancelledTaskMutex.store(task)
semaphore.signal()
do {
try await task.value
XCTFail("Expected error")
} catch {
XCTAssert(error is CancellationError)
}
// Database access is restored after cancellation (no error is thrown)
try await dbReader.unsafeRead { db in
try db.execute(sql: "SELECT 0")
}
}
try await test(makeDatabaseQueue())
try await test(makeDatabasePool())
try await test(makeDatabasePool().makeSnapshot())
#if SQLITE_ENABLE_SNAPSHOT || (!GRDBCUSTOMSQLITE && !GRDBCIPHER)
try await test(makeDatabasePool().makeSnapshotPool())
#endif
}
@available(iOS 13, macOS 10.15, tvOS 13, *)
func test_statement_execution_from_unsafeRead_is_cancelled_by_Task_cancellation_performed_after_database_access() async throws {
func test(_ dbReader: some DatabaseReader) async throws {
+62
View File
@@ -460,6 +460,37 @@ class DatabaseWriterTests : GRDBTestCase {
try await test(makeDatabasePool())
}
@available(iOS 13, macOS 10.15, tvOS 13, *)
func test_writeWithoutTransaction_is_cancelled_by_Task_cancellation_performed_after_database_access() async throws {
func test(_ dbWriter: some DatabaseWriter) async throws {
let semaphore = AsyncSemaphore(value: 0)
let cancelledTaskMutex = Mutex<Task<Void, any Error>?>(nil)
let task = Task {
await semaphore.wait()
try await dbWriter.writeWithoutTransaction { db in
try XCTUnwrap(cancelledTaskMutex.load()).cancel()
}
}
cancelledTaskMutex.store(task)
semaphore.signal()
do {
try await task.value
XCTFail("Expected error")
} catch {
XCTAssert(error is CancellationError)
}
// Database access is restored after cancellation (no error is thrown)
try await dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "SELECT 0")
}
}
try await test(makeDatabaseQueue())
try await test(makeDatabasePool())
}
@available(iOS 13, macOS 10.15, tvOS 13, *)
func test_statement_execution_from_writeWithoutTransaction_is_cancelled_by_Task_cancellation_performed_after_database_access() async throws {
func test(_ dbWriter: some DatabaseWriter) async throws {
@@ -691,6 +722,37 @@ class DatabaseWriterTests : GRDBTestCase {
try await test(makeDatabasePool())
}
@available(iOS 13, macOS 10.15, tvOS 13, *)
func test_barrierWriteWithoutTransaction_is_cancelled_by_Task_cancellation_performed_after_database_access() async throws {
func test(_ dbWriter: some DatabaseWriter) async throws {
let semaphore = AsyncSemaphore(value: 0)
let cancelledTaskMutex = Mutex<Task<Void, any Error>?>(nil)
let task = Task {
await semaphore.wait()
try await dbWriter.barrierWriteWithoutTransaction { db in
try XCTUnwrap(cancelledTaskMutex.load()).cancel()
}
}
cancelledTaskMutex.store(task)
semaphore.signal()
do {
try await task.value
XCTFail("Expected error")
} catch {
XCTAssert(error is CancellationError)
}
// Database access is restored after cancellation (no error is thrown)
try await dbWriter.barrierWriteWithoutTransaction { db in
try db.execute(sql: "SELECT 0")
}
}
try await test(makeDatabaseQueue())
try await test(makeDatabasePool())
}
@available(iOS 13, macOS 10.15, tvOS 13, *)
func test_statement_execution_from_barrierWriteWithoutTransaction_is_cancelled_by_Task_cancellation_performed_after_database_access() async throws {
func test(_ dbWriter: some DatabaseWriter) async throws {