From 236a128129f594657478e3868430871dc75f60fa Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Thu, 25 Aug 2022 20:10:43 +0300 Subject: [PATCH] Fix double membership=join event handling This fixes the problem mentioned in 8e1aa5b11c3cf0db. Turns out that it's a long-standing Synapse bug: https://github.com/matrix-org/synapse/issues/9768 --- bot/bot.go | 17 +++++++++-------- bot/sync.go | 12 ++++++++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 946d1f3..c9b37f5 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -19,14 +19,15 @@ import ( // Bot represents matrix bot type Bot struct { - noowner bool - federation bool - prefix string - domain string - rooms map[string]id.RoomID - roomsmu *sync.Mutex - log *logger.Logger - lp *linkpearl.Linkpearl + noowner bool + federation bool + prefix string + domain string + rooms map[string]id.RoomID + roomsmu *sync.Mutex + log *logger.Logger + lp *linkpearl.Linkpearl + handledEvents sync.Map } // New creates a new matrix bot diff --git a/bot/sync.go b/bot/sync.go index 4cc7af6..35efec0 100644 --- a/bot/sync.go +++ b/bot/sync.go @@ -12,10 +12,6 @@ func (b *Bot) initSync() { b.lp.OnEventType( event.StateMember, func(_ mautrix.EventSource, evt *event.Event) { - // Trying to debug the membership=join event being handled twice here. - eventJSON, _ := evt.MarshalJSON() - b.log.Debug(string(eventJSON)) - go b.onMembership(evt) }, ) @@ -108,6 +104,14 @@ func (b *Bot) onEncryptedMessage(evt *event.Event) { // onBotJoin handles the "bot joined the room" event func (b *Bot) onBotJoin(evt *event.Event, hub *sentry.Hub) { + // Workaround for membership=join events which are delivered to us twice, + // as described in this bug report: https://github.com/matrix-org/synapse/issues/9768 + _, exists := b.handledEvents.LoadOrStore(evt.ID, true) + if exists { + b.log.Info("Suppressing already handled event %s", evt.ID) + return + } + ctx := sentry.SetHubOnContext(context.Background(), hub) span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("onBotJoin")) defer span.Finish()