stream-chat-swift icon indicating copy to clipboard operation
stream-chat-swift copied to clipboard

Failure to obtain token results in an infinite loop

Open pavermakov opened this issue 1 year ago • 5 comments

What did you do?

I have the following tokenProvider in my app:

private lazy var tokenProvider: TokenProvider = { [weak self] completion in
        _ = self?.authRepository.chatToken()
            .observeOn(MainScheduler.instance)
            .subscribe(
                onSuccess: { response in
                    if let chatToken = response.chatToken {
                        completion(.success(Token(stringLiteral: chatToken)))
                        do {
                            let decoder = JWTDecoder()
                            let jwtData = try decoder.decode(jwtToken: chatToken)
                            if let exp = jwtData["exp"] as? TimeInterval {
                                self?.tokenExpiryTime = exp
                                self?.tokenExpiryDate = Date(timeIntervalSince1970: exp)
                            }
                        } catch {
                            completion(.failure(AppErrors.generic))
                        }
                    } else {
                        completion(.failure(AppErrors.generic))
                    }
                },
                onError: { error in
                    completion(.failure(error))
                }
            )
    }

If the authRepository.chatToken() fails with, for example, a 401 error, the stream chat will start repeatedly calling the tokenProvider and failing.

Screenshot 2024-09-18 at 19 15 24

What did you expect to happen?

The completion handler of the connectUser to be called with an error. Or a way to break out of the loop.

What happened instead?

An infinite loop.

GetStream Environment

GetStream Chat version: 4.52.0 GetStream Chat frameworks: StreamChat, StreamChatUI iOS version: 17.5.1 Swift version: 5.8.1 Xcode version: 15.3 Device: iPhone 12

pavermakov avatar Sep 18 '24 18:09 pavermakov

Hi @pavermakov,

This issue is already fixed on more recent versions. Please update the SDK to the latest version.

Thank you, Nuno

nuno-vieira avatar Sep 19 '24 16:09 nuno-vieira

Hi @nuno-vieira! Bumped version up to 4.63.0 and observed the same behaviour. The tokenProvider is called over and over again.

pavermakov avatar Sep 21 '24 14:09 pavermakov

Hi @pavermakov,

It is true, I can reproduce this. I will re-open it. We will let you know once we work on this issue. At the moment I can't provide you with an ETA.

Best, Nuno

nuno-vieira avatar Sep 23 '24 23:09 nuno-vieira

Thank you @nuno-vieira, I appreciate that! Also, I have noticed that the class AuthenticationRepository has constant maximumTokenRefreshAttempts set to 10. Is it possible to customise this value?

pavermakov avatar Sep 25 '24 05:09 pavermakov

Hi @pavermakov, no, but you can provide a timeout through ChatClientConfig.reconnectionTimeout

nuno-vieira avatar Sep 25 '24 11:09 nuno-vieira

Hi @pavermakov, are you still able to reproduce this? I'm not at the moment, the token provider is only called 10 times as per the maximumTokenRefreshAttempts. Also, make sure that if the response has a 401 status, that you return an error, and not a success on the Token Provider response.

nuno-vieira avatar Nov 07 '24 17:11 nuno-vieira

@nuno-vieira I no longer get an infinite loop, just invoking the token provider 10 times.

pavermakov avatar Nov 12 '24 15:11 pavermakov

Awesome 🙏

I'll close this one then 👍

nuno-vieira avatar Nov 12 '24 15:11 nuno-vieira