private async Task <IAuthenticationCredentials> GetCredentialsWithRetry(int retryCount) { for (int i = 0; i < retryCount + 1; i++) { if (FatalError) { throw new AuthException(); } var currentAuth = _currentAuthenticationCredentials; if (currentAuth?.IsAlive() == true) { return(currentAuth); } await CredantialsRequestLock.WaitAsync(); try { if (FatalError) { throw new AuthException(); } if (_currentAuthenticationCredentials?.IsAlive() != true) { var result = await RequestCredentials(); if (result != null) { _currentAuthenticationCredentials = result; } else { FatalError = true; } } } finally { CredantialsRequestLock.Release(); } } throw new TimeoutException(); }