Cancel async login if it's not waited 5 seconds after completion

This commit is contained in:
Tulir Asokan 2024-03-01 16:30:49 +02:00
parent 27d7edcb6d
commit c03191856c

13
user.go
View file

@ -470,9 +470,10 @@ func (user *User) Login(maxAttempts int) (<-chan qrChannelItem, error) {
func (user *User) AsyncLoginGoogleStart(cookies map[string]string) (outEmoji string, outErr error) { func (user *User) AsyncLoginGoogleStart(cookies map[string]string) (outEmoji string, outErr error) {
errChan := make(chan error, 1) errChan := make(chan error, 1)
if !user.googleAsyncPairErrChan.CompareAndSwap(nil, &errChan) { errChanPtr := &errChan
if !user.googleAsyncPairErrChan.CompareAndSwap(nil, errChanPtr) {
close(errChan) close(errChan)
outErr = fmt.Errorf("login already in progress") outErr = ErrLoginInProgress
return return
} }
var callbackDone bool var callbackDone bool
@ -504,6 +505,14 @@ func (user *User) AsyncLoginGoogleStart(cookies map[string]string) (outEmoji str
user.zlog.Info().Msg("Async google login succeeded") user.zlog.Info().Msg("Async google login succeeded")
} }
errChan <- err errChan <- err
if user.googleAsyncPairErrChan.Load() == errChanPtr {
go func() {
time.Sleep(5 * time.Second)
if user.googleAsyncPairErrChan.CompareAndSwap(errChanPtr, nil) {
user.zlog.Warn().Msg("Async login was never waited, clearing state")
}
}()
}
} }
}() }()
initialWait.Wait() initialWait.Wait()