From c159638498c0280a395f6d0e47db2bc00a644669 Mon Sep 17 00:00:00 2001 From: Matt Gallagher Date: Tue, 18 Apr 2023 10:45:08 +1000 Subject: [PATCH] A test demonstrating deadlock when observer count exceeds size of DatabasePool. --- .../Observers/ValueConcurrentObserver.swift | 3 + Tests/GRDBTests/ValueObservationTests.swift | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift b/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift index 87887ffdd..fed3657ea 100644 --- a/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift +++ b/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift @@ -365,6 +365,9 @@ extension ValueConcurrentObserver { } } + // FIXME: this sleep is for demonstration of Issue 1362 only. + Thread.sleep(forTimeInterval: 0.01) + // Start observation self.asyncStartObservation( from: databaseAccess, diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index cf4358472..ae705ed65 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -1086,4 +1086,59 @@ class ValueObservationTests: GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try (DatabaseQueue(path: $0), .immediate) } try Test(test).runAtTemporaryDatabasePath { try (DatabasePool(path: $0), .immediate) } } + + func testIssue1362() async throws { + func test(_ writer: some DatabaseWriter) async throws { + try await writer.write { try $0.execute(sql: "CREATE TABLE s(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + var cancellables = [AnyDatabaseCancellable]() + + let observerCount = 10 + + let receiveExpectation = expectation(description: "all observations received") + receiveExpectation.expectedFulfillmentCount = observerCount * 2 + + let initialExpectation = expectation(description: "initial observations received") + initialExpectation.expectedFulfillmentCount = observerCount + + for _ in 1...observerCount { + let observation = ValueObservation.trackingConstantRegion(Table("s").fetchCount) + let cancellable = observation.start( + in: writer, + scheduling: .async(onQueue: DispatchQueue(label: "", qos: .utility)), + onError: { error in XCTFail("Unexpected error: \(error)") }, + onChange: { value in + receiveExpectation.fulfill() + if value == 0 { + initialExpectation.fulfill() + } + }) + cancellables.append(cancellable) + } + +#if compiler(>=5.8) + await fulfillment(of: [initialExpectation], timeout: 2) +#else + wait(for: [initialExpectation], timeout: 2) +#endif + + Task { + try await writer.write { + try $0.execute(sql: "INSERT INTO s DEFAULT VALUES") + } + } + +#if compiler(>=5.8) + await fulfillment(of: [receiveExpectation], timeout: 5) +#else + wait(for: [receiveExpectation], timeout: 5) +#endif + withExtendedLifetime(cancellables) {} + } + try await AsyncTest(test).runAtTemporaryDatabasePath { + var configuration = Configuration() + configuration.qos = .userInitiated + return try DatabasePool(path: $0, configuration: configuration) + } + } }