Avoid race condition in assertExclusiveLockPrevention

This commit is contained in:
Gwendal Roué committed 2019-12-01 18:44:23 +01:00
1 parent d85a7883bb
commit 98dc5df039
2 files changed
+27 -9

No files matched your search

+18 -9
View File
@@ -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)
}
}
}
}
+9
View File
@@ -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)
}