mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
⚠️ StrictConcurrency: fix warnings about the transfer of non-sendable values
We use unsafe constructs that we'll be able to replace with transferring parameters when the feature ships in the language: https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426/5
This commit is contained in:
1 parent
87fb90ef97
commit
135de3c56e
5 files changed
+78
-13
No files matched your search
@@ -625,11 +625,13 @@ extension DatabaseReader {
|
||||
let result = dbResult.flatMap { db in
|
||||
Result { try observation.fetchInitialValue(db) }
|
||||
}
|
||||
// Safe because result is not used beyond its transfer to sheduler
|
||||
let resultWrapper = UncheckedSendableWrapper(value: result)
|
||||
|
||||
scheduler.schedule {
|
||||
if cancellable.isCancelled { return }
|
||||
do {
|
||||
try onChange(result.get())
|
||||
try onChange(resultWrapper.value.get())
|
||||
} catch {
|
||||
observation.events.didFail?(error)
|
||||
}
|
||||
|
||||
@@ -359,7 +359,10 @@ extension ValueConcurrentObserver {
|
||||
// `DatabasePool.asyncWALSnapshotTransaction` has to be used.
|
||||
initialFetchTransaction.asyncRead { dbResult in
|
||||
do {
|
||||
let fetchedValue: Reducer.Fetched
|
||||
// Safe because fetchedValue is not used beyond its transfer to reduceQueue
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let fetchedValue: Reducer.Fetched
|
||||
|
||||
let initialRegion: DatabaseRegion
|
||||
let db = try dbResult.get()
|
||||
|
||||
@@ -383,9 +386,13 @@ extension ValueConcurrentObserver {
|
||||
guard isNotifying else { return /* Cancelled */ }
|
||||
|
||||
do {
|
||||
guard let initialValue = try self.reducer._value(fetchedValue) else {
|
||||
guard let _initialValue = try self.reducer._value(fetchedValue) else {
|
||||
fatalError("Broken contract: reducer has no initial value")
|
||||
}
|
||||
|
||||
// Safe because initialValue is not used beyond its transfer to scheduler
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let initialValue = _initialValue
|
||||
|
||||
// Notify
|
||||
self.scheduler.schedule {
|
||||
@@ -466,7 +473,9 @@ extension ValueConcurrentObserver {
|
||||
events.databaseDidChange?()
|
||||
|
||||
// Fetch
|
||||
let fetchedValue: Reducer.Fetched
|
||||
// Safe because fetchedValue is not used beyond its transfer to reduceQueue
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let fetchedValue: Reducer.Fetched
|
||||
|
||||
switch self.trackingMode {
|
||||
case .constantRegion:
|
||||
@@ -497,6 +506,10 @@ extension ValueConcurrentObserver {
|
||||
|
||||
// Notify
|
||||
if let value {
|
||||
// Safe because value is not used beyond its transfer to scheduler
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let value = value
|
||||
|
||||
self.scheduler.schedule {
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
@@ -588,7 +601,10 @@ extension ValueConcurrentObserver {
|
||||
|
||||
do {
|
||||
// Fetch
|
||||
let fetchedValue: Reducer.Fetched
|
||||
// Safe because fetchedValue is not used beyond its transfer to reduceQueue
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let fetchedValue: Reducer.Fetched
|
||||
|
||||
let initialRegion: DatabaseRegion
|
||||
let db = try dbResult.get()
|
||||
switch self.trackingMode {
|
||||
@@ -611,10 +627,14 @@ extension ValueConcurrentObserver {
|
||||
guard isNotifying else { return /* Cancelled */ }
|
||||
|
||||
do {
|
||||
guard let initialValue = try self.reducer._value(fetchedValue) else {
|
||||
guard let _initialValue = try self.reducer._value(fetchedValue) else {
|
||||
fatalError("Broken contract: reducer has no initial value")
|
||||
}
|
||||
|
||||
// Safe because initialValue is not used beyond its transfer to scheduler
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let initialValue = _initialValue
|
||||
|
||||
// Notify
|
||||
self.scheduler.schedule {
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
@@ -648,7 +668,10 @@ extension ValueConcurrentObserver {
|
||||
do {
|
||||
try writerDB.isolated(readOnly: true) {
|
||||
// Fetch
|
||||
let fetchedValue: Reducer.Fetched
|
||||
// Safe because fetchedValue is not used beyond its transfer to reduceQueue
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let fetchedValue: Reducer.Fetched
|
||||
|
||||
let observedRegion: DatabaseRegion
|
||||
switch self.trackingMode {
|
||||
case .constantRegion:
|
||||
@@ -680,6 +703,10 @@ extension ValueConcurrentObserver {
|
||||
|
||||
// Notify
|
||||
if let value {
|
||||
// Safe because value is not used beyond its transfer to scheduler
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let value = value
|
||||
|
||||
self.scheduler.schedule {
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
@@ -828,6 +855,10 @@ extension ValueConcurrentObserver: TransactionObserver {
|
||||
}
|
||||
|
||||
private func reduce(_ fetchResult: Result<Reducer.Fetched, Error>) {
|
||||
// Safe because fetchedValue is not used beyond its transfer to reduceQueue
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let fetchResult = fetchResult
|
||||
|
||||
reduceQueue.async {
|
||||
do {
|
||||
let fetchedValue = try fetchResult.get()
|
||||
@@ -839,6 +870,10 @@ extension ValueConcurrentObserver: TransactionObserver {
|
||||
|
||||
// Notify value
|
||||
if let value {
|
||||
// Safe because value is not used beyond its transfer to scheduler
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let value = value
|
||||
|
||||
self.scheduler.schedule {
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
|
||||
@@ -254,10 +254,14 @@ extension ValueWriteOnlyObserver {
|
||||
writer.asyncWriteWithoutTransaction { db in
|
||||
do {
|
||||
// Fetch & Start observing the database
|
||||
guard let fetchedValue = try self.fetchAndStartObservation(db) else {
|
||||
guard let _fetchedValue = try self.fetchAndStartObservation(db) else {
|
||||
return /* Cancelled */
|
||||
}
|
||||
|
||||
// Safe because fetchedValue is not used beyond its transfer to reduceQueue
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let fetchedValue = _fetchedValue
|
||||
|
||||
// Reduce
|
||||
//
|
||||
// Reducing is performed asynchronously, so that we do not lock
|
||||
@@ -270,10 +274,14 @@ extension ValueWriteOnlyObserver {
|
||||
guard isNotifying else { return /* Cancelled */ }
|
||||
|
||||
do {
|
||||
guard let initialValue = try self.reducer._value(fetchedValue) else {
|
||||
guard let _initialValue = try self.reducer._value(fetchedValue) else {
|
||||
fatalError("Broken contract: reducer has no initial value")
|
||||
}
|
||||
|
||||
// Safe because initialValue is not used beyond its transfer to scheduler
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let initialValue = _initialValue
|
||||
|
||||
// Notify
|
||||
self.scheduler.schedule {
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
@@ -382,7 +390,9 @@ extension ValueWriteOnlyObserver: TransactionObserver {
|
||||
|
||||
do {
|
||||
// Fetch
|
||||
let fetchedValue: Reducer.Fetched
|
||||
// Safe because fetchedValue is not used beyond its transfer to reduceQueue
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let fetchedValue: Reducer.Fetched
|
||||
|
||||
switch trackingMode {
|
||||
case .constantRegion, .constantRegionRecordedFromSelection:
|
||||
@@ -417,6 +427,10 @@ extension ValueWriteOnlyObserver: TransactionObserver {
|
||||
|
||||
// Notify value
|
||||
if let value {
|
||||
// Safe because value is not used beyond its transfer to scheduler
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let value = value
|
||||
|
||||
self.scheduler.schedule {
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
|
||||
@@ -8,7 +8,16 @@ extension ValueObservation {
|
||||
public func removeDuplicates(by predicate: @escaping (Reducer.Value, Reducer.Value) -> Bool)
|
||||
-> ValueObservation<ValueReducers.RemoveDuplicates<Reducer>>
|
||||
{
|
||||
mapReducer { ValueReducers.RemoveDuplicates($0, predicate: predicate) }
|
||||
// This is safe-ish, because predicate will only be used serially,
|
||||
// in the reducer queue of ValueObservation observers.
|
||||
//
|
||||
// Yet we can't guarantee that it won't be used concurrently.
|
||||
//
|
||||
// But if we don't accept a non-sendable closure, we can't deal
|
||||
// with Equatable.==... YOLO!!!
|
||||
nonisolated(unsafe) let predicate = predicate
|
||||
|
||||
return mapReducer { ValueReducers.RemoveDuplicates($0, predicate: predicate) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -262,10 +262,15 @@ public final class SharedValueObservation<Element>: @unchecked Sendable {
|
||||
{ [weak self] element in
|
||||
self?.handleChange(element)
|
||||
})
|
||||
} else if let result = lastResult {
|
||||
} else if let lastResult {
|
||||
// Notify last result as an initial value
|
||||
|
||||
// Safe because result is not used beyond its transfer to sheduler
|
||||
// FIXME: improve when SE-0430 is shipped.
|
||||
nonisolated(unsafe) let lastResult = lastResult
|
||||
|
||||
scheduler.scheduleInitial {
|
||||
switch result {
|
||||
switch lastResult {
|
||||
case let .failure(error):
|
||||
onError(error)
|
||||
case let .success(value):
|
||||
|
||||
Reference in new issue
Block a user