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: treat driver connect failures as transient (#96884)

Andy Zhao415682532f04
diff --git a/ee/clickhouse/test/test_error.py b/ee/clickhouse/test/test_error.py
index 0d23dde3959d..7f88aa7b1934 100644
--- a/ee/clickhouse/test/test_error.py
+++ b/ee/clickhouse/test/test_error.py
@@ -1,6 +1,6 @@
import pytest
-from clickhouse_driver.errors import ServerException
+from clickhouse_driver.errors import NetworkError, ServerException, SocketTimeoutError
from posthog.clickhouse.client import sync_execute
from posthog.errors import (
@@ -261,3 +261,14 @@ def test_memory_limit_wraps_by_which_ceiling_was_hit(message, expected_per_query
if is_cluster:
assert isinstance(wrapped, CH_TRANSIENT_ERRORS)
assert classify_query_error(wrapped) == QueryErrorCategory.RATE_LIMITED
+
+
+ "error",
+ [
+ SocketTimeoutError("(clickhouse.example.com:9440)"),
+ NetworkError("Connection refused (clickhouse.example.com:9440)"),
+ ],
+)
+def test_failing_to_open_a_clickhouse_connection_is_transient(error):
+ assert isinstance(wrap_clickhouse_query_error(error), CH_TRANSIENT_ERRORS)
diff --git a/posthog/errors.py b/posthog/errors.py
index 20de3ba3dfca..dc7d5455a93a 100644
--- a/posthog/errors.py
+++ b/posthog/errors.py
@@ -3,7 +3,7 @@ from dataclasses import dataclass
from enum import StrEnum
from typing import Optional
-from clickhouse_driver.errors import ServerException
+from clickhouse_driver.errors import NetworkError, ServerException, SocketTimeoutError
from posthog.hogql.errors import ExposedHogQLError
@@ -1047,10 +1047,17 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
# CHQueryErrorQueryWasCancelled (394) is deliberately absent: a deploy cancelling in-flight queries
# and an operator or user deliberately killing one are indistinguishable at this layer, so callers
# that want the deploy case retried opt in themselves (see COHORT_RECALCULATION_TRANSIENT_ERRORS).
+# The two clickhouse_driver classes are raised only while a connection is being opened (connect, or
+# the ping-then-reconnect on a stale pooled socket), before any query is sent, so nothing has run and
+# a retry is safe. They are not ServerExceptions, so wrap_clickhouse_query_error passes them through
+# untouched: a bare "Code: 209. (host:9440)" is the driver's 10s connect_timeout firing, typically
+# because a node dropped out of the cluster's load balancer for a few seconds.
CH_TRANSIENT_ERRORS = (
CHQueryErrorS3Error,
CHQueryErrorS3FileChangedDuringRead,
CHQueryErrorTableIsReadOnly,
ClickHouseAtCapacity,
ClickHouseClusterMemoryLimitExceeded,
+ NetworkError,
+ SocketTimeoutError,
)
diff --git a/posthog/temporal/tests/test_alerts_activities.py b/posthog/temporal/tests/test_alerts_activities.py
index 634f221f4cc8..35acaf6a794b 100644
--- a/posthog/temporal/tests/test_alerts_activities.py
+++ b/posthog/temporal/tests/test_alerts_activities.py
@@ -8,6 +8,7 @@ from unittest.mock import patch
import pytest_asyncio
from asgiref.sync import sync_to_async
+from clickhouse_driver.errors import NetworkError, SocketTimeoutError
from temporalio.exceptions import ApplicationError
from temporalio.testing import ActivityEnvironment
@@ -491,7 +492,7 @@ class TestEvaluateAlert:
# an error instead sends the alert silent until its next cadence slot, an hour for hourly ones.
@pytest.mark.parametrize(
"error_class",
- [ClickHouseAtCapacity, ClickHouseClusterMemoryLimitExceeded],
+ [ClickHouseAtCapacity, ClickHouseClusterMemoryLimitExceeded, SocketTimeoutError, NetworkError],
)
async def test_evaluate_reraises_ch_transient_error(self, alert, error_class) -> None:
with patch(