mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
Protect PRAGMA query_only = 0 against SQLITE_INTERRUPT
This should fix a race condition about cancellation that happens during a read-only access.
This commit is contained in:
1 parent
5dba64d545
commit
60ea10948b
1 file changed
+19
-1
@@ -863,7 +863,25 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
readOnlyDepth -= 1
|
||||
assert(readOnlyDepth >= 0, "unbalanced endReadOnly()")
|
||||
if readOnlyDepth == 0 {
|
||||
try internalCachedStatement(sql: "PRAGMA query_only = 0").execute()
|
||||
do {
|
||||
try internalCachedStatement(sql: "PRAGMA query_only = 0").execute()
|
||||
} catch is CancellationError, DatabaseError.SQLITE_INTERRUPT {
|
||||
// Note: no test could take this path. Maybe `PRAGMA query_only` is
|
||||
// not sensitive to SQLite interrupt. But if it is, this code is necessary.
|
||||
//
|
||||
// Maybe we were unlucky, and user has interrupted the database
|
||||
// during the PRAGMA. CancellationError is thrown when
|
||||
// the PRAGMA is interrupted during execution. SQLITE_INTERRUPT
|
||||
// is thrown when the PRAGMA is interrupted during compilation.
|
||||
//
|
||||
// Let's run it again. This is a correct behavior, because:
|
||||
// 1. Since the interrupt was concurrent, we can reorder it
|
||||
// and pretend it occurred before or after the PRAGMA.
|
||||
// 2. If we do not PRAGMA, we might leave a database
|
||||
// transaction in the read-only state, with no other
|
||||
// opportunity to quit it.
|
||||
try internalCachedStatement(sql: "PRAGMA query_only = 0").execute()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user