From 7bb5c636e7dd286fe57fda163d4bf10244892867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 30 Dec 2025 10:36:01 +0100 Subject: [PATCH] DatabaseWriter.barrierWriteWithoutTransaction accepts a sending closure --- GRDB/Core/DatabasePool.swift | 40 +++++++++++++++++++--------------- GRDB/Core/DatabaseQueue.swift | 2 +- GRDB/Core/DatabaseWriter.swift | 4 ++-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 2f0e116f4..0110b73c6 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -814,32 +814,38 @@ extension DatabasePool: DatabaseWriter { } public func barrierWriteWithoutTransaction( - _ updates: @Sendable (Database) throws -> T + _ updates: sending (Database) throws -> T ) async throws -> T { guard let readerPool else { throw DatabaseError.connectionIsClosed() } - // Pool.barrier does not support async calls (yet?). - // So we perform cancellation checks just as in - // the async version of SerializedDatabase.execute(). - let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil) - return try await withTaskCancellationHandler { - try Task.checkCancellation() - return try await readerPool.barrier { + // Compiler does not see that updates can be sent. + return try await withoutActuallyEscaping(updates) { updates in + typealias SendableClosure = @Sendable (Database) throws -> T + let updates = unsafeBitCast(updates, to: SendableClosure.self) + + // Pool.barrier does not support async calls (yet?). + // So we perform cancellation checks just as in + // the async version of SerializedDatabase.execute(). + let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil) + return try await withTaskCancellationHandler { try Task.checkCancellation() - return try writer.sync { db in - defer { - cancelMutex.store(nil) - db.uncancel() - } - cancelMutex.store(db.cancel) + return try await readerPool.barrier { try Task.checkCancellation() - return try updates(db) + return try writer.sync { db in + defer { + cancelMutex.store(nil) + db.uncancel() + } + cancelMutex.store(db.cancel) + try Task.checkCancellation() + return try updates(db) + } } + } onCancel: { + cancelMutex.withLock { $0?() } } - } onCancel: { - cancelMutex.withLock { $0?() } } } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index ea5978af4..f6580c45c 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -408,7 +408,7 @@ extension DatabaseQueue: DatabaseWriter { } public func barrierWriteWithoutTransaction( - _ updates: @Sendable (Database) throws -> T + _ updates: sending (Database) throws -> T ) async throws -> T { try await writer.execute(updates) } diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index 73e5c6625..0b39ebfa3 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -213,7 +213,7 @@ public protocol DatabaseWriter: DatabaseReader { /// database access, or the error thrown by `updates`, or /// `CancellationError` if the task is cancelled. func barrierWriteWithoutTransaction( - _ updates: @Sendable (Database) throws -> T + _ updates: sending (Database) throws -> T ) async throws -> T /// Schedules database operations for execution, and returns immediately. @@ -947,7 +947,7 @@ extension AnyDatabaseWriter: DatabaseWriter { } public func barrierWriteWithoutTransaction( - _ updates: @Sendable (Database) throws -> T + _ updates: sending (Database) throws -> T ) async throws -> T { try await base.barrierWriteWithoutTransaction(updates) }