diff --git a/src/service/rooms/event_handler/fetch_and_handle_outliers.rs b/src/service/rooms/event_handler/fetch_and_handle_outliers.rs index fabd4aef3..804fc7ea6 100644 --- a/src/service/rooms/event_handler/fetch_and_handle_outliers.rs +++ b/src/service/rooms/event_handler/fetch_and_handle_outliers.rs @@ -2,7 +2,7 @@ use assign::assign; use conduwuit::{ - Err, Event, PduEvent, debug, debug_info, debug_warn, err, error, + Err, Event, PduEvent, debug, debug_error, debug_info, debug_warn, err, error, state_res::lexicographical_topological_sort, trace, utils::{IterStream, stream::BroadbandExt}, @@ -149,15 +149,21 @@ pub(super) async fn fetch_and_handle_missing_events<'a, Pdu>( candidates.len() ); + let mut seen: HashMap = HashMap::new(); for id in events { - let mut todo: VecDeque<_> = [id.clone()].into(); + let mut todo: VecDeque = [id.clone()].into(); while let Some(next_id) = todo.pop_front() { if seeded_events.contains_key(&next_id) { continue; } if let Ok(local_pdu) = self.services.timeline.get_pdu(&next_id).await { trace!("Found {next_id} in db"); - seeded_events.insert(id.clone(), local_pdu.into_canonical_object()); + seeded_events.insert(next_id.clone(), local_pdu.into_canonical_object()); + continue; + } + let attempts = seen.get(&*next_id).copied().unwrap_or_default(); + if attempts >= 5 { + debug_error!(%attempts, %next_id, "Could not fetch missing event after 5 attempts, giving up"); continue; } @@ -206,6 +212,8 @@ pub(super) async fn fetch_and_handle_missing_events<'a, Pdu>( trace!(%auth_event_id, "Already found auth event"); continue; } + debug!("Missing auth event {auth_event_id} for event {next_id}"); + seen.insert(auth_event_id.clone(), attempts.saturating_add(1)); todo.push_back(auth_event_id); have_all_auth = false; } @@ -213,13 +221,14 @@ pub(super) async fn fetch_and_handle_missing_events<'a, Pdu>( // all of its auth events have been fetched. // TODO: This may result in infinite looping, needs a breaker if have_all_auth { - debug!(%event_id, "Have all auth events"); - seeded_events.insert(event_id, value); + debug!(%next_id, "Have all auth events"); + seeded_events.insert(next_id, value); } else { debug_warn!( - "Fetched {event_id} but missing some auth events, will have to re-fetch." + "Fetched {next_id} but missing some auth events, will have to re-fetch." ); - todo.push_back(event_id); + seen.insert(next_id.clone(), attempts.saturating_add(1)); + todo.push_back(next_id); } } }