Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key import race causing a worse key to be saved in place of a better version. #4227

Open
BillCarsonFr opened this issue Nov 6, 2024 · 3 comments

Comments

@BillCarsonFr
Copy link
Member

As seen in this rageshake https://rageshakes.element.io/api/listing/2024-11-06/130841-YOSPUGGB/console.2024-11-06-12.log.gz

There is slightly late room key, so there is an UTD and the sdk tries to query the key from key storage.
And just after the room key arrives down the sync.

We then have two concurent actions trying to import the key

The race is that they both try to see if there is an existing key in order to see if the new one is better or worse

let old_session = self
.inner
.store
.get_inbound_group_session(session.room_id(), session.session_id())
.await?;
// Only import the session if we didn't have this session or
// if it's a better version of the same session.
if new_session_better(&session, old_session).await {

and

match self.store().compare_group_session(&session).await? {
SessionOrdering::Better => {
info!("Received a new megolm room key");
Ok(Some(session))
}
comparison_result => {
warn!(
?comparison_result,
"Received a megolm room key that we already have a better version \
of, discarding"
);
Ok(None)
}
}

But they both think they are the first and that there is no matching key in store.

Therefore the first one to commit to the store wins. By-pasing the better/worse check.
The late key is better than the key from key storage, because it is safe (no gray shield)

@BillCarsonFr
Copy link
Member Author

One source of element-hq/element-x-ios#3352

@BillCarsonFr
Copy link
Member Author

We might want to have some import room key lock.

@andybalaam
Copy link
Member

@poljar : we think adding an in-memory lock would fix this, but we need to try it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants