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.
This commit is contained in:
Damir Jelić
2023-04-06 15:57:00 +02:00
parent c73aeef2ed
commit 972d5cefdb

View File

@@ -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<Item = &UserId>,
encryption_settings: impl Into<EncryptionSettings>,
) -> OlmResult<Vec<Arc<ToDeviceRequest>>> {
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",
);