From e1f38f3761d4faa335ad4354448df127b8a638c5 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Thu, 13 Mar 2025 09:47:20 +1100 Subject: [PATCH] Wrap the `getValue` logic in a `performMap` to ensure thread safety --- .../Dependency Injection/Dependencies.swift | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/SessionUtilitiesKit/Dependency Injection/Dependencies.swift b/SessionUtilitiesKit/Dependency Injection/Dependencies.swift index 47810b082..906aabb5a 100644 --- a/SessionUtilitiesKit/Dependency Injection/Dependencies.swift +++ b/SessionUtilitiesKit/Dependency Injection/Dependencies.swift @@ -386,14 +386,18 @@ private extension Dependencies { /// Convenience method to retrieve the existing dependency instance from memory in a thread-safe way private func getValue(_ key: String, of variant: DependencyStorage.Key.Variant) -> T? { - guard let typedValue: DependencyStorage.Value = storage.instances[variant.key(key)] else { return nil } - guard let result: T = typedValue.value(as: T.self) else { - /// If there is a value stored for the key, but it's not the right type then something has gone wrong, and we should log - Log.critical("Failed to convert stored dependency '\(variant.key(key))' to expected type: \(T.self)") - return nil + return _storage.performMap { storage in + guard let typedValue: DependencyStorage.Value = storage.instances[variant.key(key)] else { + return nil + } + guard let result: T = typedValue.value(as: T.self) else { + /// If there is a value stored for the key, but it's not the right type then something has gone wrong, and we should log + Log.critical("Failed to convert stored dependency '\(variant.key(key))' to expected type: \(T.self)") + return nil + } + + return result } - - return result } /// Convenience method to store a dependency instance in memory in a thread-safe way