This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Update get_pdu to return the original, pristine EventBase
#13320
Merged
Merged
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ee236ca
Update get_pdu to return original, pristine EventBase
MadLittleMods 79a1b72
Add changelog
MadLittleMods bfd35fd
Internal change, no specific bugfix
MadLittleMods e0e20a5
Explain why not
MadLittleMods 22410f2
Add tests
MadLittleMods 09c411b
Some more clarity
MadLittleMods 6029b42
Re-use room ID
MadLittleMods 09167b1
Better still actionable no-fluff assertion message
MadLittleMods eb6a291
Describe why we use a cache here
MadLittleMods 1c4e57c
Remove direct access to internal property
MadLittleMods 488f5ed
Make it obvious that we're pulling and using a different cache
MadLittleMods 29a5269
Remove assumption/speculation
MadLittleMods 2688e44
Default is already no metadata
MadLittleMods 24913e7
Refactor structure to avoid duplicating the event copy logic
MadLittleMods 0e6dd5a
Pluralization typo
MadLittleMods 5bc75ed
Explain that we return a copy that is safe to modify
MadLittleMods dea7669
Fix lints
MadLittleMods 72e65a5
Fix description typo
MadLittleMods 86fe0dc
Share event throughout
MadLittleMods fd879bb
Different comment
MadLittleMods 354678f
Merge branch 'madlittlemods/pristine-get_pdu' of github.com:matrix-or…
MadLittleMods 233077c
Merge branch 'develop' into madlittlemods/pristine-get_pdu
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix `FederationClient.get_pdu()` returning events from the cache as `outliers` instead of original events we saw over federation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,7 @@ | |
| RoomVersion, | ||
| RoomVersions, | ||
| ) | ||
| from synapse.events import EventBase, builder | ||
| from synapse.events import EventBase, builder, make_event_from_dict | ||
| from synapse.federation.federation_base import ( | ||
| FederationBase, | ||
| InvalidEventSignatureError, | ||
|
|
@@ -309,7 +309,7 @@ async def get_pdu_from_destination_raw( | |
| ) | ||
|
|
||
| logger.debug( | ||
| "retrieved event id %s from %s: %r", | ||
| "get_pdu_from_destination_raw: retrieved event id %s from %s: %r", | ||
| event_id, | ||
| destination, | ||
| transaction_data, | ||
|
|
@@ -360,9 +360,25 @@ async def get_pdu( | |
|
|
||
| # TODO: Rate limit the number of times we try and get the same event. | ||
|
|
||
| ev = self._get_pdu_cache.get(event_id) | ||
| if ev: | ||
| return ev | ||
| event_from_cache = self._get_pdu_cache.get(event_id) | ||
| if event_from_cache: | ||
| assert not event_from_cache.internal_metadata.outlier, ( | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| "Event from cache unexpectedly an `outlier` when it should be pristine and untouched without metadata set. " | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| "We are probably not be returning a copy of the event because downstream callers are modifying the event reference we have in the cache." | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| # Make sure to return a copy because downstream callers will use | ||
| # this event reference directly and change our original, pristine, | ||
| # untouched PDU. For example when people mark the event as an | ||
| # `outlier` (`event.internal_metadata.outlier = true`), we don't | ||
| # want that to propagate back into the cache. | ||
| event_copy = make_event_from_dict( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we're going to build a new EventBase on each call, why not just cache the raw json?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would work. But we would have to convert the |
||
| event_from_cache.get_pdu_json(), | ||
| event_from_cache.room_version, | ||
| internal_metadata_dict=None, | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| return event_copy | ||
|
|
||
| pdu_attempts = self.pdu_destination_tried.setdefault(event_id, {}) | ||
|
|
||
|
|
@@ -405,7 +421,22 @@ async def get_pdu( | |
| if signed_pdu: | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
| self._get_pdu_cache[event_id] = signed_pdu | ||
|
|
||
| return signed_pdu | ||
| # Make sure to return a copy because downstream callers will use this | ||
| # event reference directly and change our original, pristine, untouched | ||
| # PDU. For example when people mark the event as an `outlier` | ||
| # (`event.internal_metadata.outlier = true`), we don't want that to | ||
| # propagate back into the cache. | ||
| # | ||
| # We could get away with only making a new copy of the event when | ||
| # pulling from cache but it's probably better to have good hygiene and | ||
| # not dirty the cache in the first place as well. | ||
| event_copy = make_event_from_dict( | ||
| signed_pdu.get_pdu_json(), | ||
| signed_pdu.room_version, | ||
| internal_metadata_dict=None, | ||
| ) | ||
|
MadLittleMods marked this conversation as resolved.
Outdated
|
||
|
|
||
| return event_copy | ||
|
|
||
| async def get_room_state_ids( | ||
| self, destination: str, room_id: str, event_id: str | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.