From cbbd0cb94529326ff109d40da1c8e970907d6039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 19 Jul 2025 11:41:29 +0200 Subject: [PATCH] Add asserts about expected ends of transaction --- GRDB/Core/DatabasePool.swift | 12 +++++++++--- GRDB/Core/DatabaseQueue.swift | 6 ++++-- GRDB/Core/DatabaseSnapshot.swift | 2 ++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 1cc88fcde..cddf3f610 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -361,7 +361,9 @@ extension DatabasePool: DatabaseReader { return try await readerPool.get { reader in try await reader.execute { db in defer { - try? db.commit() // Ignore commit error + // Ignore commit error, but make sure we leave the transaction + try? db.commit() + assert(!db.isInsideTransaction) } // The block isolation comes from the DEFERRED transaction. try db.beginTransaction(.deferred) @@ -385,7 +387,9 @@ extension DatabasePool: DatabaseReader { // Second async jump because that's how `Pool.async` has to be used. reader.async { db in defer { - try? db.commit() // Ignore commit error + // Ignore commit error, but make sure we leave the transaction + try? db.commit() + assert(!db.isInsideTransaction) releaseReader(.reuse) } do { @@ -547,7 +551,9 @@ extension DatabasePool: DatabaseReader { let (reader, releaseReader) = try readerPool.get() reader.async { db in defer { - try? db.commit() // Ignore commit error + // Ignore commit error, but make sure we leave the transaction + try? db.commit() + assert(!db.isInsideTransaction) releaseReader(.reuse) } do { diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 37b62fde5..dad9a492d 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -248,8 +248,9 @@ extension DatabaseQueue: DatabaseReader { ) { writer.async { db in defer { - // Ignore error because we can not notify it. + // Ignore commit error (we can not notify it), but make sure we leave the transaction try? db.commit() + assert(!db.isInsideTransaction) try? db.endReadOnly() } @@ -296,8 +297,9 @@ extension DatabaseQueue: DatabaseReader { GRDBPrecondition(!db.isInsideTransaction, "must not be called from inside a transaction.") defer { - // Ignore error because we can not notify it. + // Ignore commit error (we can not notify it), but make sure we leave the transaction try? db.commit() + assert(!db.isInsideTransaction) try? db.endReadOnly() } diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 7609a3433..f980edf77 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -113,7 +113,9 @@ public final class DatabaseSnapshot { deinit { // Leave snapshot isolation reader.reentrantSync { db in + // Ignore commit error (we can not notify it), but make sure we leave the transaction try? db.commit() + assert(!db.isInsideTransaction) } }