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): stop bulk Gmail deletes from queuing on one CRM cleanup row (#9358)

teo6610366a527d
diff --git a/.sqlx/query-368f362bd133f3dbea094757d24e04056011bc04e76c956e94b4badeec59edc5.json b/.sqlx/query-368f362bd133f3dbea094757d24e04056011bc04e76c956e94b4badeec59edc5.json
deleted file mode 100644
index 94a994d7cf..0000000000
--- a/.sqlx/query-368f362bd133f3dbea094757d24e04056011bc04e76c956e94b4badeec59edc5.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO crm_cleanup_candidates (link_id, contact_email)\n SELECT $1, unnest($2::text[])\n ON CONFLICT (link_id, contact_email) DO UPDATE SET created_at = now()\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "TextArray"
- ]
- },
- "nullable": []
- },
- "hash": "368f362bd133f3dbea094757d24e04056011bc04e76c956e94b4badeec59edc5"
-}
diff --git a/.sqlx/query-5ebe5411562b9d4546a7969f4858a1469d0e25e2e314554c8815fb9bd3dfdadc.json b/.sqlx/query-5ebe5411562b9d4546a7969f4858a1469d0e25e2e314554c8815fb9bd3dfdadc.json
deleted file mode 100644
index cd05dfd901..0000000000
--- a/.sqlx/query-5ebe5411562b9d4546a7969f4858a1469d0e25e2e314554c8815fb9bd3dfdadc.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "INSERT INTO crm_cleanup_candidates (link_id, contact_email)\n SELECT DISTINCT $2::uuid, lower(trim(c.email_address))\n FROM email_message_recipients r JOIN email_contacts c ON c.id = r.contact_id\n WHERE r.message_id=$1 AND c.email_address IS NOT NULL AND trim(c.email_address) <> ''\n ON CONFLICT (link_id, contact_email) DO UPDATE SET created_at=NOW()",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "5ebe5411562b9d4546a7969f4858a1469d0e25e2e314554c8815fb9bd3dfdadc"
-}
diff --git a/.sqlx/query-86d25242758f7528bbad456f87df54a16205fe23ae743e66194733ea3cb80910.json b/.sqlx/query-86d25242758f7528bbad456f87df54a16205fe23ae743e66194733ea3cb80910.json
new file mode 100644
index 0000000000..4f6037054e
--- /dev/null
+++ b/.sqlx/query-86d25242758f7528bbad456f87df54a16205fe23ae743e66194733ea3cb80910.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO crm_cleanup_candidates (link_id, contact_email)\n SELECT $1, contact_email\n FROM unnest($2::text[]) AS contact_email\n ORDER BY contact_email\n ON CONFLICT (link_id, contact_email) DO UPDATE SET created_at = now()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "TextArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "86d25242758f7528bbad456f87df54a16205fe23ae743e66194733ea3cb80910"
+}
diff --git a/.sqlx/query-aabd840d29381ccb28f16a1162e0d67add00264fbb31bb36c6e3744a5fdd2f18.json b/.sqlx/query-aabd840d29381ccb28f16a1162e0d67add00264fbb31bb36c6e3744a5fdd2f18.json
new file mode 100644
index 0000000000..049a7a3baf
--- /dev/null
+++ b/.sqlx/query-aabd840d29381ccb28f16a1162e0d67add00264fbb31bb36c6e3744a5fdd2f18.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT DISTINCT lower(trim(c.email_address)) AS \"email!\"\n FROM email_message_recipients r JOIN email_contacts c ON c.id = r.contact_id\n WHERE r.message_id=$1 AND c.email_address IS NOT NULL AND trim(c.email_address) <> ''",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "email!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "aabd840d29381ccb28f16a1162e0d67add00264fbb31bb36c6e3744a5fdd2f18"
+}
diff --git a/.sqlx/query-b839d9a95009e4026efb41d9b6fb05688bf715ca65d03afd58d214d0fd8e56f0.json b/.sqlx/query-b839d9a95009e4026efb41d9b6fb05688bf715ca65d03afd58d214d0fd8e56f0.json
new file mode 100644
index 0000000000..b3b34ad3a5
--- /dev/null
+++ b/.sqlx/query-b839d9a95009e4026efb41d9b6fb05688bf715ca65d03afd58d214d0fd8e56f0.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT id FROM email_threads WHERE id = $1 FOR UPDATE",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "b839d9a95009e4026efb41d9b6fb05688bf715ca65d03afd58d214d0fd8e56f0"
+}
diff --git a/crates/email_db_client/src/crm_cleanup/candidates.rs b/crates/email_db_client/src/crm_cleanup/candidates.rs
index 4b06e3637a..70ee87e6e1 100644
--- a/crates/email_db_client/src/crm_cleanup/candidates.rs
+++ b/crates/email_db_client/src/crm_cleanup/candidates.rs
@@ -11,6 +11,9 @@ use sqlx::types::Uuid;
/// populate still in flight. Keeping the original timestamp would let the
/// clock expire while that populate is pending.
///
+/// Rows are written in email order, so two deletes that share contacts lock
+/// them in the same order and wait instead of deadlocking.
+///
/// Returns the number of rows recorded (inserted or refreshed).
#[tracing::instrument(skip(executor, contact_emails), err)]
pub async fn insert_candidates<'e, E>(
@@ -28,7 +31,9 @@ where
let result = sqlx::query!(
r#"
INSERT INTO crm_cleanup_candidates (link_id, contact_email)
- SELECT $1, unnest($2::text[])
+ SELECT $1, contact_email
+ FROM unnest($2::text[]) AS contact_email
+ ORDER BY contact_email
ON CONFLICT (link_id, contact_email) DO UPDATE SET created_at = now()
"#,
link_id,
diff --git a/crates/email_db_client/src/messages/delete.rs b/crates/email_db_client/src/messages/delete.rs
index 10c4c1dd44..0d6ecf7682 100644
--- a/crates/email_db_client/src/messages/delete.rs
+++ b/crates/email_db_client/src/messages/delete.rs
@@ -1,3 +1,4 @@
+use crate::crm_cleanup::candidates::insert_candidates;
use crate::threads;
use models_email::email::service::message;
use sqlx::types::Uuid;
@@ -52,6 +53,15 @@ async fn delete_message_in_thread(
});
}
+ // Deletes in one thread take turns here, so the last one to commit sees
+ // the others' deletions when it checks for an empty thread and recomputes.
+ sqlx::query!(
+ "SELECT id FROM email_threads WHERE id = $1 FOR UPDATE",
+ thread_id
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+
// if it was the only message in the thread, delete the thread too
let deleted_thread = threads::delete::delete_thread_if_empty(&mut *tx, thread_id).await?;
@@ -129,16 +139,13 @@ pub async fn reconcile_deferred_deletion(
// Capture recipients before the cascade. Extra candidates are harmless:
// the CRM cleanup worker checks remaining email sources before removal.
- sqlx::query!(
- r#"INSERT INTO crm_cleanup_candidates (link_id, contact_email)
- SELECT DISTINCT $2::uuid, lower(trim(c.email_address))
+ let crm_emails = sqlx::query_scalar!(
+ r#"SELECT DISTINCT lower(trim(c.email_address)) AS "email!"
FROM email_message_recipients r JOIN email_contacts c ON c.id = r.contact_id
- WHERE r.message_id=$1 AND c.email_address IS NOT NULL AND trim(c.email_address) <> ''
- ON CONFLICT (link_id, contact_email) DO UPDATE SET created_at=NOW()"#,
+ WHERE r.message_id=$1 AND c.email_address IS NOT NULL AND trim(c.email_address) <> ''"#,
message_id,
- link_id,
)
- .execute(&mut *tx)
+ .fetch_all(&mut *tx)
.await?;
let result = delete_message_in_thread(
&mut tx,
@@ -148,6 +155,9 @@ pub async fn reconcile_deferred_deletion(
message.is_draft,
)
.await?;
+ // Upsert last, as inbox sync does: candidate rows are locked after the
+ // thread row in every delete.
+ insert_candidates(&mut *tx, link_id, &crm_emails).await?;
tx.commit().await?;
tracing::info!(event = "email_send.deferred_deletion_resolved", %message_id, %link_id);
Ok(match result {
diff --git a/services/email_service/src/pubsub/inbox_sync/operations/delete_message.rs b/services/email_service/src/pubsub/inbox_sync/operations/delete_message.rs
index f164884fc3..c2ce29fb2a 100644
--- a/services/email_service/src/pubsub/inbox_sync/operations/delete_message.rs
+++ b/services/email_service/src/pubsub/inbox_sync/operations/delete_message.rs
@@ -87,14 +87,6 @@ pub async fn delete_message(
sender.into_iter().filter_map(|c| c.email_address).collect()
};
- // Commit CRM cleanup candidates with the deletion so neither can survive
- // a rollback alone. Deferred deletions retain the message; cleanup's
- // `link_has_any_message_with` pre-check keeps its contacts until removal.
- if !crm_emails.is_empty() {
- let self_email = link.email_address.0.as_ref().to_ascii_lowercase();
- insert_crm_cleanup_candidates(&mut *tx, link.id, &self_email, crm_emails).await?;
- }
-
let result = async {
let result = email_db_client::messages::delete::delete_message_with_tx(&mut tx, &message)
.await
@@ -104,6 +96,15 @@ pub async fn delete_message(
source: e.context("Failed to delete message with transaction".to_string()),
})
})?;
+ // Commit CRM cleanup candidates with the deletion so neither can survive
+ // a rollback alone. Deferred deletions retain the message; cleanup's
+ // `link_has_any_message_with` pre-check keeps its contacts until removal.
+ // Upsert last: the row lock lasts until commit, and every delete of mail
+ // with the same contact waits on it.
+ if !crm_emails.is_empty() {
+ let self_email = link.email_address.0.as_ref().to_ascii_lowercase();
+ insert_crm_cleanup_candidates(&mut *tx, link.id, &self_email, crm_emails).await?;
+ }
Ok::<MessageDeletion, ProcessingError>(result)
}
.await;