Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(signals): retry notification reads on transient clickhouse errors (#114119)

Mikayla Thompson7d0fccbbe200
diff --git a/docs/internal/signals-pr-lifecycle.md b/docs/internal/signals-pr-lifecycle.md
index 6187aea433bf..f2ed1677d4d3 100644
--- a/docs/internal/signals-pr-lifecycle.md
+++ b/docs/internal/signals-pr-lifecycle.md
@@ -58,6 +58,7 @@ When a project member adds a reviewer, the report shows "Added by" and that memb
Slack notifications for a ready report include only reviewers who have access to the report's project when delivery starts.
The same access rule applies when a reviewer is added later.
If no suggested reviewer has access, the ready report still goes to the configured team channel without reviewer mentions.
+The notification activity retries transient ClickHouse reads before it claims the report for delivery.
## Report links
diff --git a/products/signals/backend/temporal/inbox_notification.py b/products/signals/backend/temporal/inbox_notification.py
index 2092c9756147..6573d168b360 100644
--- a/products/signals/backend/temporal/inbox_notification.py
+++ b/products/signals/backend/temporal/inbox_notification.py
@@ -20,8 +20,10 @@ import structlog
import temporalio
from temporalio import workflow
from temporalio.common import RetryPolicy
+from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_random_exponential
from posthog.dataclasses import frozen
+from posthog.errors import CH_TRANSIENT_ERRORS
from posthog.models import Team
from posthog.sync import database_sync_to_async
from posthog.temporal.common.scoped import scoped_temporal
@@ -112,6 +114,16 @@ def _release_inbox_notification_claim(team_id: int, report_id: str) -> None:
SignalReport.objects.filter(id=report_id, team_id=team_id).update(inbox_notified_at=None)
+@retry(
+ retry=retry_if_exception_type(CH_TRANSIENT_ERRORS),
+ stop=stop_after_attempt(4),
+ wait=wait_random_exponential(multiplier=2, max=10),
+ reraise=True,
+)
+def _fetch_signals_for_notification(team: Team, report_id: str) -> list[dict]:
+ return fetch_signals_for_report_sync(team, report_id)
+
+
def _send_report_inbox_notifications(team_id: int, report_id: str) -> int:
# Guard on status: a deferred wait can outlast the READY state (suppressed/deleted/re-promoted).
report = SignalReport.objects.filter(id=report_id, team_id=team_id).only("status").first()
@@ -133,7 +145,7 @@ def _send_report_inbox_notifications(team_id: int, report_id: str) -> int:
return 0
# Re-derive source products at send time so a deferred notification reflects the current signals.
- signals = fetch_signals_for_report_sync(team, report_id)
+ signals = _fetch_signals_for_notification(team, report_id)
source_products = sorted({s["source_product"] for s in signals if s.get("source_product")})
# Point any support ticket that raised this report at it. Shares this function's READY guard.
@@ -189,7 +201,7 @@ def _send_report_github_comments(team_id: int, report_id: str) -> int:
team = Team.objects.filter(id=team_id).first()
if team is None:
return 0
- return post_report_link_to_github_issues(team, report_id, fetch_signals_for_report_sync(team, report_id))
+ return post_report_link_to_github_issues(team, report_id, _fetch_signals_for_notification(team, report_id))
@temporalio.activity.defn
diff --git a/products/signals/backend/test/test_inbox_notification_workflow.py b/products/signals/backend/test/test_inbox_notification_workflow.py
index 15a17503d2b1..69eb3eef04c1 100644
--- a/products/signals/backend/test/test_inbox_notification_workflow.py
+++ b/products/signals/backend/test/test_inbox_notification_workflow.py
@@ -12,6 +12,7 @@ from temporalio.exceptions import ApplicationError
from temporalio.testing import WorkflowEnvironment
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
+from posthog.exceptions import ClickHouseAtCapacity
from posthog.models import Organization, Team
from products.signals.backend.models import SignalReport, SignalReportTask
@@ -236,6 +237,28 @@ def test_send_stamps_the_report_and_refuses_a_second_send(team):
assert report.inbox_notified_at is not None
+def test_send_retries_transient_clickhouse_failure_before_claiming_report(team):
+ report = _make_report(team)
+ with (
+ patch(
+ "products.signals.backend.temporal.inbox_notification.fetch_signals_for_report_sync",
+ side_effect=[ClickHouseAtCapacity(), []],
+ ) as fetch,
+ patch("products.signals.backend.temporal.inbox_notification._fetch_signals_for_notification.retry.sleep"),
+ patch("products.signals.backend.temporal.inbox_notification.post_report_findings_to_tickets"),
+ patch(
+ "products.signals.backend.slack_inbox_notifications.dispatch_inbox_item_notifications", return_value=1
+ ) as dispatch,
+ ):
+ assert _send_report_inbox_notifications(team.id, str(report.id)) == 1
+
+ assert fetch.call_count == 2
+ dispatch.assert_called_once()
+ report.refresh_from_db()
+ assert report.inbox_notified_at is not None
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize("state", [NO_TASK, ALREADY_NOTIFIED])
@pytest.mark.parametrize("github_fails", [False, True])