From 972d5cefdbdb2219798aaec06491275ed0f20995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 6 Apr 2023 15:57:00 +0200 Subject: [PATCH] If we can't load an outbound group session from the store, rotate it An error is currently thrown if loading an outbound group session fails. This error may only affect loading outbound group sessions, while other store operations continue to work fine. As a result, the user is unable to send messages, but can still use the application without any problems. Since outbound group sessions are rotated frequently, we can simply rotate the session if loading fails. However, if the error is related to a more serious storage issue, persisting the newly rotated outbound group session will also fail. If the error is specific to outbound group sessions, the user will be able to continue using the application without interruption. --- .../src/session_manager/group_sessions.rs | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs b/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs index 9d177b6bd..1b6d8d4c2 100644 --- a/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs +++ b/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs @@ -29,7 +29,7 @@ use ruma::{ UserId, }; use serde_json::Value; -use tracing::{debug, info, trace}; +use tracing::{debug, error, info, instrument, trace}; use crate::{ error::{EventError, MegolmResult, OlmResult}, @@ -68,16 +68,23 @@ impl GroupSessionCache { // and put it in the cache. if let Some(s) = self.sessions.get(room_id) { Ok(Some(s.clone())) - } else if let Some(s) = self.store.get_outbound_group_session(room_id).await? { - for request_id in s.pending_request_ids() { - self.sessions_being_shared.insert(request_id, s.clone()); - } - - self.sessions.insert(room_id.to_owned(), s.clone()); - - Ok(Some(s)) } else { - Ok(None) + match self.store.get_outbound_group_session(room_id).await { + Ok(Some(s)) => { + for request_id in s.pending_request_ids() { + self.sessions_being_shared.insert(request_id, s.clone()); + } + + self.sessions.insert(room_id.to_owned(), s.clone()); + + Ok(Some(s)) + } + Ok(None) => Ok(None), + Err(e) => { + error!("Couldn't restore an outbound group session: {e:?}"); + Ok(None) + } + } } } @@ -452,13 +459,14 @@ impl GroupSessionManager { /// /// `encryption_settings` - The settings that should be used for /// the room key. + #[instrument(skip(self, users, encryption_settings))] pub async fn share_room_key( &self, room_id: &RoomId, users: impl Iterator, encryption_settings: impl Into, ) -> OlmResult>> { - trace!(room_id = room_id.as_str(), "Checking if a room key needs to be shared"); + trace!("Checking if a room key needs to be shared"); let encryption_settings = encryption_settings.into(); let mut changes = Changes::default(); @@ -489,7 +497,6 @@ impl GroupSessionManager { changes.inbound_group_sessions.push(inbound); debug!( - room_id = room_id.as_str(), old_session_id = old_session_id, session_id = outbound.session_id(), "A user or device has left the room since we last sent a \ @@ -528,7 +535,6 @@ impl GroupSessionManager { info!( index = message_index, ?recipients, - room_id = room_id.as_str(), session_id = outbound.session_id(), "Trying to encrypt a room key", );