From 6961ebc3590adf28db55371859f0d1a76ca7b5e2 Mon Sep 17 00:00:00 2001 From: Tomas Baculak Date: Thu, 13 Apr 2023 11:56:21 +0200 Subject: [PATCH] serialized requests on IAMBasedConnectionPool dictionary is not a thread safe object, need to be serialized in some way to avoid crashes --- .../IAMBasedConnectionPool.swift | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/AWSAppSyncClient/Internal/SubscriptionFactory/IAMBasedConnectionPool.swift b/AWSAppSyncClient/Internal/SubscriptionFactory/IAMBasedConnectionPool.swift index a4fcb68b..666c2545 100644 --- a/AWSAppSyncClient/Internal/SubscriptionFactory/IAMBasedConnectionPool.swift +++ b/AWSAppSyncClient/Internal/SubscriptionFactory/IAMBasedConnectionPool.swift @@ -13,6 +13,10 @@ class IAMBasedConnectionPool: SubscriptionConnectionPool { private let credentialProvider: AWSCredentialsProvider private let regionType: AWSRegionType var endPointToProvider: [String: ConnectionProvider] + + private let queue = DispatchQueue(label: "com.amazonaws.connectionPool.IAMBased.concurrentQueue", + attributes: .concurrent, + target:.global(qos: .userInitiated)) init(_ credentialProvider: AWSCredentialsProvider, region: AWSRegionType) { self.credentialProvider = credentialProvider @@ -21,15 +25,16 @@ class IAMBasedConnectionPool: SubscriptionConnectionPool { } func connection(for url: URL, connectionType: SubscriptionConnectionType) -> SubscriptionConnection { - - let connectionProvider = endPointToProvider[url.absoluteString] ?? - ConnectionProviderFactory.createConnectionProvider(for: URLRequest(url: url), + queue.sync(flags: .barrier) { + let connectionProvider = endPointToProvider[url.absoluteString] ?? + ConnectionProviderFactory.createConnectionProvider(for: URLRequest(url: url), authInterceptor: IAMAuthInterceptor(credentialProvider, region: regionType), connectionType: connectionType) - - endPointToProvider[url.absoluteString] = connectionProvider - let connection = AppSyncSubscriptionConnection(provider: connectionProvider) - return connection + + endPointToProvider[url.absoluteString] = connectionProvider + let connection = AppSyncSubscriptionConnection(provider: connectionProvider) + return connection + } } }