: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(signals): treat stale read-only connection in scratchpad prune as transient (#117506)
posthog[bot]890be4ff63f6
diff --git a/products/signals/backend/tasks.py b/products/signals/backend/tasks.pyindex 5147782a98df..dc44ba36e96e 100644--- a/products/signals/backend/tasks.py+++ b/products/signals/backend/tasks.py@@ -2,7 +2,7 @@ from datetime import timedeltafrom decimal import Decimalfrom django.core.cache import cache-from django.db import OperationalError+from django.db import OperationalError, connectionsfrom django.db.models import Qfrom django.utils import timezone@@ -23,6 +23,7 @@ from posthog.models.organization import BillingPeriodfrom posthog.models.scoping import with_team_scopefrom posthog.ph_client import ph_scoped_capturefrom posthog.scoping_audit import skip_team_scope_audit+from posthog.temporal.common.utils import is_stale_connection_read_only_errorfrom products.signals.backend.billing import current_billing_period_boundsfrom products.signals.backend.implementation_dispatch_tasks import (@@ -1136,8 +1137,13 @@ def prune_expired_scratchpad_entries_task() -> None:# A transient DB blip self-heals — the sweep runs again tomorrow — so don't page on it.logger.warning("signals_scout.scratchpad_prune_transient_db_error", error=str(exc))except Exception as exc:- capture_exception(exc)- logger.exception("signals_scout.scratchpad_prune_failed")+ if is_stale_connection_read_only_error(exc):+ # The pooled connection outlived a primary failover. Drop it so the next run reconnects to the primary.+ connections.close_all()+ logger.warning("signals_scout.scratchpad_prune_transient_db_error", error=str(exc))+ else:+ capture_exception(exc)+ logger.exception("signals_scout.scratchpad_prune_failed")logger.info("signals_scout scratchpad prune finished", deleted=deleted)diff --git a/products/signals/backend/test/test_scratchpad_retention.py b/products/signals/backend/test/test_scratchpad_retention.pyindex 6aa8de804f28..ea50eb656c0d 100644--- a/products/signals/backend/test/test_scratchpad_retention.py+++ b/products/signals/backend/test/test_scratchpad_retention.py@@ -1,13 +1,21 @@from datetime import timedeltafrom posthog.test.base import BaseTest+from unittest.mock import patch+from django.db import InternalErrorfrom django.utils import timezone+from parameterized import parameterized+from posthog.models.scoping import team_scopefrom products.signals.backend.models import SignalScratchpad-from products.signals.backend.tasks import SCRATCHPAD_EXPIRY_GRACE_DAYS, prune_expired_scratchpad_entries+from products.signals.backend.tasks import (+ SCRATCHPAD_EXPIRY_GRACE_DAYS,+ prune_expired_scratchpad_entries,+ prune_expired_scratchpad_entries_task,+)class TestPruneExpiredScratchpadEntries(BaseTest):@@ -29,3 +37,24 @@ class TestPruneExpiredScratchpadEntries(BaseTest):assert deleted == 1survivors = set(SignalScratchpad.objects.unscoped().values_list("key", flat=True))assert survivors == {"durable", "live", "within_grace"}++ @parameterized.expand(+ [+ ("stale_connection", "cannot execute DELETE in a read-only transaction", True),+ ("other_internal_error", "deadlock detected", False),+ ]+ )+ def test_task_reports_only_errors_that_are_not_a_stale_connection(+ self, _name: str, message: str, is_stale: bool+ ) -> None:+ with (+ patch(+ "products.signals.backend.tasks.prune_expired_scratchpad_entries",+ side_effect=InternalError(message),+ ),+ patch("products.signals.backend.tasks.connections") as connections,+ patch("products.signals.backend.tasks.capture_exception") as capture,+ ):+ prune_expired_scratchpad_entries_task()+ assert connections.close_all.called == is_stale+ assert capture.called == (not is_stale)