From 70322699abcf69531fdb47a282561f19592b8dfd Mon Sep 17 00:00:00 2001 From: Till <2353100+S7evinK@users.noreply.github.com> Date: Thu, 9 Mar 2023 09:52:13 +0100 Subject: [PATCH] Unset `RoomServerEvent`, since we can't be sure that `Set` actually updates the cached entry (#3002) This should deflake UTs and be more correct in terms of getting `Events`. `Events` tries to fetch the event from the cache first and may get an unredacted event from it, while it should already be redacted. --- internal/caching/cache_roomevents.go | 5 +++++ roomserver/storage/shared/storage.go | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/caching/cache_roomevents.go b/internal/caching/cache_roomevents.go index 9d5d3b91..14b6c3af 100644 --- a/internal/caching/cache_roomevents.go +++ b/internal/caching/cache_roomevents.go @@ -10,6 +10,7 @@ import ( type RoomServerEventsCache interface { GetRoomServerEvent(eventNID types.EventNID) (*gomatrixserverlib.Event, bool) StoreRoomServerEvent(eventNID types.EventNID, event *gomatrixserverlib.Event) + InvalidateRoomServerEvent(eventNID types.EventNID) } func (c Caches) GetRoomServerEvent(eventNID types.EventNID) (*gomatrixserverlib.Event, bool) { @@ -19,3 +20,7 @@ func (c Caches) GetRoomServerEvent(eventNID types.EventNID) (*gomatrixserverlib. func (c Caches) StoreRoomServerEvent(eventNID types.EventNID, event *gomatrixserverlib.Event) { c.RoomServerEvents.Set(int64(eventNID), event) } + +func (c Caches) InvalidateRoomServerEvent(eventNID types.EventNID) { + c.RoomServerEvents.Unset(int64(eventNID)) +} diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index 106a8244..78bda95e 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -567,6 +567,7 @@ func (d *EventDatabase) events( if !redactionsArePermanent { d.applyRedactions(results) } + return results, nil } eventJSONs, err := d.EventJSONTable.BulkSelectEventJSON(ctx, txn, eventNIDs) if err != nil { @@ -578,8 +579,9 @@ func (d *EventDatabase) events( } for _, eventJSON := range eventJSONs { + redacted := gjson.GetBytes(eventJSON.EventJSON, "unsigned.redacted_because").Exists() events[eventJSON.EventNID], err = gomatrixserverlib.NewEventFromTrustedJSONWithEventID( - eventIDs[eventJSON.EventNID], eventJSON.EventJSON, false, roomInfo.RoomVersion, + eventIDs[eventJSON.EventNID], eventJSON.EventJSON, redacted, roomInfo.RoomVersion, ) if err != nil { return nil, err @@ -1020,7 +1022,9 @@ func (d *EventDatabase) MaybeRedactEvent( return fmt.Errorf("d.RedactionsTable.MarkRedactionValidated: %w", err) } - d.Cache.StoreRoomServerEvent(redactedEvent.EventNID, redactedEvent.Event) + // We remove the entry from the cache, as if we just "StoreRoomServerEvent", we can't be + // certain that the cached entry actually is updated, since ristretto is eventual-persistent. + d.Cache.InvalidateRoomServerEvent(redactedEvent.EventNID) return nil })