Skip to content

Commit 0a005c6

Browse files
committed
refactor: receive_imf: Don't abort processing message on non-critical errors
Follow-up to f27d54f. Non-critical errors shouldn't lead to missing messages. But in debug builds, panic on such errors, otherwise we won't catch them w/o special tests.
1 parent b9183fe commit 0a005c6

File tree

2 files changed

+85
-61
lines changed

2 files changed

+85
-61
lines changed

src/receive_imf.rs

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use crate::key::self_fingerprint_opt;
3131
use crate::key::{DcKey, Fingerprint, SignedPublicKey};
3232
use crate::log::LogExt;
3333
use crate::log::{info, warn};
34-
use crate::logged_debug_assert;
3534
use crate::message::{
3635
self, Message, MessageState, MessengerMessage, MsgId, Viewtype, rfc724_mid_exists,
3736
};
@@ -47,6 +46,7 @@ use crate::sync::Sync::*;
4746
use crate::tools::{self, buf_compress, remove_subject_prefix};
4847
use crate::{chatlist_events, ensure_and_debug_assert, ensure_and_debug_assert_eq, location};
4948
use crate::{contact, imap};
49+
use crate::{logged_debug_assert, logged_debug_assert_ok};
5050

5151
/// This is the struct that is returned after receiving one email (aka MIME message).
5252
///
@@ -1146,10 +1146,8 @@ async fn decide_chat_assignment(
11461146
let now = tools::time();
11471147
let update_config = if last_time.saturating_add(24 * 60 * 60) <= now {
11481148
let mut msg = Message::new_text(stock_str::cant_decrypt_outgoing_msgs(context).await);
1149-
chat::add_device_msg(context, None, Some(&mut msg))
1150-
.await
1151-
.log_err(context)
1152-
.ok();
1149+
let res = chat::add_device_msg(context, None, Some(&mut msg)).await;
1150+
logged_debug_assert_ok!(context, res).ok();
11531151
true
11541152
} else {
11551153
last_time > now
@@ -1941,7 +1939,7 @@ async fn add_parts(
19411939
};
19421940

19431941
for (group_changes_msg, cmd, added_removed_id) in group_changes.extra_msgs {
1944-
chat::add_info_msg_with_cmd(
1942+
let res = chat::add_info_msg_with_cmd(
19451943
context,
19461944
chat_id,
19471945
&group_changes_msg,
@@ -1952,7 +1950,8 @@ async fn add_parts(
19521950
None,
19531951
added_removed_id,
19541952
)
1955-
.await?;
1953+
.await;
1954+
logged_debug_assert_ok!(context, res).ok();
19561955
}
19571956

19581957
if let Some(node_addr) = mime_parser.get_header(HeaderDef::IrohNodeAddr) {
@@ -1990,7 +1989,7 @@ async fn add_parts(
19901989
if part.is_reaction {
19911990
let reaction_str = simplify::remove_footers(part.msg.as_str());
19921991
let is_incoming_fresh = mime_parser.incoming && !seen;
1993-
set_msg_reaction(
1992+
let res = set_msg_reaction(
19941993
context,
19951994
mime_in_reply_to,
19961995
chat_id,
@@ -1999,16 +1998,21 @@ async fn add_parts(
19991998
Reaction::from(reaction_str.as_str()),
20001999
is_incoming_fresh,
20012000
)
2002-
.await?;
2001+
.await;
2002+
if logged_debug_assert_ok!(context, res).is_err() {
2003+
continue;
2004+
}
20032005
}
20042006

20052007
let mut param = part.param.clone();
20062008
if is_system_message != SystemMessage::Unknown {
20072009
param.set_int(Param::Cmd, is_system_message as i32);
20082010
}
20092011

2010-
if let Some(replace_msg_id) = replace_msg_id {
2011-
let placeholder = Message::load_from_db(context, replace_msg_id).await?;
2012+
if let Some(placeholder) = match replace_msg_id {
2013+
None => None,
2014+
Some(replace_msg_id) => Message::load_from_db_optional(context, replace_msg_id).await?,
2015+
} {
20122016
for key in [
20132017
Param::WebxdcSummary,
20142018
Param::WebxdcSummaryTimestamp,
@@ -2019,6 +2023,8 @@ async fn add_parts(
20192023
param.set(key, value);
20202024
}
20212025
}
2026+
} else {
2027+
replace_msg_id = None;
20222028
}
20232029

20242030
let (msg, typ): (&str, Viewtype) = if let Some(better_msg) = &better_msg {
@@ -2156,26 +2162,29 @@ RETURNING id
21562162
if let Some(topic) = mime_parser.get_header(HeaderDef::IrohGossipTopic) {
21572163
// default encoding of topic ids is `hex`.
21582164
let mut topic_raw = [0u8; 32];
2159-
BASE32_NOPAD
2165+
let res = BASE32_NOPAD
21602166
.decode_mut(topic.to_ascii_uppercase().as_bytes(), &mut topic_raw)
21612167
.map_err(|e| e.error)
2162-
.context("Wrong gossip topic header")?;
2163-
2164-
let topic = TopicId::from_bytes(topic_raw);
2165-
insert_topic_stub(context, *msg_id, topic).await?;
2168+
.context("Wrong gossip topic header");
2169+
if logged_debug_assert_ok!(context, res).is_ok() {
2170+
let topic = TopicId::from_bytes(topic_raw);
2171+
let res = insert_topic_stub(context, *msg_id, topic).await;
2172+
logged_debug_assert_ok!(context, res).ok();
2173+
}
21662174
} else {
21672175
warn!(context, "webxdc doesn't have a gossip topic")
21682176
}
21692177
}
21702178

2171-
maybe_set_logging_xdc_inner(
2179+
let res = maybe_set_logging_xdc_inner(
21722180
context,
21732181
part.typ,
21742182
chat_id,
21752183
part.param.get(Param::Filename),
21762184
*msg_id,
21772185
)
2178-
.await?;
2186+
.await;
2187+
logged_debug_assert_ok!(context, res).ok();
21792188
}
21802189

21812190
if let Some(replace_msg_id) = replace_msg_id {
@@ -3647,7 +3656,9 @@ async fn mark_recipients_as_verified(
36473656
}
36483657

36493658
mark_contact_id_as_verified(context, to_id, from_id).await?;
3650-
ChatId::set_protection_for_contact(context, to_id, mimeparser.timestamp_sent).await?;
3659+
let res =
3660+
ChatId::set_protection_for_contact(context, to_id, mimeparser.timestamp_sent).await;
3661+
logged_debug_assert_ok!(context, res).ok();
36513662
}
36523663

36533664
Ok(())
@@ -3709,21 +3720,21 @@ async fn add_or_lookup_contacts_by_address_list(
37093720
) -> Result<Vec<Option<ContactId>>> {
37103721
let mut contact_ids = Vec::new();
37113722
for info in address_list {
3723+
contact_ids.push(None);
37123724
let addr = &info.addr;
37133725
if !may_be_valid_addr(addr) {
3714-
contact_ids.push(None);
37153726
continue;
37163727
}
37173728
let display_name = info.display_name.as_deref();
3718-
if let Ok(addr) = ContactAddress::new(addr) {
3719-
let (contact_id, _) =
3720-
Contact::add_or_lookup(context, display_name.unwrap_or_default(), &addr, origin)
3721-
.await?;
3722-
contact_ids.push(Some(contact_id));
3723-
} else {
3729+
let Ok(addr) = ContactAddress::new(addr) else {
37243730
warn!(context, "Contact with address {:?} cannot exist.", addr);
3725-
contact_ids.push(None);
3726-
}
3731+
continue;
3732+
};
3733+
let res =
3734+
Contact::add_or_lookup(context, display_name.unwrap_or_default(), &addr, origin).await;
3735+
let contact_id = logged_debug_assert_ok!(context, res).ok().map(|(id, _)| id);
3736+
contact_ids.pop();
3737+
contact_ids.push(contact_id);
37273738
}
37283739

37293740
Ok(contact_ids)
@@ -3740,9 +3751,9 @@ async fn add_or_lookup_key_contacts_by_address_list(
37403751
let mut contact_ids = Vec::new();
37413752
let mut fingerprint_iter = fingerprints.iter();
37423753
for info in address_list {
3754+
contact_ids.push(None);
37433755
let addr = &info.addr;
37443756
if !may_be_valid_addr(addr) {
3745-
contact_ids.push(None);
37463757
continue;
37473758
}
37483759
let fingerprint: String = if let Some(fp) = fingerprint_iter.next() {
@@ -3751,24 +3762,24 @@ async fn add_or_lookup_key_contacts_by_address_list(
37513762
} else if let Some(key) = gossiped_keys.get(addr) {
37523763
key.dc_fingerprint().hex()
37533764
} else {
3754-
contact_ids.push(None);
37553765
continue;
37563766
};
37573767
let display_name = info.display_name.as_deref();
3758-
if let Ok(addr) = ContactAddress::new(addr) {
3759-
let (contact_id, _) = Contact::add_or_lookup_ex(
3760-
context,
3761-
display_name.unwrap_or_default(),
3762-
&addr,
3763-
&fingerprint,
3764-
origin,
3765-
)
3766-
.await?;
3767-
contact_ids.push(Some(contact_id));
3768-
} else {
3768+
let Ok(addr) = ContactAddress::new(addr) else {
37693769
warn!(context, "Contact with address {:?} cannot exist.", addr);
3770-
contact_ids.push(None);
3771-
}
3770+
continue;
3771+
};
3772+
let res = Contact::add_or_lookup_ex(
3773+
context,
3774+
display_name.unwrap_or_default(),
3775+
&addr,
3776+
&fingerprint,
3777+
origin,
3778+
)
3779+
.await;
3780+
let contact_id = logged_debug_assert_ok!(context, res).ok().map(|(id, _)| id);
3781+
contact_ids.pop();
3782+
contact_ids.push(contact_id);
37723783
}
37733784

37743785
ensure_and_debug_assert_eq!(contact_ids.len(), address_list.len(),);
@@ -3899,35 +3910,36 @@ async fn lookup_key_contacts_by_address_list(
38993910
let mut contact_ids = Vec::new();
39003911
let mut fingerprint_iter = fingerprints.iter();
39013912
for info in address_list {
3913+
contact_ids.push(None);
39023914
let addr = &info.addr;
39033915
if !may_be_valid_addr(addr) {
3904-
contact_ids.push(None);
39053916
continue;
39063917
}
39073918

3908-
if let Some(fp) = fingerprint_iter.next() {
3919+
let contact_id = if let Some(fp) = fingerprint_iter.next() {
39093920
// Iterator has not ran out of fingerprints yet.
39103921
let display_name = info.display_name.as_deref();
39113922
let fingerprint: String = fp.hex();
39123923

3913-
if let Ok(addr) = ContactAddress::new(addr) {
3914-
let (contact_id, _) = Contact::add_or_lookup_ex(
3915-
context,
3916-
display_name.unwrap_or_default(),
3917-
&addr,
3918-
&fingerprint,
3919-
Origin::Hidden,
3920-
)
3921-
.await?;
3922-
contact_ids.push(Some(contact_id));
3923-
} else {
3924+
let Ok(addr) = ContactAddress::new(addr) else {
39243925
warn!(context, "Contact with address {:?} cannot exist.", addr);
3925-
contact_ids.push(None);
3926-
}
3926+
continue;
3927+
};
3928+
let res = Contact::add_or_lookup_ex(
3929+
context,
3930+
display_name.unwrap_or_default(),
3931+
&addr,
3932+
&fingerprint,
3933+
Origin::Hidden,
3934+
)
3935+
.await;
3936+
logged_debug_assert_ok!(context, res).ok().map(|(id, _)| id)
39273937
} else {
3928-
let contact_id = lookup_key_contact_by_address(context, addr, chat_id).await?;
3929-
contact_ids.push(contact_id);
3930-
}
3938+
let res = lookup_key_contact_by_address(context, addr, chat_id).await;
3939+
logged_debug_assert_ok!(context, res).ok().flatten()
3940+
};
3941+
contact_ids.pop();
3942+
contact_ids.push(contact_id);
39313943
}
39323944
ensure_and_debug_assert_eq!(address_list.len(), contact_ids.len(),);
39333945
Ok(contact_ids)

src/tools.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,5 +815,17 @@ macro_rules! logged_debug_assert {
815815
};
816816
}
817817

818+
/// Logs a warning if the second arg is an error. Evaluates to the second arg value.
819+
/// In non-optimized builds, panics also on error.
820+
#[macro_export]
821+
macro_rules! logged_debug_assert_ok {
822+
($ctx:expr, $res:expr) => {{
823+
let res_val = $res;
824+
let res_val = res_val.log_err($ctx);
825+
debug_assert!(res_val.is_ok());
826+
res_val
827+
}};
828+
}
829+
818830
#[cfg(test)]
819831
mod tools_tests;

0 commit comments

Comments
 (0)