Skip to content

Commit ababf36

Browse files
authored
[Collections] Throw error instead of preconditionFailure after storage closed (#3513)
Motivation: With #3505, the code does `preconditionFailure` when trying to execute DB statement after connection is disconnected. In `populateTargetTrie` we check connection state before calling `executeStatement`, but it's still possible for storage to close _after_ the check but _before_ `executeStatement`. This might have caused rdar://78513692. Modifications: - Throw error instead of `preconditionFailure` - Unrelated: `GitHubPackageMetadataProvider.configuration` doesn't need to be var
1 parent 9ca70ec commit ababf36

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

Sources/PackageCollections/Providers/GitHubPackageMetadataProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
2323

2424
public var name: String = "GitHub"
2525

26-
var configuration: Configuration
26+
let configuration: Configuration
2727

2828
private let httpClient: HTTPClient
2929
private let diagnosticsEngine: DiagnosticsEngine?

Sources/PackageCollections/Storage/SQLitePackageCollectionsStorage.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
760760
}
761761

762762
internal func populateTargetTrie(callback: @escaping (Result<Void, Error>) -> Void = { _ in }) {
763-
DispatchQueue.sharedConcurrent.async(group: nil, qos: .background, flags: .assignCurrentContext, execute: {
763+
DispatchQueue.sharedConcurrent.async(group: nil, qos: .background, flags: .assignCurrentContext) {
764764
self.targetTrieReady.memoize {
765765
do {
766766
// since running on low priority thread make sure the database has not already gone away
@@ -771,6 +771,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
771771
default:
772772
break
773773
}
774+
774775
// Use FTS to build the trie
775776
let query = "SELECT collection_id_blob_base64, package_repository_url, name FROM \(Self.targetsFTSName);"
776777
try self.executeStatement(query) { statement in
@@ -802,7 +803,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
802803
return false
803804
}
804805
}
805-
})
806+
}
806807
}
807808

808809
// for testing
@@ -842,7 +843,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
842843
let db: SQLite
843844
switch (self.location, self.state) {
844845
case (_, .disconnecting), (_, .disconnected):
845-
preconditionFailure("DB id disconnecting or disconnected")
846+
throw StringError("DB is disconnecting or disconnected")
846847
case (.path(let path), .connected(let database)):
847848
if self.fileSystem.exists(path) {
848849
db = database

Tests/PackageCollectionsTests/GitHubPackageMetadataProviderTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
265265
httpClient.configuration.retryStrategy = .none
266266
var configuration = GitHubPackageMetadataProvider.Configuration()
267267
configuration.cacheDir = tmpPath
268-
var provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
269-
provider.configuration.authTokens = { authTokens }
268+
configuration.authTokens = { authTokens }
269+
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
270270
defer { XCTAssertNoThrow(try provider.close()) }
271271

272272
let reference = PackageReference(repository: RepositorySpecifier(url: repoURL))

0 commit comments

Comments
 (0)