Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): insert contacts in key order so concurrent syncs cannot deadlock (#7479)

teo26d48a14aa45
diff --git a/crates/email/src/outbound/email_pg_repo/contact.rs b/crates/email/src/outbound/email_pg_repo/contact.rs
index 4fe9613d06..4c073b11f5 100644
--- a/crates/email/src/outbound/email_pg_repo/contact.rs
+++ b/crates/email/src/outbound/email_pg_repo/contact.rs
@@ -1,6 +1,6 @@
use crate::domain::models::{ParsedAddresses, RecipientType, UpsertedContacts, UpsertedRecipient};
use sqlx::PgPool;
-use std::collections::HashMap;
+use std::collections::{BTreeMap, HashMap};
use uuid::Uuid;
/// Upsert contacts from parsed addresses. Called outside a transaction to avoid deadlocks.
@@ -10,8 +10,9 @@ pub(crate) async fn upsert_contacts(
link_id: Uuid,
addresses: ParsedAddresses,
) -> Result<UpsertedContacts, sqlx::Error> {
- // Collect all unique email addresses and normalize names
- let mut unique_map: HashMap<String, Option<String>> = HashMap::new();
+ // Collect all unique email addresses and normalize names. Sorted so that concurrent inserts of
+ // overlapping addresses claim their unique keys in the same order and cannot deadlock.
+ let mut unique_map: BTreeMap<String, Option<String>> = BTreeMap::new();
let from_lower = addresses.from_email.to_lowercase();
let from_name_normalized =
@@ -68,13 +69,15 @@ pub(crate) async fn upsert_contacts(
.collect();
// Step 2: Update names for existing contacts that don't have one yet
- let name_updates: Vec<(Uuid, String)> = unique_map
+ let mut name_updates: Vec<(Uuid, String)> = unique_map
.iter()
.filter_map(|(email, name)| {
name.as_ref()
.and_then(|n| email_to_id.get(email).map(|id| (*id, n.clone())))
})
.collect();
+ // The update locks rows in array order, so concurrent updates must agree on it.
+ name_updates.sort_unstable_by_key(|(id, _)| *id);
if !name_updates.is_empty() {
let update_ids: Vec<Uuid> = name_updates.iter().map(|(id, _)| *id).collect();
diff --git a/crates/email_db_client/src/contacts/upsert_message.rs b/crates/email_db_client/src/contacts/upsert_message.rs
index 018be21f45..5b42003e41 100644
--- a/crates/email_db_client/src/contacts/upsert_message.rs
+++ b/crates/email_db_client/src/contacts/upsert_message.rs
@@ -200,6 +200,11 @@ async fn update_missing_contact_names(
pool: &PgPool,
updates: &[(Uuid, String)],
) -> anyhow::Result<()> {
+ // Rows are locked in array order; concurrent messages naming the same contacts must lock
+ // them in the same order or they deadlock.
+ let mut updates: Vec<&(Uuid, String)> = updates.iter().collect();
+ updates.sort_unstable_by_key(|(id, _)| *id);
+
let ids: Vec<Uuid> = updates.iter().map(|(id, _)| *id).collect();
let names: Vec<String> = updates.iter().map(|(_, name)| name.clone()).collect();
@@ -225,6 +230,12 @@ async fn insert_new_contacts(
pool: &PgPool,
contacts: &[ContactPhotoless],
) -> anyhow::Result<Vec<address::FetchedAddressId>> {
+ // Rows claim their (link_id, email_address) keys in array order; concurrent messages that
+ // share new addresses must claim them in the same order or they deadlock.
+ let mut contacts: Vec<&ContactPhotoless> = contacts.iter().collect();
+ contacts
+ .sort_unstable_by(|a, b| (a.link_id, &a.email_address).cmp(&(b.link_id, &b.email_address)));
+
let mut ids: Vec<Uuid> = Vec::with_capacity(contacts.len());
let mut link_ids: Vec<Uuid> = Vec::with_capacity(contacts.len());
let mut emails: Vec<String> = Vec::with_capacity(contacts.len());
diff --git a/crates/email_db_client/src/contacts/upsert_message/tests.rs b/crates/email_db_client/src/contacts/upsert_message/tests.rs
index 143c578d63..356ac63eb6 100644
--- a/crates/email_db_client/src/contacts/upsert_message/tests.rs
+++ b/crates/email_db_client/src/contacts/upsert_message/tests.rs
@@ -324,6 +324,76 @@ async fn insert_new_contacts_allows_same_email_different_links(pool: Pool<Postgr
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("upsert_message_contacts"))
+)]
+// should not deadlock when concurrent batches insert the same new addresses in different orders
+async fn insert_new_contacts_overlapping_batches_do_not_deadlock(
+ pool: Pool<Postgres>,
+) -> Result<()> {
+ const _: &sqlx::migrate::Migrator = &MACRO_DB_MIGRATIONS;
+
+ let link_id = Uuid::parse_str("00000000-0000-0000-0000-00000000001a")?;
+ let contact = |email: &str| ContactPhotoless {
+ id: macro_uuid::generate_uuid_v7(),
+ link_id,
+ email_address: email.to_string(),
+ name: None,
+ };
+ let batch_a = [
+ contact("[email protected]"),
+ contact("[email protected]"),
+ contact("[email protected]"),
+ ];
+ let batch_b = [
+ contact("[email protected]"),
+ contact("[email protected]"),
+ contact("[email protected]"),
+ ];
+
+ // An uncommitted insert of the gate address parks both batches mid-statement, so each batch can
+ // hold an address the other still needs at the moment the gate opens.
+ let mut gate = pool.begin().await?;
+ sqlx::query("INSERT INTO email_contacts (id, link_id, email_address) VALUES ($1, $2, $3)")
+ .bind(macro_uuid::generate_uuid_v7())
+ .bind(link_id)
+ .execute(&mut *gate)
+ .await?;
+
+ let open_gate = async {
+ tokio::time::timeout(std::time::Duration::from_secs(10), async {
+ loop {
+ let parked: i64 = sqlx::query_scalar(
+ "SELECT count(*) FROM pg_stat_activity
+ WHERE datname = current_database() AND wait_event_type = 'Lock'",
+ )
+ .fetch_one(&pool)
+ .await?;
+ if parked == 2 {
+ return Ok::<_, sqlx::Error>(());
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(5)).await;
+ }
+ })
+ .await??;
+ gate.rollback().await?;
+ anyhow::Ok(())
+ };
+
+ let (inserted_a, inserted_b, opened) = tokio::join!(
+ insert_new_contacts(&pool, &batch_a),
+ insert_new_contacts(&pool, &batch_b),
+ open_gate
+ );
+ opened?;
+
+ assert_eq!(inserted_a?.len() + inserted_b?.len(), 3);
+
+ Ok(())
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../fixtures", scripts("update_missing_contact_names"))