diff --git a/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift b/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift index 78cc9352c..a287a3767 100644 --- a/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift +++ b/GRDB/ValueObservation/Observers/ValueConcurrentObserver.swift @@ -865,6 +865,8 @@ extension ValueConcurrentObserver: DatabaseCancellable { // Notify cancellation let (events, dbPool): (ValueObservationEvents?, DatabasePool?) = lock.synchronized { let events = notificationCallbacks?.events + // Set callbacks to nil so that we can't notify anything after + // the cancellation. notificationCallbacks = nil return (events, databaseAccess?.dbPool) } diff --git a/GRDB/ValueObservation/Observers/ValueWriteOnlyObserver.swift b/GRDB/ValueObservation/Observers/ValueWriteOnlyObserver.swift index ea084cce8..e7dc67bda 100644 --- a/GRDB/ValueObservation/Observers/ValueWriteOnlyObserver.swift +++ b/GRDB/ValueObservation/Observers/ValueWriteOnlyObserver.swift @@ -448,6 +448,8 @@ extension ValueWriteOnlyObserver: DatabaseCancellable { // Notify cancellation let (events, writer) = lock.synchronized { let events = notificationCallbacks?.events + // Set callbacks to nil so that we can't notify anything after + // the cancellation. notificationCallbacks = nil return (events, databaseAccess?.writer) } diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index f5c1e4908..952264105 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -748,6 +748,53 @@ class ValueObservationTests: GRDBTestCase { try test(makeDatabasePool()) } + func testIssue1550() throws { + func test(_ writer: some DatabaseWriter) throws { + try writer.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + // Start observing + var counts: [Int] = [] + let cancellable = ValueObservation + .trackingConstantRegion { try Table("t").fetchCount($0) } + .start(in: writer) { error in + XCTFail("Unexpected error: \(error)") + } onChange: { count in + counts.append(count) + } + + // Perform a write after cancellation, but before the + // observation could schedule the removal of its transaction + // observer from the writer dispatch queue. + let semaphore = DispatchSemaphore(value: 0) + writer.asyncWriteWithoutTransaction { db in + semaphore.wait() + do { + try db.execute(sql: "INSERT INTO t(id) VALUES (NULL)") + } catch { + XCTFail("Unexpected error: \(error)") + } + } + cancellable.cancel() + semaphore.signal() + + // Wait until a second write could run, after the observation + // has removed its transaction observer from the writer + // dispatch queue. + let secondWriteExpectation = expectation(description: "cancelled") + writer.asyncWriteWithoutTransaction { _ in + secondWriteExpectation.fulfill() + } + wait(for: [secondWriteExpectation], timeout: 5) + + // We should not have been notified of the first write, because + // it was performed after cancellation. + XCTAssertFalse(counts.contains(1)) + } + + try test(makeDatabaseQueue()) + try test(makeDatabasePool()) + } + func testIssue1209() throws { func test(_ dbWriter: some DatabaseWriter) throws { try dbWriter.write {