diff --git a/CHANGELOG.md b/CHANGELOG.md index 099d8abfb..21510f996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,12 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --- +## 5.22.1 + +Released March 22, 2022 • [diff](https://github.com/groue/GRDB.swift/compare/v5.22.0...v5.22.1) + +- **Fixed** a 5.22.0 regression: [#1194](https://github.com/groue/GRDB.swift/issues/1194) ValueObservation could deadlock when the database is configured with a serial target DispatchQueue. + ## 5.22.0 Released March 22, 2022 • [diff](https://github.com/groue/GRDB.swift/compare/v5.21.0...v5.22.0) diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index bcdb819dd..56c10c4ce 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '5.22.0' + s.version = '5.22.1' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A toolkit for SQLite databases, with a focus on application development.' diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index d8eeee059..ed3d47455 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -316,4 +316,17 @@ public struct Configuration { return DispatchQueue(label: label, qos: qos) } } + + /// Creates a DispatchQueue which has the quality of service of + /// read accesses. + /// + /// The returned queue has no target queue, and won't create deadlocks when + /// used synchronously from a database access. + func makeDispatchQueue(label: String) -> DispatchQueue { + if let targetQueue = targetQueue { + return DispatchQueue(label: label, qos: targetQueue.qos) + } else { + return DispatchQueue(label: label, qos: qos) + } + } } diff --git a/GRDB/ValueObservation/ValueConcurrentObserver.swift b/GRDB/ValueObservation/ValueConcurrentObserver.swift index cea80f6fe..cc829eb83 100644 --- a/GRDB/ValueObservation/ValueConcurrentObserver.swift +++ b/GRDB/ValueObservation/ValueConcurrentObserver.swift @@ -138,7 +138,7 @@ final class ValueConcurrentObserver { fetch: reducer._fetch) self.notificationCallbacks = NotificationCallbacks(events: events, onChange: onChange) self.reducer = reducer - self.reduceQueue = dbPool.configuration.makeReaderDispatchQueue(label: dbPool.configuration.identifier( + self.reduceQueue = dbPool.configuration.makeDispatchQueue(label: dbPool.configuration.identifier( defaultLabel: "GRDB", purpose: "ValueObservation")) } diff --git a/GRDB/ValueObservation/ValueWriteOnlyObserver.swift b/GRDB/ValueObservation/ValueWriteOnlyObserver.swift index 9e07516ff..2856c0b01 100644 --- a/GRDB/ValueObservation/ValueWriteOnlyObserver.swift +++ b/GRDB/ValueObservation/ValueWriteOnlyObserver.swift @@ -139,7 +139,7 @@ final class ValueWriteOnlyObserverCFBundlePackageType FMWK CFBundleShortVersionString - 5.22.0 + 5.22.1 CFBundleSignature ???? CFBundleVersion diff --git a/Tests/GRDBCombineTests/ValueObservationPublisherTests.swift b/Tests/GRDBCombineTests/ValueObservationPublisherTests.swift index 755febc62..a30ed393a 100644 --- a/Tests/GRDBCombineTests/ValueObservationPublisherTests.swift +++ b/Tests/GRDBCombineTests/ValueObservationPublisherTests.swift @@ -423,5 +423,53 @@ class ValueObservationPublisherTests : XCTestCase { .runAtTemporaryDatabasePath { try setUp(DatabaseQueue(path: $0)) } .runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) } } + + // MARK: - Regression Tests + + /// Regression test for https://github.com/groue/GRDB.swift/issues/1194 + func testIssue1194() throws { + guard #available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *) else { + throw XCTSkip("Combine is not available") + } + + struct Record: Codable, FetchableRecord, PersistableRecord { + var id: Int64 + } + + var configuration = Configuration() + configuration.targetQueue = DispatchQueue(label: "crash.test", qos: .userInitiated) + + let database = DatabaseQueue(configuration: configuration) + + var migrator = DatabaseMigrator() + migrator.registerMigration("v1") { (db) in + try db.create(table: Record.databaseTableName) { (t) in + t.autoIncrementedPrimaryKey("id") + } + } + + try migrator.migrate(database) + + let observation = ValueObservation.tracking { (db) in + try Record.fetchCount(db) + } + + let exp = expectation(description: "") + let cancellable = observation.publisher(in: database, scheduling: .immediate) + .map { _ in + database.readPublisher { (db) in + try Record.fetchCount(db) + } + } + .switchToLatest() + .sink(receiveCompletion: { _ in }, + receiveValue: { (value) in + exp.fulfill() + }) + + withExtendedLifetime(cancellable) { + waitForExpectations(timeout: 1) + } + } } #endif