@@ -361,9 +361,13 @@ extension DatabasePool: DatabaseReader {
361361 return try await readerPool. get { reader in
362362 try await reader. execute { db in
363363 defer {
364- #warning("TODO: when Task is cancelled, this commit fails, and we're still in a transaction")
365- // Ignore commit error, but make sure we leave the transaction
366- try ? db. commit ( )
364+ // Commit or rollback, but make sure we leave the read-only transaction
365+ // (commit may fail with a CancellationError).
366+ do {
367+ try db. commit ( )
368+ } catch {
369+ try ? db. rollback ( )
370+ }
367371 assert ( !db. isInsideTransaction)
368372 }
369373 // The block isolation comes from the DEFERRED transaction.
@@ -388,8 +392,13 @@ extension DatabasePool: DatabaseReader {
388392 // Second async jump because that's how `Pool.async` has to be used.
389393 reader. async { db in
390394 defer {
391- // Ignore commit error, but make sure we leave the transaction
392- try ? db. commit ( )
395+ // Commit or rollback, but make sure we leave the read-only transaction
396+ // (commit may fail with a CancellationError).
397+ do {
398+ try db. commit ( )
399+ } catch {
400+ try ? db. rollback ( )
401+ }
393402 assert ( !db. isInsideTransaction)
394403 releaseReader ( . reuse)
395404 }
@@ -552,8 +561,13 @@ extension DatabasePool: DatabaseReader {
552561 let ( reader, releaseReader) = try readerPool. get ( )
553562 reader. async { db in
554563 defer {
555- // Ignore commit error, but make sure we leave the transaction
556- try ? db. commit ( )
564+ // Commit or rollback, but make sure we leave the read-only transaction
565+ // (commit may fail with a CancellationError).
566+ do {
567+ try db. commit ( )
568+ } catch {
569+ try ? db. rollback ( )
570+ }
557571 assert ( !db. isInsideTransaction)
558572 releaseReader ( . reuse)
559573 }
0 commit comments