From 0f4f00295c327c3b314d34ded4cc5f2225b10557 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 26 Feb 2024 16:53:36 +0200 Subject: [PATCH] Add hacky sleep before sending set active session request --- libgm/client.go | 8 ++++++++ libgm/pair_google.go | 1 + 2 files changed, 9 insertions(+) diff --git a/libgm/client.go b/libgm/client.go index 2679556..f6f6476 100644 --- a/libgm/client.go +++ b/libgm/client.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/url" + "sync/atomic" "time" "github.com/google/uuid" @@ -74,6 +75,8 @@ type Client struct { conversationsFetchedOnce bool + hackyDelaySetActive atomic.Bool + AuthData *AuthData cfg *gmproto.Config @@ -160,6 +163,11 @@ func (c *Client) Connect() error { } func (c *Client) postConnect() { + // For some reason SetActiveSession fails if it's called immediately after reconnecting after a google login, + // so hackily delay it a few seconds to make it work. + if c.hackyDelaySetActive.CompareAndSwap(true, false) { + time.Sleep(3 * time.Second) + } c.Logger.Debug().Msg("Sending get updates request") err := c.SetActiveSession() if err != nil { diff --git a/libgm/pair_google.go b/libgm/pair_google.go index 4081281..4f59037 100644 --- a/libgm/pair_google.go +++ b/libgm/pair_google.go @@ -300,6 +300,7 @@ func (c *Client) DoGaiaPairing(ctx context.Context, emojiCallback func(string)) c.AuthData.PairingID = ps.UUID c.triggerEvent(&events.PairSuccessful{PhoneID: c.AuthData.Mobile.GetSourceID()}) + c.hackyDelaySetActive.Store(true) go func() { // Sleep for a bit to let the phone save the pair data. If we reconnect too quickly, // the phone won't recognize the session the bridge will get unpaired.