mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Avoid race condition in assertExclusiveLockPrevention
This commit is contained in:
1 parent
d85a7883bb
commit
98dc5df039
2 files changed
+27
-9
No files matched your search
@@ -717,15 +717,24 @@ extension Database {
|
||||
}
|
||||
|
||||
func assertExclusiveLockPrevention(from statement: Statement) throws {
|
||||
if preventsExclusiveLock.value {
|
||||
// Attempt at releasing eventual exclusive lock.
|
||||
// See Database.startPreventingExclusiveLock()
|
||||
try? rollback()
|
||||
throw DatabaseError(
|
||||
resultCode: .SQLITE_ABORT,
|
||||
message: "Can't acquire exclusive lock",
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
SchedulingWatchdog.assertValidQueue(self)
|
||||
|
||||
try preventsExclusiveLock.read { preventsExclusiveLock in
|
||||
if preventsExclusiveLock {
|
||||
// Attempt at releasing eventual exclusive lock with ROLLBACk,
|
||||
// as explained in Database.startPreventingExclusiveLock().
|
||||
//
|
||||
// We don't use `try? rollback()` because we're not in the
|
||||
// protected dispatch queue here due to
|
||||
// `preventsExclusiveLock.read`.
|
||||
_ = sqlite3_exec(sqliteConnection, "ROLLBACK", nil, nil, nil)
|
||||
|
||||
throw DatabaseError(
|
||||
resultCode: .SQLITE_ABORT,
|
||||
message: "Can't acquire exclusive lock",
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,15 @@ final class SchedulingWatchdog {
|
||||
GRDBPrecondition(current?.allows(db) ?? false, message(), file: file, line: line)
|
||||
}
|
||||
|
||||
static func assertValidQueue(
|
||||
_ db: Database,
|
||||
_ message: @autoclosure() -> String = "Database was not used on the correct thread.",
|
||||
file: StaticString = #file,
|
||||
line: UInt = #line)
|
||||
{
|
||||
assert(current?.allows(db) ?? false, message(), file: file, line: line)
|
||||
}
|
||||
|
||||
static var current: SchedulingWatchdog? {
|
||||
return DispatchQueue.getSpecific(key: watchDogKey)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user