Skip to content

[5.5][Collections] testPopulateTargetTrie sometimes gets stuck #3523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable

// Targets have in-memory trie in addition to SQLite FTS as optimization
private let targetTrie = Trie<CollectionPackage>()
private var targetTrieReady = ThreadSafeBox<Bool>()
private var targetTrieReady: Bool?
private let populateTargetTrieLock = Lock()

init(location: SQLite.Location? = nil, configuration: Configuration = .init(), diagnosticsEngine: DiagnosticsEngine? = nil) {
self.location = location ?? .path(localFileSystem.swiftPMCacheDirectory.appending(components: "package-collection.db"))
Expand Down Expand Up @@ -503,7 +504,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
var matchingCollections = Set<Model.CollectionIdentifier>()

// Trie is more performant for target search; use it if available
if self.targetTrieReady.get() ?? false {
if self.populateTargetTrieLock.withLock({ self.targetTrieReady }) ?? false {
do {
switch type {
case .exactMatch:
Expand Down Expand Up @@ -761,13 +762,18 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable

internal func populateTargetTrie(callback: @escaping (Result<Void, Error>) -> Void = { _ in }) {
DispatchQueue.sharedConcurrent.async(group: nil, qos: .background, flags: .assignCurrentContext) {
self.targetTrieReady.memoize {
do {
do {
try self.populateTargetTrieLock.withLock { // Prevent race to populate targetTrie
// Exit early if we've already done the computation before
guard self.targetTrieReady == nil else {
return
}

// since running on low priority thread make sure the database has not already gone away
switch (try self.withStateLock { self.state }) {
case .disconnected, .disconnecting:
callback(.success(()))
return false
self.targetTrieReady = false
return
default:
break
}
Expand Down Expand Up @@ -796,12 +802,11 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
}
}
}
callback(.success(()))
return true
} catch {
callback(.failure(error))
return false
self.targetTrieReady = true
}
callback(.success(()))
} catch {
callback(.failure(error))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2020 Apple Inc. and the Swift project authors
Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -231,10 +231,8 @@ class PackageCollectionsStorageTests: XCTestCase {
do {
try tsc_await { callback in storage2.populateTargetTrie(callback: callback) }

do {
let searchResult = try tsc_await { callback in storage2.searchTargets(query: targetName, type: .exactMatch, callback: callback) }
XCTAssert(searchResult.items.count > 0, "should get results")
}
let searchResult = try tsc_await { callback in storage2.searchTargets(query: targetName, type: .exactMatch, callback: callback) }
XCTAssert(searchResult.items.count > 0, "should get results")
} catch {
// It's possible that some platforms don't have support FTS
XCTAssertEqual(false, storage2.useSearchIndices.get(), "populateTargetTrie should fail only if FTS is not available")
Expand Down