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(whatsapp): stop a group added during its first message waiting forever (#9776)

Wolf Mermelstein0cecb3efcb57
diff --git a/.sqlx/query-e33ab34f74a21214d5216ec07070bf758a2e65d732242f31e58a11c7470f7ef7.json b/.sqlx/query-e33ab34f74a21214d5216ec07070bf758a2e65d732242f31e58a11c7470f7ef7.json
new file mode 100644
index 0000000000..8b5078a441
--- /dev/null
+++ b/.sqlx/query-e33ab34f74a21214d5216ec07070bf758a2e65d732242f31e58a11c7470f7ef7.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT pg_advisory_xact_lock(hashtextextended('external-chat:whatsapp-anchors:' || $1, 0))",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "pg_advisory_xact_lock",
+ "type_info": "Void"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "e33ab34f74a21214d5216ec07070bf758a2e65d732242f31e58a11c7470f7ef7"
+}
diff --git a/crates/whatsapp_linked/AGENTS.md b/crates/whatsapp_linked/AGENTS.md
index 30553fdc73..1130898503 100644
--- a/crates/whatsapp_linked/AGENTS.md
+++ b/crates/whatsapp_linked/AGENTS.md
@@ -49,7 +49,7 @@ They share no HTTP: only MacroDB rows and `NOTIFY` (`external_chat`'s
`waiting_for_message` until a chat update brings one, and a direct chat (a
saved contact, a looked-up number) is added `done`, made
`waiting_for_phone` only by an anchor older than its adoption while nothing
- was imported. The phone is never asked without that Import. Saved contacts are chat-list rows with no activity. History
+ was imported. The phone is never asked without that Import. Recording an anchor and adding a chat share a per-user advisory lock (`lock_anchors_in`), so a chat added while its first message is recorded still sees it. Saved contacts are chat-list rows with no activity. History
messages skip the adoption-time boundary but only reach the account owner's
own groups (`observed_bindings_in`).
- `src/outbound/pg/lookups.rs`: phone-number lookups (`whatsapp_lookup`,
diff --git a/crates/whatsapp_linked/Cargo.toml b/crates/whatsapp_linked/Cargo.toml
index e06a796a73..73457fc836 100644
--- a/crates/whatsapp_linked/Cargo.toml
+++ b/crates/whatsapp_linked/Cargo.toml
@@ -64,5 +64,5 @@ image = { workspace = true }
macro_db_migrator = { path = "../macro_db_migrator" }
model_user = { path = "../model_user" }
models_permissions = { path = "../models_permissions" }
-tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
+tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }
tower = { workspace = true, features = ["util"] }
diff --git a/crates/whatsapp_linked/src/outbound/pg/adoptions.rs b/crates/whatsapp_linked/src/outbound/pg/adoptions.rs
index 4b5205b889..80f1ef9a3c 100644
--- a/crates/whatsapp_linked/src/outbound/pg/adoptions.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/adoptions.rs
@@ -33,6 +33,8 @@ impl AdoptionRepository for PgWhatsAppRepository {
// for a message that had already arrived.
sqlx::query_scalar!("SELECT id FROM whatsapp_linked_account WHERE id = $1 AND user_id = $2 AND generation = $3 AND state = 'connected' FOR NO KEY UPDATE", account.id,user,account.authorization_generation)
.fetch_optional(&mut *tx).await.map_err(failure)?.ok_or(ExternalChatError::Disconnected)?;
+ // Taken after the account, in update_chats_in's order.
+ super::chats::lock_anchors_in(&mut tx, user).await?;
let existing = sqlx::query_as!(ChatRow,"SELECT id, provider, remote_id, kind, user_id, state, adopted_at FROM external_chat WHERE user_id = $1 AND provider = 'whatsapp' AND remote_id = $2 AND state <> 'detached' FOR UPDATE",user,adoption.jid)
.fetch_optional(&mut *tx).await.map_err(failure)?;
let adopted = match existing {
diff --git a/crates/whatsapp_linked/src/outbound/pg/chats.rs b/crates/whatsapp_linked/src/outbound/pg/chats.rs
index cb305888c1..25b6cacbea 100644
--- a/crates/whatsapp_linked/src/outbound/pg/chats.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/chats.rs
@@ -25,6 +25,24 @@ fn merge(into: &mut ChatUpdate, update: &ChatUpdate) {
}
}
+/// Serializes recording chat anchors with adding chats, per user. Under read
+/// committed each would otherwise miss the other's uncommitted write: a group
+/// added while the replica records its first message would wait for a
+/// message forever.
+pub(super) async fn lock_anchors_in(
+ tx: &mut Transaction<'_, Postgres>,
+ user: &str,
+) -> ExternalChatResult<()> {
+ sqlx::query!(
+ "SELECT pg_advisory_xact_lock(hashtextextended('external-chat:whatsapp-anchors:' || $1, 0))",
+ user
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(failure)?;
+ Ok(())
+}
+
/// Upsert chat-list rows in a few statements for the whole batch; signal the
/// owner's list once if any row changed. A message for an added chat that was
/// waiting for one makes it ready to import (`waiting_for_phone`).
@@ -49,6 +67,9 @@ pub(super) async fn update_chats_in(
}
}
}
+ if merged.values().any(|chat| chat.anchor.is_some()) {
+ lock_anchors_in(tx, &account.user_id).await?;
+ }
let (created, updated): (Vec<&ChatUpdate>, Vec<&ChatUpdate>) =
merged.values().partition(|chat| chat.create);
let anchors = created
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs b/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
index ba55a45bf4..868e992230 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
@@ -93,6 +93,69 @@ async fn observers_can_refresh_groups_in_opposite_orders(pool: PgPool) {
tokio::join!(alice_refresh, bob_refresh);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn adding_a_group_waits_for_its_first_message_in_flight(pool: PgPool) {
+ workspace(&pool).await;
+ let (linked, session, _) = services(&pool);
+ let alice = pair(&linked, &session, ALICE, ALICE_JID).await;
+ let info = group(&[ALICE_JID, CUSTOMER_JID]);
+ session
+ .update_groups(alice, std::slice::from_ref(&info), true)
+ .await
+ .unwrap();
+ // The replica records the group's first message, not yet committed.
+ let mut tx = pool.begin().await.unwrap();
+ let account = fence_in(&mut tx, alice).await.unwrap();
+ super::super::chats::update_chats_in(
+ &mut tx,
+ &account,
+ &[ChatUpdate {
+ jid: GROUP.to_owned(),
+ kind: ExternalChatKind::Group,
+ name: None,
+ last_activity_at: Some(Utc::now()),
+ archived: None,
+ pinned: None,
+ anchor: Some(MessageAnchor {
+ id: "3EB0F157".to_owned(),
+ at: Utc::now(),
+ from_me: false,
+ sender_jid: None,
+ }),
+ create: true,
+ }],
+ )
+ .await
+ .unwrap();
+ let add = AddChat {
+ provider: ExternalChatProvider::WhatsApp,
+ remote_id: GROUP.into(),
+ kind: ExternalChatKind::Group,
+ };
+ let alice_user = person(ALICE);
+ let adding = linked.chats.add(&alice_user, &add, SendAuthority::Human);
+ tokio::pin!(adding);
+ assert!(
+ tokio::time::timeout(std::time::Duration::from_millis(500), &mut adding)
+ .await
+ .is_err(),
+ "adding waits for the message instead of missing it"
+ );
+ tx.commit().await.unwrap();
+ let binding = adding.await.unwrap();
+ assert_eq!(
+ sqlx::query_scalar!(
+ "SELECT backfill_state FROM external_chat WHERE id = $1",
+ binding.id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap()
+ .as_deref(),
+ Some("waiting_for_phone")
+ );
+}
+
/// A chat-list update carrying `group`'s first message.
fn first_message(group: &str) -> crate::domain::events::ChatUpdate {
crate::domain::events::ChatUpdate {
diff --git a/deny.toml b/deny.toml
index 5e9e89faa6..f03755fc50 100644
--- a/deny.toml
+++ b/deny.toml
@@ -91,6 +91,7 @@ ignore = [
{ id = "RUSTSEC-2026-0258", reason = "h2 0.3 (via Stripe 0.41's hyper-0.14 client) has no patched release; the hyper 1.x line is bumped to the patched 0.4.16" },
{ id = "RUSTSEC-2026-0346", reason = "Temporarily accepted for rustls 0.21 pinned by Stripe 0.41; other consumers use patched rustls 0.23.46" },
{ id = "RUSTSEC-2026-0349", reason = "Temporarily accepted for rustls 0.21 pinned by Stripe 0.41; other consumers use patched rustls 0.23.46" },
+ { id = "RUSTSEC-2026-0441", reason = "pin-utils is unmaintained, not unsound; transitive via aws-smithy-http, -runtime and -types, no release without it yet" },
# 0.26.11 only re-exports webpki-roots 1.x; the bans below require patched roots.
# Remove this exception when SQLx and the remaining callers stop using the wrapper.
{ id = "RUSTSEC-2026-0381", reason = "webpki-roots 0.26.11 contains no certificates and re-exports patched 1.0.10; bans reject every other version below 1.0.10" },