diff --git a/AUDIT.md b/AUDIT.md index d4bcd1b..79154c1 100644 --- a/AUDIT.md +++ b/AUDIT.md @@ -45,7 +45,7 @@ Read-only audit at commit 92fe81b (tag 0.9). `swift test` passes: 170 tests. Fin 27. ✅ FIXED for Box and pCloud (branch `safety-pass`, package traits `Box` / `PCloud`; ZIPFoundation in core still open) **Box and pCloud SDKs are in every consumer's dependency graph**, because they are top-level package dependencies. Core LLVS depends on ZIPFoundation for one file. 28. ✅ FIXED (untracked) Root `Package.resolved` is tracked, but `.gitignore:4` ignores `**/Package.resolved`. 29. ✅ FIXED (renamed to `LICENSE`, with an "MIT License" title) `LICENCE.txt` uses the British spelling; GitHub and Swift Package Index licence detection may miss it. -30. Swift 5 language mode everywhere. Swift 6 blockers: global `log` (non-Sendable, shadows Foundation `log()`), `History` escapes from `queryHistory`, non-Sendable `Store` captured in `@Sendable` closures, `Zone`/`MergeArbiter`/`DynamicTaskBatcher` not Sendable. +30. ✅ FIXED (every target is in Swift 6 language mode; the named blockers are all resolved) Swift 5 language mode everywhere. Swift 6 blockers: global `log` (non-Sendable, shadows Foundation `log()`), `History` escapes from `queryHistory`, non-Sendable `Store` captured in `@Sendable` closures, `Zone`/`MergeArbiter`/`DynamicTaskBatcher` not Sendable. ## Minor / dead code @@ -87,4 +87,5 @@ Open: - Zipping a live SQLite database in the middle of a transaction can capture a hot journal or a torn file. - `Store.init` derives `values/`, `versions/` and `maps/` from the unresolved root URL, while `rootDirectoryURL` is resolved. No failure seen. - `WebDAVResponseParser` ignores the namespace URI. Turning on `shouldProcessNamespaces` and checking for `DAV:` would be stricter. +- `Store` is `@unchecked Sendable`, and vouches transitively for whatever `Storage` the caller supplies. The protocol now documents that implementations must be thread-safe, but nothing enforces it. diff --git a/CHANGELOG.md b/CHANGELOG.md index f61f73a..cfd07c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Changelog +## Unreleased + +### Changed + +- Every target builds in Swift 6 language mode, with strict concurrency checking. The public value types (`Version`, `Value`, `Value.Change`, `ZoneReference`, `SnapshotManifest`, `SnapshotPolicy` and their nested types) are `Sendable`, and so are the `Exchange` and `Zone` protocols. A custom `Exchange` or `Zone` in your own code must now be `Sendable` too. `Cache` gained a `ValueType: Sendable` constraint, and its methods take `some Hashable & Sendable` instead of `AnyHashable`. `SQLiteDatabase.Error.bindingFailed` carries a `valueDescription: String` instead of an `Any?` value, so that the error is `Sendable`. +- `Log.level` can be set from any thread. +- The `@Atomic` property wrapper is renamed to `@Guarded`, because the standard library now has its own `Atomic`. + +### Fixed + +- `CloudKitExchange` had two real data races that strict concurrency brought to light: records were collected from its query callbacks without a lock, and its cached restoration state was read, changed and written back in separate steps, so concurrent callbacks could lose version IDs. Its `store` property is now a `let`, and its temporary directory is no longer a `lazy var`, which is not thread-safe. +- `BoxExchange` used a `lazy var` for its temporary directory. + ## 0.10.0 (2026-09-18) ### Source breaking diff --git a/CLAUDE.md b/CLAUDE.md index f35e233..c622cb9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -19,7 +19,7 @@ swift test --filter storeCreatesDirectories # Run a single test by name - Tests are **Swift Testing** (`@Suite`/`@Test`/`#expect`), not XCTest. Test names have no `test` prefix. - Tests are in `Tests/LLVSTests/` and `Tests/LLVSModelTests/`, and depend on `LLVS`, `LLVSSQLite`, `LLVSModel`, and `LLVSWebDAV` (for its XML parser only). The other cloud backend targets have no tests. - SPM builds ALL targets before tests run. A broken backend target blocks the whole test suite. After a change to `LLVSBox` or `LLVSPCloud`, build with `--enable-all-traits`, or the change is not compiled at all. -- `swift-tools-version: 6.1`, but every target except the macro uses `.swiftLanguageMode(.v5)`. Platforms: macOS 15, iOS 18, watchOS 11 (needed for `Mutex` from `Synchronization`). +- `swift-tools-version: 6.1`, and every target builds in Swift 6 language mode (strict concurrency). Platforms: macOS 15, iOS 18, watchOS 11 (needed for `Mutex` from `Synchronization`). - CI is `.github/workflows/ci.yml`: `swift test`, then `swift build --enable-all-traits`. There is no linter. Sample apps (`Samples/LoCo`, `Samples/TheMessage`) are Xcode projects that reference the package locally; they are not part of the SPM package. - `docs/` is a 2019 Jekyll blog that teaches a removed Combine API. It may be served by GitHub Pages, so it has been left in place. Do not trust it for API. Record user-visible changes in `CHANGELOG.md`. diff --git a/Package.swift b/Package.swift index 166ff86..9a631d8 100644 --- a/Package.swift +++ b/Package.swift @@ -62,33 +62,33 @@ let package = Package( dependencies: [ .product(name: "ZIPFoundation", package: "ZIPFoundation"), ], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .testTarget( name: "LLVSTests", dependencies: ["LLVS", "LLVSSQLite", "LLVSWebDAV"], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .target( name: "LLVSCloudKit", dependencies: ["LLVS"], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .target( name: "LLVSSQLite", dependencies: ["LLVS", "SQLite3"], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .target( name: "LLVSPCloud", dependencies: [ "LLVS", .product(name: "PCloudSDKSwift", package: "pcloud-sdk-swift", condition: .when(traits: ["PCloud"])) ], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .target( name: "LLVSBox", dependencies: [ "LLVS", .product(name: "BoxSDK", package: "box-ios-sdk", condition: .when(traits: ["Box"])) ], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .macro( name: "LLVSModelMacros", dependencies: [ @@ -101,19 +101,19 @@ let package = Package( "LLVS", "LLVSModelMacros", ], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .target( name: "LLVSWebDAV", dependencies: ["LLVS"], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .target( name: "LLVSGoogleDrive", dependencies: ["LLVS"], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .target( name: "LLVSOneDrive", dependencies: ["LLVS"], - swiftSettings: [.swiftLanguageMode(.v5)]), + swiftSettings: [.swiftLanguageMode(.v6)]), .testTarget( name: "LLVSModelTests", dependencies: [ @@ -121,6 +121,6 @@ let package = Package( "LLVS", "LLVSSQLite", ], - swiftSettings: [.swiftLanguageMode(.v5)]) + swiftSettings: [.swiftLanguageMode(.v6)]) ] ) diff --git a/Sources/LLVS/Core/Exchange.swift b/Sources/LLVS/Core/Exchange.swift index a1b7bfe..3af11a9 100644 --- a/Sources/LLVS/Core/Exchange.swift +++ b/Sources/LLVS/Core/Exchange.swift @@ -22,7 +22,7 @@ public protocol SnapshotExchange { func sendSnapshot(manifest: SnapshotManifest, chunkProvider: @escaping @Sendable (Int) throws -> Data) async throws } -public protocol Exchange: AnyObject { +public protocol Exchange: AnyObject, Sendable { var newVersionsAvailable: AsyncStream { get } diff --git a/Sources/LLVS/Core/FileZone.swift b/Sources/LLVS/Core/FileZone.swift index 4a918d1..8342bcd 100644 --- a/Sources/LLVS/Core/FileZone.swift +++ b/Sources/LLVS/Core/FileZone.swift @@ -28,7 +28,8 @@ public class FileStorage: Storage, SnapshotCapable { } -internal final class FileZone: Zone { +/// Each zone owns its `FileManager`, which is the documented way to use it from more than one thread. +internal final class FileZone: Zone, @unchecked Sendable { let rootDirectory: URL let fileExtension: String diff --git a/Sources/LLVS/Core/Map.swift b/Sources/LLVS/Core/Map.swift index 3244664..9c6ea2d 100644 --- a/Sources/LLVS/Core/Map.swift +++ b/Sources/LLVS/Core/Map.swift @@ -352,7 +352,7 @@ final class Map { extension Map { - struct Key: Codable, Hashable { + struct Key: Codable, Hashable, Sendable { var keyString: String init(_ keyString: String = UUID().uuidString) { self.keyString = keyString @@ -365,7 +365,7 @@ extension Map { var valueFork: Value.Fork } - struct KeyValuePair: Codable, Hashable { + struct KeyValuePair: Codable, Hashable, Sendable { var key: Key var valueReference: Value.Reference } @@ -380,7 +380,7 @@ extension Map { } } - struct Node: Codable, Hashable { + struct Node: Codable, Hashable, Sendable { var reference: ZoneReference var children: Children } diff --git a/Sources/LLVS/Core/Snapshot.swift b/Sources/LLVS/Core/Snapshot.swift index 3bd809c..a032139 100644 --- a/Sources/LLVS/Core/Snapshot.swift +++ b/Sources/LLVS/Core/Snapshot.swift @@ -8,7 +8,7 @@ import Foundation /// Metadata describing a snapshot stored in the cloud. -public struct SnapshotManifest: Codable { +public struct SnapshotManifest: Codable, Sendable { public var snapshotId: String public var format: String public var createdAt: Date @@ -33,7 +33,7 @@ public struct SnapshotManifest: Codable { } /// Policy controlling automatic snapshot creation after sync. -public struct SnapshotPolicy { +public struct SnapshotPolicy: Sendable { public var enabled: Bool public var minimumInterval: TimeInterval public var minimumNewVersions: Int diff --git a/Sources/LLVS/Core/Storage.swift b/Sources/LLVS/Core/Storage.swift index 762277e..120d99f 100644 --- a/Sources/LLVS/Core/Storage.swift +++ b/Sources/LLVS/Core/Storage.swift @@ -12,6 +12,7 @@ public enum MapType { case userDefined(label: String) } +/// Implementations must be safe to use from more than one thread: `Store` vouches for its storage. public protocol Storage { func makeValuesZone(in store: Store) throws -> Zone diff --git a/Sources/LLVS/Core/Store.swift b/Sources/LLVS/Core/Store.swift index 9b37d41..b3d6cc4 100644 --- a/Sources/LLVS/Core/Store.swift +++ b/Sources/LLVS/Core/Store.swift @@ -11,7 +11,7 @@ import Synchronization // MARK:- Branch -public struct Branch: RawRepresentable { +public struct Branch: RawRepresentable, Sendable { public let rawValue: String public init(rawValue: String) { @@ -27,7 +27,7 @@ public struct Branch: RawRepresentable { // MARK:- Store -public final class Store { +public final class Store: @unchecked Sendable { public enum Error: Swift.Error { case missingVersion diff --git a/Sources/LLVS/Core/Value.swift b/Sources/LLVS/Core/Value.swift index 1626671..7d8e6ef 100644 --- a/Sources/LLVS/Core/Value.swift +++ b/Sources/LLVS/Core/Value.swift @@ -7,7 +7,7 @@ import Foundation -public struct Value: Codable, Identifiable { +public struct Value: Codable, Identifiable, Sendable { public typealias ID = Identifier public var id: ID @@ -49,12 +49,12 @@ public struct Value: Codable, Identifiable { public extension Value { - struct Reference: Codable, Hashable { + struct Reference: Codable, Hashable, Sendable { public var valueId: ID public var storedVersionId: Version.ID } - struct Identifier: RawRepresentable, Hashable, Codable { + struct Identifier: RawRepresentable, Hashable, Codable, Sendable { public var rawValue: String public init(rawValue: String = UUID().uuidString) { @@ -66,7 +66,7 @@ public extension Value { } } - enum Change: Codable { + enum Change: Codable, Sendable { case insert(Value) case update(Value) case remove(ID) @@ -118,8 +118,8 @@ public extension Value { } } - enum Fork: Equatable { - public enum Branch: Equatable { + enum Fork: Equatable, Sendable { + public enum Branch: Equatable, Sendable { case first case second diff --git a/Sources/LLVS/Core/Version.swift b/Sources/LLVS/Core/Version.swift index cced641..510d9e7 100644 --- a/Sources/LLVS/Core/Version.swift +++ b/Sources/LLVS/Core/Version.swift @@ -7,18 +7,18 @@ import Foundation -public struct Version: Hashable, Identifiable { +public struct Version: Hashable, Identifiable, Sendable { public typealias Metadata = [MetadataKey:MetadataValue] - public struct MetadataKey: Hashable, Codable, RawRepresentable { + public struct MetadataKey: Hashable, Codable, RawRepresentable, Sendable { public var rawValue: String public init(rawValue: String) { self.rawValue = rawValue } public static let branch = Self(rawValue: "__llvs_branch") } - public struct MetadataValue: Codable { + public struct MetadataValue: Codable, Sendable { public let data: Data public init(data: Data) { self.data = data } public init(_ value: T) { self.data = try! JSONEncoder().encode(value) } @@ -87,7 +87,7 @@ extension Version: Codable { extension Version { - public struct Identifier: RawRepresentable, Codable, Hashable { + public struct Identifier: RawRepresentable, Codable, Hashable, Sendable { public var rawValue: String public init(rawValue: String = UUID().uuidString) { @@ -99,7 +99,7 @@ extension Version { } } - public struct Predecessors: Codable, Hashable { + public struct Predecessors: Codable, Hashable, Sendable { public internal(set) var idOfFirst: ID public internal(set) var idOfSecond: ID? public var ids: [ID] { @@ -114,7 +114,7 @@ extension Version { } } - public struct Successors: Codable, Hashable { + public struct Successors: Codable, Hashable, Sendable { public internal(set) var ids: Set internal init(ids: Set = []) { self.ids = ids diff --git a/Sources/LLVS/Core/Zone.swift b/Sources/LLVS/Core/Zone.swift index 6e7a8d6..3b6b20a 100644 --- a/Sources/LLVS/Core/Zone.swift +++ b/Sources/LLVS/Core/Zone.swift @@ -7,12 +7,12 @@ import Foundation -public struct ZoneReference: Codable, Hashable { +public struct ZoneReference: Codable, Hashable, Sendable { public var key: String public var version: Version.ID } -public protocol Zone { +public protocol Zone: Sendable { func store(_ data: Data, for reference: ZoneReference) throws // Default provided, but zone implementations can optimize this. diff --git a/Sources/LLVS/Exchanges/FileSystemExchange.swift b/Sources/LLVS/Exchanges/FileSystemExchange.swift index 5a4757f..ebd9149 100644 --- a/Sources/LLVS/Exchanges/FileSystemExchange.swift +++ b/Sources/LLVS/Exchanges/FileSystemExchange.swift @@ -7,7 +7,7 @@ import Foundation -public class FileSystemExchange: NSObject, Exchange, NSFilePresenter, SnapshotExchange { +public final class FileSystemExchange: NSObject, Exchange, NSFilePresenter, SnapshotExchange, @unchecked Sendable { public enum Error: Swift.Error { case versionFileInvalid @@ -117,7 +117,8 @@ public class FileSystemExchange: NSObject, Exchange, NSFilePresenter, SnapshotEx case read, write } - private func coordinateFileAccess(_ access: FileAccess, by block: @escaping () throws -> T) async throws -> T { + // The block runs on the operation queue, so it and its result must be safe to hand across threads. + private func coordinateFileAccess(_ access: FileAccess, by block: @escaping @Sendable () throws -> T) async throws -> T { try await withCheckedThrowingContinuation { continuation in queue.addOperation { if self.usesFileCoordination { diff --git a/Sources/LLVS/Exchanges/MultipeerExchange.swift b/Sources/LLVS/Exchanges/MultipeerExchange.swift index 98c2af1..c2b2192 100644 --- a/Sources/LLVS/Exchanges/MultipeerExchange.swift +++ b/Sources/LLVS/Exchanges/MultipeerExchange.swift @@ -21,7 +21,8 @@ public protocol PeerTransport: AnyObject { /// /// Each peer runs its own MultipeerExchange instance. One peer's `send()`/`retrieve()` /// sends requests to the other peer, which responds automatically via `handleRequest`. -public class MultipeerExchange: Exchange { +/// All of its own state is immutable, or lives in the `State` actor. +public final class MultipeerExchange: Exchange, @unchecked Sendable { public enum Error: Swift.Error { case transportUnavailable @@ -32,7 +33,7 @@ public class MultipeerExchange: Exchange { // MARK: - PeerMessage - struct PeerMessage: Codable { + struct PeerMessage: Codable, Sendable { let id: String let type: MessageType var requestId: String? diff --git a/Sources/LLVS/General/Cache.swift b/Sources/LLVS/General/Cache.swift index a445d24..16eaa33 100644 --- a/Sources/LLVS/General/Cache.swift +++ b/Sources/LLVS/General/Cache.swift @@ -11,24 +11,27 @@ import Synchronization /// Generational cache. Fills up each generation to a limit, then discards oldest creating a new generation. /// When you retrieve a value, it automatically adds that value to the newest generation, to keep it around. /// Creating generations is based on the number of values in the latest generation, not on time or data size. -public final class Cache { +public final class Cache: Sendable { - private class Generation { - private var valuesByIdentifier: [AnyHashable:ValueType] = [:] + /// A type-erased key. Like `AnyHashable`, but it carries the promise that the key is `Sendable`. + private struct Key: Hashable, @unchecked Sendable { + private let base: AnyHashable + init(_ base: some Hashable & Sendable) { self.base = AnyHashable(base) } + } - subscript(id: AnyHashable) -> ValueType? { - get { - return valuesByIdentifier[id] - } - set(newValue) { - valuesByIdentifier[id] = newValue - } + // A struct, so that the compiler can see that nothing escapes the lock + private struct Generation: Sendable { + private var valuesByIdentifier: [Key:ValueType] = [:] + + subscript(id: Key) -> ValueType? { + get { valuesByIdentifier[id] } + set { valuesByIdentifier[id] = newValue } } var count: Int { return valuesByIdentifier.count } } - private struct State { + private struct State: Sendable { var generations: [Generation] } @@ -44,30 +47,26 @@ public final class Cache { self.state = Mutex(State(generations: generations)) } - public func setValue(_ value: ValueType, for identifier: AnyHashable) { + public func setValue(_ value: ValueType, for identifier: some Hashable & Sendable) { state.withLock { state in regenerateIfNeeded(&state) - state.generations.first![identifier] = value + state.generations[0][Key(identifier)] = value } } - public func removeValue(for identifier: AnyHashable) { + public func removeValue(for identifier: some Hashable & Sendable) { state.withLock { state in - state.generations.forEach { generation in - generation[identifier] = nil + for i in state.generations.indices { + state.generations[i][Key(identifier)] = nil } } } - public func value(for identifier: AnyHashable) -> ValueType? { + public func value(for identifier: some Hashable & Sendable) -> ValueType? { state.withLock { state in - if let generation = state.generations.first(where: { $0[identifier] != nil }) { - let value = generation[identifier] - state.generations.first![identifier] = value // Keep current by adding to most recent generation - return value - } else { - return nil - } + guard let value = state.generations.lazy.compactMap({ $0[Key(identifier)] }).first else { return nil } + state.generations[0][Key(identifier)] = value // Keep current by adding to most recent generation + return value } } @@ -78,8 +77,7 @@ public final class Cache { } private func regenerateIfNeeded(_ state: inout State) { - let generation = state.generations.first! - if generation.count > regenerationLimit { + if state.generations[0].count > regenerationLimit { regenerate(&state) } } diff --git a/Sources/LLVS/General/General.swift b/Sources/LLVS/General/General.swift index fdc0c72..96b67ae 100644 --- a/Sources/LLVS/General/General.swift +++ b/Sources/LLVS/General/General.swift @@ -43,7 +43,7 @@ public extension ClosedRange where Bound == Int { } @propertyWrapper -public struct Atomic { +public struct Guarded: Sendable { private final class Storage: @unchecked Sendable { let mutex: Mutex @@ -52,6 +52,11 @@ public struct Atomic { private let storage: Storage + /// Read, change and write the value in one locked step. + public func withLock(_ body: (inout Value) throws -> Result) rethrows -> Result { + try storage.mutex.withLock { try body(&$0) } + } + public init(wrappedValue value: Value) { self.storage = Storage(value) } diff --git a/Sources/LLVS/General/Log.swift b/Sources/LLVS/General/Log.swift index 26917fd..9c25918 100644 --- a/Sources/LLVS/General/Log.swift +++ b/Sources/LLVS/General/Log.swift @@ -7,12 +7,14 @@ import Foundation import os +import Synchronization public let log = Log() -public class Log { +/// The shared logger. `level` can be set from any thread, and is `.none` until you raise it. +public final class Log: Sendable { - public enum Level : Int, Comparable { + public enum Level : Int, Comparable, Sendable { case none case error case warning @@ -35,7 +37,11 @@ public class Log { } } - public var level = Level.none + public var level: Level { + get { _level.withLock { $0 } } + set { _level.withLock { $0 = newValue } } + } + private let _level = Mutex(Level.none) @inline(__always) public final func verbose(_ messageClosure: @autoclosure () -> String, path: StaticString = #file, function: StaticString = #function, line: Int = #line) { if level >= .verbose { diff --git a/Sources/LLVSBox/BoxExchange.swift b/Sources/LLVSBox/BoxExchange.swift index 4d291dd..d6256b4 100644 --- a/Sources/LLVSBox/BoxExchange.swift +++ b/Sources/LLVSBox/BoxExchange.swift @@ -19,7 +19,7 @@ import BoxSdkGen /// Uses `BoxClient` from the official Box SDK for all API operations. /// The caller provides an authenticated `BoxClient` (e.g. via `BoxDeveloperTokenAuth` /// or `BoxCCGAuth`). -public class BoxExchange: FolderBasedExchange { +public final class BoxExchange: FolderBasedExchange, @unchecked Sendable { public typealias FileID = String public typealias FolderID = String @@ -37,7 +37,7 @@ public class BoxExchange: FolderBasedExchange { /// The Box folder ID that serves as the root for LLVS data. public let rootFolderID: String - @Atomic private var restoration = RestorationInfo() + @Guarded private var restoration = RestorationInfo() public let newVersionsAvailable: AsyncStream private let newVersionsContinuation: AsyncStream.Continuation @@ -51,7 +51,8 @@ public class BoxExchange: FolderBasedExchange { } } - fileprivate lazy var temporaryDirectory: URL = { + /// Not lazy: a lazy var is not thread-safe, and this is touched from callback queues. + fileprivate let temporaryDirectory: URL = { let result = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try? FileManager.default.createDirectory(at: result, withIntermediateDirectories: true, attributes: nil) return result diff --git a/Sources/LLVSCloudKit/CloudKitExchange.swift b/Sources/LLVSCloudKit/CloudKitExchange.swift index 9c5dc9b..be9cc0c 100644 --- a/Sources/LLVSCloudKit/CloudKitExchange.swift +++ b/Sources/LLVSCloudKit/CloudKitExchange.swift @@ -9,7 +9,7 @@ import Foundation import CloudKit import LLVS -public class CloudKitExchange: Exchange { +public final class CloudKitExchange: Exchange, @unchecked Sendable { public enum CloudDatabaseDescription { case privateDatabaseWithCustomZone(CKContainer, zoneIdentifier: String) @@ -49,14 +49,15 @@ public class CloudKitExchange: Exchange { case snapshotChunkAssetMissing(Int) } - fileprivate lazy var temporaryDirectory: URL = { + /// Not lazy: a lazy var is not thread-safe, and this is touched from callback queues. + fileprivate let temporaryDirectory: URL = { let result = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try? FileManager.default.createDirectory(at: result, withIntermediateDirectories: true, attributes: nil) return result }() /// The store the exchange is updating. - public var store: Store + public let store: Store /// Client to inform of updates public let newVersionsAvailable: AsyncStream @@ -84,7 +85,7 @@ public class CloudKitExchange: Exchange { } /// Restoration state - @Atomic private var restoration: Restoration = .init() + @Guarded private var restoration: Restoration = .init() /// Limit to use for CloudKit fetches. Should be less than actual limit (ie 400) private let cloudKitFetchLimit = 200 @@ -153,7 +154,7 @@ fileprivate extension CloudKitExchange { operation.fetchAllChanges = true operation.recordChangedBlock = { record in let versionId = Version.ID(record.recordID.recordName) - self.restoration.versionsInCloud.insert(versionId) + self._restoration.withLock { $0.versionsInCloud.insert(versionId) } log.verbose("Found record for version: \(versionId)") } operation.recordZoneFetchCompletionBlock = { zoneID, token, clientData, moreComing, error in @@ -167,8 +168,10 @@ fileprivate extension CloudKitExchange { let tokenExpired = cloudError?.code == .changeTokenExpired || cloudError?.partialErrorsByItemID?.values.contains { ($0 as? CKError)?.code == .changeTokenExpired } == true if !isRetryAfterTokenReset, tokenExpired { - self.restoration.fetchRecordChangesToken = nil - self.restoration.versionsInCloud = [] + self._restoration.withLock { + $0.fetchRecordChangesToken = nil + $0.versionsInCloud = [] + } log.error("iCloud token expired. Cleared cached data") // Retry once. A second failure (eg zone not found, rate limited) is thrown to the caller. Task { @@ -222,9 +225,9 @@ fileprivate extension CloudKitExchange { do { let records = try await queryDatabase(with: .query(query)) let versionIds = records.map { Version.ID($0.recordID.recordName) } - self.restoration.versionsInCloud.formUnion(versionIds) + self._restoration.withLock { $0.versionsInCloud.formUnion(versionIds) } let modificationDates = records.map { $0.modificationDate! } - self.restoration.lastQueryDate = max(self.restoration.lastQueryDate ?? Date.distantPast, modificationDates.max() ?? Date.distantPast ) + self._restoration.withLock { $0.lastQueryDate = max($0.lastQueryDate ?? Date.distantPast, modificationDates.max() ?? Date.distantPast) } } catch let error as CKError where error.code == .unknownItem { // Probably don't have data in cloud yet. Ignore error self.restoration.lastQueryDate = Date.distantPast @@ -237,16 +240,17 @@ fileprivate extension CloudKitExchange { return try await withCheckedThrowingContinuation { continuation in let operation = queryInfo.makeQueryOperation() - var records: [CKRecord] = [] + // CloudKit calls these blocks on its own queue, so the records are collected under a lock + let records = Guarded<[CKRecord]>(wrappedValue: []) operation.recordFetchedBlock = { record in - records.append(record) + records.withLock { $0.append(record) } } operation.queryCompletionBlock = { cursor, error in if let cursor = cursor { Task { do { let moreRecords = try await self.queryDatabase(with: .cursor(cursor)) - continuation.resume(returning: records + moreRecords) + continuation.resume(returning: records.wrappedValue + moreRecords) } catch { continuation.resume(throwing: error) } @@ -257,7 +261,7 @@ fileprivate extension CloudKitExchange { log.error("Failed to fetch new versions: \(error)") continuation.resume(throwing: error) } else { - continuation.resume(returning: records) + continuation.resume(returning: records.wrappedValue) } } } diff --git a/Sources/LLVSPCloud/PCloudExchange.swift b/Sources/LLVSPCloud/PCloudExchange.swift index 8bd9041..be352b8 100644 --- a/Sources/LLVSPCloud/PCloudExchange.swift +++ b/Sources/LLVSPCloud/PCloudExchange.swift @@ -18,7 +18,7 @@ import PCloudSDKSwift /// /// Uses `PCloudClient` from the official pCloud SDK for all API operations. /// Folder IDs are cached via `restorationState` to avoid repeated lookups. -public class PCloudExchange: FolderBasedExchange { +public final class PCloudExchange: FolderBasedExchange, @unchecked Sendable { public typealias FileID = UInt64 public typealias FolderID = UInt64 @@ -42,7 +42,7 @@ public class PCloudExchange: FolderBasedExchange { /// URL session used for downloading file data from pCloud CDN links. private let urlSession: URLSession - @Atomic private var restoration = RestorationInfo() + @Guarded private var restoration = RestorationInfo() public let newVersionsAvailable: AsyncStream private let newVersionsContinuation: AsyncStream.Continuation diff --git a/Sources/LLVSSQLite/SQLiteDatabase.swift b/Sources/LLVSSQLite/SQLiteDatabase.swift index c1c63f3..a488ba6 100644 --- a/Sources/LLVSSQLite/SQLiteDatabase.swift +++ b/Sources/LLVSSQLite/SQLiteDatabase.swift @@ -27,7 +27,8 @@ public final class SQLiteDatabase { case openFailed(code: Int32) case closeFailed(code: Int32) case statementFailed(statement: String, code: Int32) - case bindingFailed(bindingIndex: Int, value: Any?, code: Int32) + /// The value is described, rather than carried, so that the error can cross threads. + case bindingFailed(bindingIndex: Int, valueDescription: String, code: Int32) case queryFailed(query: String, code: Int32) } @@ -176,7 +177,7 @@ public final class SQLiteDatabase { } guard code == SQLITE_OK else { - throw Error.bindingFailed(bindingIndex: i+1, value: value, code: code) + throw Error.bindingFailed(bindingIndex: i+1, valueDescription: String(describing: value), code: code) } } } diff --git a/Sources/LLVSSQLite/SQLiteZone.swift b/Sources/LLVSSQLite/SQLiteZone.swift index bf1f629..d5c489a 100644 --- a/Sources/LLVSSQLite/SQLiteZone.swift +++ b/Sources/LLVSSQLite/SQLiteZone.swift @@ -30,7 +30,8 @@ public class SQLiteStorage: Storage, SnapshotCapable { } -internal final class SQLiteZone: Zone { +/// Not thread-safe by design. The caller serialises access. See `SQLiteDatabase`. +internal final class SQLiteZone: Zone, @unchecked Sendable { let rootDirectory: URL let fileExtension: String diff --git a/Tests/LLVSTests/DynamicTaskBatcherTests.swift b/Tests/LLVSTests/DynamicTaskBatcherTests.swift index 3e60ee1..433dfee 100644 --- a/Tests/LLVSTests/DynamicTaskBatcherTests.swift +++ b/Tests/LLVSTests/DynamicTaskBatcherTests.swift @@ -18,9 +18,9 @@ import Foundation } @Test func failure() async throws { - var count = 0 + let counter = Counter() let batcher = DynamicTaskBatcher(numberOfTasks: 10, taskCostEvaluator: { _ in 0.1 }) { range in - count += 1 + counter.increment() return .definitive(.failure(TestError.testError)) } @@ -30,7 +30,7 @@ import Foundation } catch { // Expected } - #expect(count == 1) + #expect(counter.value == 1) } @Test func zeroTasks() async throws { @@ -41,55 +41,55 @@ import Foundation } @Test func oneTask() async throws { - var count = 0 + let counter = Counter() let batcher = DynamicTaskBatcher(numberOfTasks: 1, taskCostEvaluator: { _ in 0.1 }) { range in - count += 1 + counter.increment() #expect(range == 0..<1) return .definitive(.success(())) } try await batcher.start() - #expect(count == 1) + #expect(counter.value == 1) } @Test func oneLargeTask() async throws { - var count = 0 + let counter = Counter() let batcher = DynamicTaskBatcher(numberOfTasks: 1, taskCostEvaluator: { _ in 2.0 }) { range in - count += 1 + counter.increment() #expect(range == 0..<1) return .definitive(.success(())) } try await batcher.start() - #expect(count == 1) + #expect(counter.value == 1) } @Test func twoSmallTasks() async throws { - var count = 0 + let counter = Counter() let batcher = DynamicTaskBatcher(numberOfTasks: 2, taskCostEvaluator: { _ in 0.1 }) { range in - count += 1 + counter.increment() #expect(range == 0..<2) return .definitive(.success(())) } try await batcher.start() - #expect(count == 1) + #expect(counter.value == 1) } @Test func twoLargeTasks() async throws { - var count = 0 + let counter = Counter() let batcher = DynamicTaskBatcher(numberOfTasks: 2, taskCostEvaluator: { _ in 1.0 }) { range in - count += 1 + counter.increment() #expect(range.count == 1) return .definitive(.success(())) } try await batcher.start() - #expect(count == 2) + #expect(counter.value == 2) } @Test func accumulatingCost() async throws { - var count = 0 + let counter = Counter() let batcher = DynamicTaskBatcher(numberOfTasks: 4, taskCostEvaluator: { index in switch index { case 0, 1: @@ -102,7 +102,7 @@ import Foundation return 0.1 } }) { range in - count += 1 + counter.increment() if range.lowerBound == 0 { #expect(range.count == 1) } else if range.lowerBound == 1 { @@ -114,13 +114,13 @@ import Foundation } try await batcher.start() - #expect(count == 3) + #expect(counter.value == 3) } @Test func growingAndRepeatingBatchesUntilFail() async throws { - var count = 0 + let counter = Counter() let batcher = DynamicTaskBatcher(numberOfTasks: 2, taskCostEvaluator: { _ in 1.01 }) { range in - count += 1 + counter.increment() #expect(range.lowerBound == 0) return .growBatchAndReexecute } @@ -131,13 +131,13 @@ import Foundation } catch { // Expected } - #expect(count == 2) + #expect(counter.value == 2) } @Test func growingAndRepeatingBatchesWithSuccess() async throws { - var count = 0 + let counter = Counter() let batcher = DynamicTaskBatcher(numberOfTasks: 3, taskCostEvaluator: { _ in 1.01 }) { range in - count += 1 + counter.increment() switch range { case 0..<1: return .growBatchAndReexecute @@ -152,6 +152,14 @@ import Foundation } try await batcher.start() - #expect(count == 3) + #expect(counter.value == 3) } } + +/// The batcher can call its closure from more than one task, so the test counts under a lock. +private final class Counter: @unchecked Sendable { + private let lock = NSLock() + private var count = 0 + var value: Int { lock.withLock { count } } + func increment() { lock.withLock { count += 1 } } +} diff --git a/Tests/LLVSTests/ExchangeRobustnessTests.swift b/Tests/LLVSTests/ExchangeRobustnessTests.swift index 44f55f4..ddf3c0c 100644 --- a/Tests/LLVSTests/ExchangeRobustnessTests.swift +++ b/Tests/LLVSTests/ExchangeRobustnessTests.swift @@ -3,7 +3,8 @@ import Foundation @testable import LLVS /// An exchange whose remote returns version metadata, but the wrong value changes. -private final class FaultyChangesExchange: Exchange { +/// Unchecked only because the tests drive it from one thread. Do not copy this into a real exchange. +private final class FaultyChangesExchange: Exchange, @unchecked Sendable { let store: Store let remoteVersions: [Version] var restorationState: Data? @@ -26,7 +27,7 @@ private final class FaultyChangesExchange: Exchange { } /// An exchange with an empty remote, which records the order in which versions are sent. -private final class SendRecordingExchange: Exchange { +private final class SendRecordingExchange: Exchange, @unchecked Sendable { let store: Store var restorationState: Data? let newVersionsAvailable: AsyncStream = AsyncStream { $0.finish() } diff --git a/Tests/LLVSTests/FileSystemExchangeTests.swift b/Tests/LLVSTests/FileSystemExchangeTests.swift index 529f8c3..3fff22d 100644 --- a/Tests/LLVSTests/FileSystemExchangeTests.swift +++ b/Tests/LLVSTests/FileSystemExchangeTests.swift @@ -147,8 +147,9 @@ import Foundation let _ = try store1.makeVersion(basedOnPredecessor: nil, storing: []) // Start listening before the send + let listeningExchange = exchange2 let notificationTask = Task { - for await _ in exchange2.newVersionsAvailable { + for await _ in listeningExchange.newVersionsAvailable { return // Got a notification } } diff --git a/Tests/LLVSTests/MemoryExchangeTests.swift b/Tests/LLVSTests/MemoryExchangeTests.swift index f47b530..6281a96 100644 --- a/Tests/LLVSTests/MemoryExchangeTests.swift +++ b/Tests/LLVSTests/MemoryExchangeTests.swift @@ -131,8 +131,9 @@ import Foundation _ = try store1.makeVersion(basedOnPredecessor: nil, storing: []) // Start a task to listen for the notification + let listeningExchange = exchange1 let notified = Task { - for await _ in exchange1.newVersionsAvailable { + for await _ in listeningExchange.newVersionsAvailable { return true } return false diff --git a/Tests/LLVSTests/MultipeerExchangeTests.swift b/Tests/LLVSTests/MultipeerExchangeTests.swift index dbb7158..1d31841 100644 --- a/Tests/LLVSTests/MultipeerExchangeTests.swift +++ b/Tests/LLVSTests/MultipeerExchangeTests.swift @@ -203,8 +203,9 @@ class MockPeerTransport: PeerTransport { let ver = try store1.makeVersion(basedOnPredecessor: nil, storing: [.insert(val)]) // Listen for new versions on exchange2 + let listeningExchange = exchange2 let notified = Task { - for await _ in exchange2.newVersionsAvailable { + for await _ in listeningExchange.newVersionsAvailable { return true } return false diff --git a/Tests/LLVSTests/SnapshotTests.swift b/Tests/LLVSTests/SnapshotTests.swift index aeec0ba..6a7d64d 100644 --- a/Tests/LLVSTests/SnapshotTests.swift +++ b/Tests/LLVSTests/SnapshotTests.swift @@ -576,15 +576,14 @@ import Foundation let replacerExchange = FileSystemExchange(rootDirectoryURL: exchangeURL, store: store2, usesFileCoordination: false) - var bootstrapError: Swift.Error? - - // Race bootstrap and snapshot replacement - await withTaskGroup(of: Void.self) { group in + // Race bootstrap and snapshot replacement. The group reports whether the bootstrap failed. + let bootstrapFailed = await withTaskGroup(of: Bool.self) { group -> Bool in group.addTask { do { try await coordinator3.bootstrapFromSnapshot() + return false } catch { - bootstrapError = error + return true } } group.addTask { @@ -592,11 +591,13 @@ import Foundation let chunkFile = snapshotDir2.appendingPathComponent(String(format: "chunk-%03d", index)) return try Data(contentsOf: chunkFile) }) + return false } + return await group.reduce(false) { $0 || $1 } } // The key assertion: no crash, and the store is in a consistent state. - if bootstrapError != nil { + if bootstrapFailed { // Bootstrap failed due to race — this is the expected graceful recovery. var versionCount = 0 coordinator3.store.queryHistory { history in