From adc16ea7dc887777f596aa0ca8fa7d6d0a9ece41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 15 Aug 2021 13:02:00 +0200 Subject: [PATCH] Protect mutating TextOutputStream with a lock This change addresses https://github.com/groue/GRDB.swift/pull/1025#issuecomment-898894618 --- CHANGELOG.md | 1 + GRDB/ValueObservation/ValueObservation.swift | 29 +++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29caf77a6..cd6c2c397 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -81,6 +81,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - **Fixed**: [#980](https://github.com/groue/GRDB.swift/pull/980) by [@jroselightricks](https://github.com/jroselightricks): Fix spelling - **Fixed**: [#989](https://github.com/groue/GRDB.swift/pull/989) by [@pp5x](https://github.com/pp5x): FTS: add support of ifNotExists in synchronize() - **Fixed**: [#999](https://github.com/groue/GRDB.swift/issues/999): `request(for: association)` no longer crashes when the foreign key contains a NULL value. +- **Fixed**: Fix thread unsafety in `ValueObservation.print()` - **New**: The `selectID()` method is now available for [Identifiable Records](README.md#identifiable-records) - **New**: Cached prepared statements can profit from [SQL Interpolation](Documentation/SQLInterpolation.md): diff --git a/GRDB/ValueObservation/ValueObservation.swift b/GRDB/ValueObservation/ValueObservation.swift index b600b379b..86e6f89e5 100644 --- a/GRDB/ValueObservation/ValueObservation.swift +++ b/GRDB/ValueObservation/ValueObservation.swift @@ -172,16 +172,31 @@ extension ValueObservation: Refinable { to stream: TextOutputStream? = nil) -> ValueObservation> { + let lock = NSLock() let prefix = prefix.isEmpty ? "" : "\(prefix): " var stream = stream ?? PrintOutputStream() return handleEvents( - willStart: { stream.write("\(prefix)start") }, - willFetch: { stream.write("\(prefix)fetch") }, - willTrackRegion: { stream.write("\(prefix)tracked region: \($0)") }, - databaseDidChange: { stream.write("\(prefix)database did change") }, - didReceiveValue: { stream.write("\(prefix)value: \($0)") }, - didFail: { stream.write("\(prefix)failure: \($0)") }, - didCancel: { stream.write("\(prefix)cancel") }) + willStart: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)start") }, + willFetch: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)fetch") }, + willTrackRegion: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)tracked region: \($0)") }, + databaseDidChange: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)database did change") }, + didReceiveValue: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)value: \($0)") }, + didFail: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)failure: \($0)") }, + didCancel: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)cancel") }) } // MARK: - Fetching Values