diff --git a/CHANGES.md b/CHANGES.md index ce1efa3076..31306a4487 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,10 @@ +## Changes in 0.22.3 (2022-02-24) + +🐛 Bugfixes + +- Thread Safety: Replace all objc_sync_enter/exit methods with recursive locks. ([#5675](https://github.com/vector-im/element-ios/issues/5675)) + + ## Changes in 0.22.2 (2022-02-22) 🙌 Improvements diff --git a/MatrixSDK.podspec b/MatrixSDK.podspec index fb91ceec70..c041f151c7 100644 --- a/MatrixSDK.podspec +++ b/MatrixSDK.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "MatrixSDK" - s.version = "0.22.2" + s.version = "0.22.3" s.summary = "The iOS SDK to build apps compatible with Matrix (https://www.matrix.org)" s.description = <<-DESC diff --git a/MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift b/MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift index c96025967d..0dc5a28af9 100644 --- a/MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift +++ b/MatrixSDK/Data/EventTimeline/Thread/MXThreadEventTimeline.swift @@ -55,6 +55,7 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline { /// The current pending request private var currentHttpOperation: MXHTTPOperation? + private let lockListeners = NSRecursiveLock() private lazy var threadEventFilter: MXRoomEventFilter = { let filter = MXRoomEventFilter() @@ -439,9 +440,9 @@ public class MXThreadEventTimeline: NSObject, MXEventTimeline { /// Thread safe access to listeners array private func synchronizeListeners(_ block: () -> Void) { - objc_sync_enter(listeners) + lockListeners.lock() + defer { lockListeners.unlock() } block() - objc_sync_exit(listeners) } private func fixRoomId(inEvents events: [MXEvent]) { diff --git a/MatrixSDK/MatrixSDKVersion.m b/MatrixSDK/MatrixSDKVersion.m index 59fea862a0..2e34709006 100644 --- a/MatrixSDK/MatrixSDKVersion.m +++ b/MatrixSDK/MatrixSDKVersion.m @@ -16,4 +16,4 @@ #import -NSString *const MatrixSDKVersion = @"0.22.2"; +NSString *const MatrixSDKVersion = @"0.22.3"; diff --git a/MatrixSDK/Threads/MXThreadingService.swift b/MatrixSDK/Threads/MXThreadingService.swift index 499e7a4db2..b98f649218 100644 --- a/MatrixSDK/Threads/MXThreadingService.swift +++ b/MatrixSDK/Threads/MXThreadingService.swift @@ -46,6 +46,7 @@ public class MXThreadingService: NSObject { private weak var session: MXSession? + private let lockThreads = NSRecursiveLock() private var threads: [String: MXThread] = [:] private let multicastDelegate: MXMulticastDelegate = MXMulticastDelegate() @@ -110,10 +111,9 @@ public class MXThreadingService: NSObject { /// - Parameter identifier: identifier of a thread /// - Returns: thread instance if found, nil otherwise public func thread(withId identifier: String) -> MXThread? { - objc_sync_enter(threads) - let result = threads[identifier] - objc_sync_exit(threads) - return result + lockThreads.lock() + defer { lockThreads.unlock() } + return threads[identifier] } public func createTempThread(withId identifier: String, roomId: String) -> MXThread { @@ -308,9 +308,9 @@ public class MXThreadingService: NSObject { } private func saveThread(_ thread: MXThread) { - objc_sync_enter(threads) + lockThreads.lock() + defer { lockThreads.unlock() } threads[thread.id] = thread - objc_sync_exit(threads) } // MARK: - Delegate diff --git a/MatrixSDK/Utils/MXMulticastDelegate.swift b/MatrixSDK/Utils/MXMulticastDelegate.swift index 875ec31720..46fad221ee 100644 --- a/MatrixSDK/Utils/MXMulticastDelegate.swift +++ b/MatrixSDK/Utils/MXMulticastDelegate.swift @@ -22,6 +22,7 @@ public class MXMulticastDelegate { /// Weakly referenced delegates private let delegates: NSHashTable = NSHashTable.weakObjects() private let dispatchQueue: DispatchQueue + private let lockDelegates = NSRecursiveLock() /// Initializer /// - Parameter dispatchQueue: Queue to invoke delegate methods @@ -70,9 +71,9 @@ public class MXMulticastDelegate { /// Thread safe access to delegates array private func synchronizeDelegates(_ block: () -> Void) { - objc_sync_enter(delegates) + lockDelegates.lock() + defer { lockDelegates.unlock() } block() - objc_sync_exit(delegates) } deinit {