diff --git a/libgm/client.go b/libgm/client.go index f273ecc..3f814bc 100644 --- a/libgm/client.go +++ b/libgm/client.go @@ -168,12 +168,13 @@ func (c *Client) postConnect() { time.Sleep(2 * time.Second) if c.skipCount > 0 { c.Logger.Warn().Int("skip_count", c.skipCount).Msg("Skip count is non-zero in postConnect, waiting longer") - for i := 0; i < 10 && c.skipCount > 0; i++ { + for i := 0; i < 3 && c.skipCount > 0; i++ { time.Sleep(1 * time.Second) } if c.skipCount > 0 { c.Logger.Warn().Int("skip_count", c.skipCount).Msg("Skip count is still non-zero") } + c.triggerEvent(&events.HackySetActiveMayFail{}) } c.Logger.Debug().Msg("Sending acks before get updates request") c.sessionHandler.sendAckRequest() diff --git a/libgm/events/ready.go b/libgm/events/ready.go index 0f7cb77..c34321f 100644 --- a/libgm/events/ready.go +++ b/libgm/events/ready.go @@ -92,3 +92,5 @@ type PingFailed struct { Error error ErrorCount int } + +type HackySetActiveMayFail struct{} diff --git a/user.go b/user.go index 93e3b30..722e68f 100644 --- a/user.go +++ b/user.go @@ -81,6 +81,7 @@ type User struct { batteryLowAlertSent time.Time pollErrorAlertSent bool phoneNotRespondingAlertSent bool + didHackySetActive bool loginInProgress atomic.Bool pairSuccessChan chan struct{} @@ -644,6 +645,7 @@ func (user *User) HasSession() bool { func (user *User) DeleteSession() { user.Session = nil user.SelfParticipantIDs = []string{} + user.didHackySetActive = false err := user.Update(context.TODO()) if err != nil { user.zlog.Err(err).Msg("Failed to delete session from database") @@ -673,6 +675,28 @@ func (user *User) sendMarkdownBridgeAlert(ctx context.Context, important bool, f } } +func (user *User) hackyResetActive() { + if user.didHackySetActive { + return + } + user.didHackySetActive = true + time.Sleep(7 * time.Second) + if !user.ready && user.phoneResponding { + user.zlog.Warn().Msg("Client is still not ready, trying to re-set active session") + err := user.Client.SetActiveSession() + if err != nil { + user.zlog.Err(err).Msg("Failed to re-set active session") + } else { + time.Sleep(7 * time.Second) + } + if !user.ready && user.phoneResponding { + user.zlog.Warn().Msg("Client is still not ready, reconnecting") + user.DeleteConnection() + user.Connect() + } + } +} + func (user *User) syncHandleEvent(event any) { ctx := context.TODO() switch v := event.(type) { @@ -716,6 +740,8 @@ func (user *User) syncHandleEvent(event any) { go user.sendMarkdownBridgeAlert(ctx, false, "Phone is responding again") user.phoneNotRespondingAlertSent = false } + case *events.HackySetActiveMayFail: + go user.hackyResetActive() case *events.PingFailed: if errors.Is(v.Error, events.ErrRequestedEntityNotFound) { go user.Logout(status.BridgeState{