Skip to content

fix race condition when trie generation thread may be invoked after storage has been closed #3505

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 1 commit into from
May 19, 2021
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 @@ -763,6 +763,14 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
DispatchQueue.sharedConcurrent.async(group: nil, qos: .background, flags: .assignCurrentContext, execute: {
self.targetTrieReady.memoize {
do {
// 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
default:
break
}
// Use FTS to build the trie
let query = "SELECT collection_id_blob_base64, package_repository_url, name FROM \(Self.targetsFTSName);"
try self.executeStatement(query) { statement in
Expand Down Expand Up @@ -833,6 +841,8 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
let db = try self.withStateLock { () -> SQLite in
let db: SQLite
switch (self.location, self.state) {
case (_, .disconnecting), (_, .disconnected):
preconditionFailure("DB id disconnecting or disconnected")
case (.path(let path), .connected(let database)):
if self.fileSystem.exists(path) {
db = database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,17 @@ class PackageCollectionsStorageTests: XCTestCase {
}

try storage.close()
storage.resetCache()

XCTAssertTrue(storage.fileSystem.exists(storagePath), "expected file to exist at \(path)")
try storage.fileSystem.writeFileContents(storagePath, bytes: ByteString("blah".utf8))

XCTAssertThrowsError(try tsc_await { callback in storage.get(identifier: mockCollections.first!.identifier, callback: callback) }, "expected error", { error in
let storage2 = SQLitePackageCollectionsStorage(path: path)
defer { XCTAssertNoThrow(try storage2.close()) }
XCTAssertThrowsError(try tsc_await { callback in storage2.get(identifier: mockCollections.first!.identifier, callback: callback) }, "expected error", { error in
XCTAssert("\(error)".contains("is not a database"), "Expected file is not a database error")
})

XCTAssertThrowsError(try tsc_await { callback in storage.put(collection: mockCollections.first!, callback: callback) }, "expected error", { error in
XCTAssertThrowsError(try tsc_await { callback in storage2.put(collection: mockCollections.first!, callback: callback) }, "expected error", { error in
XCTAssert("\(error)".contains("is not a database"), "Expected file is not a database error")
})
}
Expand Down