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(data-warehouse): explain a sync that lost its connection (#95592)

Tom Owerseb29b3a502b3
diff --git a/products/warehouse_sources/backend/temporal/data_imports/external_data_job.py b/products/warehouse_sources/backend/temporal/data_imports/external_data_job.py
index 5a9ad16ee4bd..3a2049154fc8 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/external_data_job.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/external_data_job.py
@@ -177,6 +177,57 @@ Any_Source_Errors: dict[str, str | None] = {
}
+# Customer-facing copy for a failure that stayed retryable and still exhausted its retry budget.
+# Nothing is disabled and the schema syncs again on its next schedule, so the copy explains that
+# rather than asking for a re-enable.
+TRANSIENT_SOURCE_CONNECTION_MESSAGE = (
+ "PostHog lost its connection to your source and couldn't reconnect. Check that it stays "
+ "reachable and doesn't drop idle connections; the next sync runs on schedule."
+)
+
+TRANSIENT_POOLER_MESSAGE = (
+ "Your database's connection pooler couldn't reach your database and refused the connection. "
+ "Check that the database is running; the next sync runs on schedule."
+)
+
+TRANSIENT_EGRESS_MESSAGE = (
+ "PostHog couldn't reach your source over the network. This is on PostHog's side and usually "
+ "clears on its own; the next sync runs on schedule."
+)
+
+# A retryable failure that outlives its retries keeps whatever the driver said, so `latest_error`
+# ends up holding raw connection text — a psycopg "connection to server at <host>, port <port>
+# failed: ..." line, a pymysql `(2013, ...)` tuple, a urllib3 connection-pool dump. None of it
+# names something the customer can act on, and all of it echoes their host and port back at them.
+# Unlike `Any_Source_Errors` above, matching here only rewrites the stored message: the error stays
+# retryable and the schema stays enabled. Keys are the same stable, host-free fragments the sources
+# already classify these conditions by, so they can't collide with a customer value.
+Transient_Error_Messages: dict[str, str] = {
+ # libpq/psycopg losing an established connection, at connect or mid-stream, in every wording
+ # `_CONNECTION_DROPPED_ERROR_SUBSTRINGS` (postgres.py) retries in-process first.
+ "server closed the connection unexpectedly": TRANSIENT_SOURCE_CONNECTION_MESSAGE,
+ "SSL connection has been closed unexpectedly": TRANSIENT_SOURCE_CONNECTION_MESSAGE,
+ "SSL SYSCALL error": TRANSIENT_SOURCE_CONNECTION_MESSAGE,
+ "connection to server was lost": TRANSIENT_SOURCE_CONNECTION_MESSAGE,
+ # pymysql error 2013, retried in-process by `_connect_with_transient_retry` (mysql.py). The
+ # deterministic filesort variant carries its own marker and is classified non-retryable first.
+ "Lost connection to MySQL server": TRANSIENT_SOURCE_CONNECTION_MESSAGE,
+ # A TLS session cut at the socket, which clickhouse-connect surfaces through urllib3 rather
+ # than as a driver error (see the ClickHouse source's `get_retryable_errors`).
+ "UNEXPECTED_EOF_WHILE_READING": TRANSIENT_SOURCE_CONNECTION_MESSAGE,
+ # Supavisor refusing a connect because its own lookup of the tenant's database failed — the
+ # pooler's bookkeeping, not the customer's credentials, which postgres.py retries in-process.
+ "(ECIRCUITBREAKER) failed to retrieve database credentials": TRANSIENT_POOLER_MESSAGE,
+ "(EAUTHQUERY)": TRANSIENT_POOLER_MESSAGE,
+ # PostHog's own egress proxy refusing the CONNECT. Nothing on the customer's side is wrong, so
+ # this message asks nothing of them.
+ "Cannot connect to proxy": TRANSIENT_EGRESS_MESSAGE,
+ "Tunnel connection failed: 502": TRANSIENT_EGRESS_MESSAGE,
+ "Tunnel connection failed: 503": TRANSIENT_EGRESS_MESSAGE,
+ "Tunnel connection failed: 504": TRANSIENT_EGRESS_MESSAGE,
+}
+
+
UNEXPECTED_ERROR_MESSAGE = "An unexpected error has occurred"
CANCELLED_RUN_MESSAGE = (
@@ -227,6 +278,21 @@ def _is_app_db_failure(internal_error: str) -> bool:
return normalized.startswith(APP_DB_ERROR_PREFIX) and READ_ONLY_TRANSACTION_PHRASE in normalized
+def _transient_error_message(internal_error: str) -> str | None:
+ """Customer-facing copy for a transient failure that outlived its retries, if we have any.
+
+ First match wins, mirroring how the non-retryable path picks its friendly message.
+ """
+ return next(
+ (
+ message
+ for pattern, message in Transient_Error_Messages.items()
+ if error_message_matches(internal_error, [pattern])
+ ),
+ None,
+ )
+
+
def _fail_stale_running_schema(
schema_id: str, team_id: int, latest_error: str | None, logger: FilteringBoundLogger
) -> None:
@@ -379,6 +445,10 @@ async def update_external_data_job_model(inputs: UpdateExternalDataJobStatusInpu
disable_error_message=inputs.latest_error or AUTO_DISABLED_JOB_ERROR,
disable_exclude_workflow_id=activity.info().workflow_id,
)
+ elif not platform_failure:
+ transient_message = _transient_error_message(internal_error_normalized)
+ if transient_message is not None:
+ inputs.latest_error = transient_message
await database_sync_to_async_pool(update_external_job_status)(
job_id=job_id,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/test_external_data_job_activities.py b/products/warehouse_sources/backend/temporal/data_imports/tests/test_external_data_job_activities.py
index ec206384d45d..153a6e1399ed 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/tests/test_external_data_job_activities.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/test_external_data_job_activities.py
@@ -18,6 +18,9 @@ from products.warehouse_sources.backend.models.external_data_schema import Exter
from products.warehouse_sources.backend.models.external_data_source import ExternalDataSource
from products.warehouse_sources.backend.temporal.data_imports.external_data_job import (
CANCELLED_RUN_MESSAGE,
+ TRANSIENT_EGRESS_MESSAGE,
+ TRANSIENT_POOLER_MESSAGE,
+ TRANSIENT_SOURCE_CONNECTION_MESSAGE,
UNEXPECTED_ERROR_MESSAGE,
UpdateExternalDataJobStatusInputs,
_customer_facing_error,
@@ -258,3 +261,82 @@ def test_read_only_transaction_disables_the_schema_only_when_the_source_raised_i
assert mock_update_should_sync.called is expect_disabled
customer_message = mock_update_job_status.call_args.kwargs["latest_error"] or ""
assert ("tries to write to your database" in customer_message) is expect_disabled
+
+
+# transaction=True for the same reason as the tests above: the activity resolves the source through
+# database_sync_to_async_pool, and the pool thread's connection can't see a test transaction.
+ [
+ # psycopg's connect-time wrapper around a dropped TLS session. The address is a
+ # documentation-reserved one, standing in for the host the driver echoes back.
+ (
+ "postgres_ssl_drop",
+ ExternalDataSourceType.POSTGRES,
+ 'connection failed: connection to server at "198.51.100.7", port 5432 failed: '
+ "SSL SYSCALL error: EOF detected",
+ TRANSIENT_SOURCE_CONNECTION_MESSAGE,
+ ),
+ # pymysql renders a mid-query drop as a bare code/message tuple.
+ (
+ "mysql_lost_connection",
+ ExternalDataSourceType.MYSQL,
+ "(2013, 'Lost connection to MySQL server during query')",
+ TRANSIENT_SOURCE_CONNECTION_MESSAGE,
+ ),
+ # Supavisor refusing the connect after its own credential lookup failed.
+ (
+ "pooler_credential_lookup",
+ ExternalDataSourceType.POSTGRES,
+ "FATAL: (ECIRCUITBREAKER) failed to retrieve database credentials after multiple "
+ "attempts, new connections are temporarily blocked",
+ TRANSIENT_POOLER_MESSAGE,
+ ),
+ # PostHog's own egress proxy refusing the CONNECT.
+ (
+ "egress_proxy_bad_gateway",
+ ExternalDataSourceType.SALESFORCE,
+ "ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 502 Bad gateway'))",
+ TRANSIENT_EGRESS_MESSAGE,
+ ),
+ ]
+)
[email protected]_db(transaction=True)
+def test_transient_failure_replaces_the_raw_driver_text_without_disabling_the_schema(
+ _name: str, source_type: ExternalDataSourceType, internal_error: str, expected_message: str
+) -> None:
+ org = Organization.objects.create(name="org")
+ team = Team.objects.create(organization=org, name="team")
+ source = ExternalDataSource.objects.create(team=team, source_type=source_type.value)
+ schema = ExternalDataSchema.objects.create(team=team, source=source, name="table")
+ job = ExternalDataJob.objects.create(
+ team=team, pipeline=source, schema=schema, status=ExternalDataJob.Status.RUNNING, rows_synced=0
+ )
+
+ env = ActivityEnvironment()
+ inputs = UpdateExternalDataJobStatusInputs(
+ team_id=team.id,
+ job_id=str(job.id),
+ schema_id=str(schema.id),
+ source_id=str(source.id),
+ status=ExternalDataJob.Status.FAILED,
+ internal_error=internal_error,
+ latest_error=internal_error,
+ )
+
+ with (
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.external_data_job.get_rows", return_value=0
+ ),
+ mock.patch("products.warehouse_sources.backend.temporal.data_imports.external_data_job.finish_row_tracking"),
+ mock.patch("posthoganalytics.capture"),
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.external_data_job.update_should_sync"
+ ) as mock_update_should_sync,
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.external_data_job.update_external_job_status"
+ ) as mock_update_job_status,
+ ):
+ asyncio.run(env.run(update_external_data_job_model, inputs))
+
+ mock_update_should_sync.assert_not_called()
+ assert mock_update_job_status.call_args.kwargs["latest_error"] == expected_message