From 215128390d01fe08be001e6692e33f36a8e0a8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Rou=C3=A9?= Date: Wed, 23 Jul 2025 11:54:42 +0200 Subject: [PATCH] Fix race condition: prevent cancellation from cancelling other database accesses after database accesses has completed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change, cancelling a database access could cancel *another* database access 😱 --- GRDB/Core/DatabasePool.swift | 1 + GRDB/Core/SerializedDatabase.swift | 1 + 2 files changed, 2 insertions(+) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 9f2623b3d..4c791a1d9 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -803,6 +803,7 @@ extension DatabasePool: DatabaseWriter { try Task.checkCancellation() return try writer.sync { db in defer { + cancelMutex.store(nil) db.uncancel() } cancelMutex.store(db.cancel) diff --git a/GRDB/Core/SerializedDatabase.swift b/GRDB/Core/SerializedDatabase.swift index 25d4776f2..920d279d0 100644 --- a/GRDB/Core/SerializedDatabase.swift +++ b/GRDB/Core/SerializedDatabase.swift @@ -256,6 +256,7 @@ final class SerializedDatabase { return try await withTaskCancellationHandler { try await actor.execute { defer { + cancelMutex.store(nil) db.uncancel() preconditionNoUnsafeTransactionLeft(db) }