Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(frecency): clean up stale transaction in poller to prevent deadlock (#2609)
gbirman67c7873cd0cb
diff --git a/rust/cloud-storage/frecency/src/outbound/postgres.rs b/rust/cloud-storage/frecency/src/outbound/postgres.rsindex 96a491074f..5bf7fa4236 100644--- a/rust/cloud-storage/frecency/src/outbound/postgres.rs+++ b/rust/cloud-storage/frecency/src/outbound/postgres.rs@@ -442,6 +442,21 @@ impl UnprocessedEventsRepo for FrecencyPgProcessor {async fn get_unprocessed_events(&self,) -> Result<Vec<EventRecordWithId<'static, i64>>, Self::Err> {+ // Clean up any stale transaction from a previous failed processing run.+ // Without this, a failed run leaves its tx (and advisory lock) in the mutex,+ // causing every subsequent poll to fail on pg_try_advisory_xact_lock.+ {+ let mut guard = self.tx.try_lock()?;+ if let Some(stale_tx) = guard.take() {+ tracing::warn!(+ "rolling back stale frecency transaction from a previous failed run"+ );+ let _ = stale_tx.rollback().await.inspect_err(|e| {+ tracing::error!(error = ?e, "failed to rollback stale frecency transaction");+ });+ }+ }+let mut tx = self.pool.begin().await?;let true = sqlx::query_scalar!(r#"diff --git a/rust/cloud-storage/frecency/src/outbound/postgres/tests.rs b/rust/cloud-storage/frecency/src/outbound/postgres/tests.rsindex fd0994900a..a229d580dc 100644--- a/rust/cloud-storage/frecency/src/outbound/postgres/tests.rs+++ b/rust/cloud-storage/frecency/src/outbound/postgres/tests.rs@@ -899,20 +899,20 @@ async fn it_cannot_be_read_concurrently(pool: PgPool) {.await.unwrap();- let processor = FrecencyPgProcessor::new(pool.clone());-- let events = processor.get_unprocessed_events().await.unwrap();+ let first = FrecencyPgProcessor::new(pool.clone());+ let second = FrecencyPgProcessor::new(pool);+ let events = first.get_unprocessed_events().await.unwrap();assert_eq!(events.len(), 1);assert_eq!(events.first().unwrap().id, event_id);- // try to create a second processor and read events- let second = FrecencyPgProcessor::new(pool);- // this fails because the original processor still holds the lock- let _err = processor.get_unprocessed_events().await.unwrap_err();+ // try to read from a second processor+ // this fails because the first processor still holds the lock+ let err = second.get_unprocessed_events().await.unwrap_err();+ assert!(matches!(err, PollerErr::DbLockErr));// finish the tx- processor.mark_processed(Vec::new()).await.unwrap();+ first.mark_processed(Vec::new()).await.unwrap();// now second can read because the transaction has finishedlet res = second.get_unprocessed_events().await.unwrap();@@ -920,6 +920,45 @@ async fn it_cannot_be_read_concurrently(pool: PgPool) {assert_eq!(res.first().unwrap().id, event_id);}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn test_stale_transaction_is_cleaned_up(pool: PgPool) {+ let test_user_id = "test_stale_tx_cleanup";++ sqlx::query!(+ r#"+ INSERT INTO frecency_events (+ user_id, entity_type, event_type, timestamp,+ connection_id, entity_id, was_processed+ )+ VALUES ($1, $2, $3, $4, $5, $6, false)+ "#,+ test_user_id,+ "document",+ "open",+ Utc::now(),+ "conn_tx",+ "doc_tx"+ )+ .execute(&pool)+ .await+ .unwrap();++ let processor = FrecencyPgProcessor::new(pool);++ // First call succeeds, starts a transaction and stores it in the mutex+ let events = processor.get_unprocessed_events().await.unwrap();+ assert_eq!(events.len(), 1);++ // Simulate a processing failure by NOT calling mark_processed.+ // The next call to get_unprocessed_events should clean up the stale tx+ // and successfully start a new processing cycle.+ let events = processor.get_unprocessed_events().await.unwrap();+ assert_eq!(events.len(), 1);++ // Verify we can still complete the full lifecycle+ processor.mark_processed(Vec::new()).await.unwrap();+}+/// Tests that mixing supported (chat_id) and unsupported (role) filters/// does not produce malformed SQL. Role filters don't apply to frecency/// aggregates and should be ignored without breaking the query.