Skip to content

AWSCloudWatchLoggingCategoryClient asks for Current User Identifier During Configuration (CRASH) #3897

@Hartistic

Description

@Hartistic

Describe the bug

While configuring Amplify with an AWSCloudWatchLoggingPluginConfiguration and an AWSCognitoAuthPlugin configuration the library has a race condition that crashes the application 50% of the time with an error of Thread 20: Fatal error: Authentication category is not configured. Call Amplify.configure() before using any methods on the category.

The problem is that these two configurations are added to Amplify and this crash occurs when calling Amplify.configure().

Order of Methods that causes crash to occur:

  1. try configure(CategoryType.logging.category, using: resolvedConfiguration)
  2. try configurable.configure(using: configuration)
  3. try configure(using: categoryConfiguration(from: amplifyConfiguration))
  4. try Amplify.configure(plugins: Array(plugins.values), using: configuration)
  5. try plugin.configure(using: pluginConfiguration)
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
            self.loggingClient.takeUserIdentifierFromCurrentUser()
        }
func takeUserIdentifierFromCurrentUser() {
        Task {
            do {
                let user = try await authentication.getCurrentUser()
                self.userIdentifier = user.userId
            } catch {
                self.userIdentifier = nil
            }
            self.updateSessionControllers()
        }
    }
public func getCurrentUser() async throws -> AuthUser {
        try await plugin.getCurrentUser()
    }
return Fatal.preconditionFailure(
                """
                \(categoryType.displayName) category is not configured. Call Amplify.configure() before using \
                any methods on the category.
                """
            )

Steps To Reproduce

Steps to reproduce the behavior:
1. Add AWSCloudWatchLoggingPluginConfiguration and AWSCognitoAuthPlugin to Amplify:

    private func addAuthenticationPlugin() throws {
        let authPlugin = AWSCognitoAuthPlugin(networkPreferences: Self.networkPreferences)
        do {
            try Amplify.add(plugin: authPlugin)
        } catch {
            throw error
        }
    }

    private func addCloudWatchLoggingPlugin() throws {
        do {
            let logLevels: [String: LogLevel] = ["AWSAuthFetchSessionTask": .error]
            let loggingConstraints = LoggingConstraints(defaultLogLevel: .debug, categoryLogLevel: logLevels)
            let loggingConfiguration = AWSCloudWatchLoggingPluginConfiguration(logGroupName: "your log group",
                                                                               region: "us-east-1",
                                                                               localStoreMaxSizeInMB: 1,
                                                                               flushIntervalInSeconds: 10,
                                                                               loggingConstraints: loggingConstraints)
            let plugin = AWSCloudWatchLoggingPlugin(loggingPluginConfiguration: loggingConfiguration)
            try Amplify.add(plugin: plugin)
        } catch {
            throw error
        }
    }
2. load a amplifyconfiguration.json and call configure:

    private func loadAmplifyConfiguration() throws {
        do {
            let amplifyConfig = try getLocalAmplifyConfigurationFile()
            try Amplify.configure(amplifyConfig)
        } catch {
            throw error
        }
    }
3. On iPhone 15 Pro Max and newer seems to crash way more often.

Expected behavior

The AWSCloudWatchLoggingPluginConfiguration shouldn't be asking for Authentication details during it's configuration...the library says you can only call configure() once, so configuring Auth and then Logging isn't possible.

Amplify Framework Version

2.41.1

Amplify Categories

Analytics, API, Auth

Dependency manager

Swift PM

Swift version

5.8.0

CLI version

I am not sure

Xcode version

16.0

Relevant log output

No response

Is this a regression?

Yes

Regression additional context

No response

Platforms

iOS

OS Version

IOS 18

Device

iPhone 15 Pro Max

Specific to simulators

no

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    authIssues related to the Auth categoryquestionGeneral question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions