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(clickhouse): handle non-ClickHouse HTTP responses at connect (#74296)

Tom Owers48228041da14
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
index 18e3714aea87..21b69b65dc57 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
@@ -77,6 +77,22 @@ class ClickHouseConnectionError(Exception):
pass
+# clickhouse-connect probes the server with `SELECT version(), timezone()` while
+# constructing the client and unpacks the reply into exactly two tab-separated
+# values (BaseClient._init_common_settings). A host that answers 2xx with a body
+# that isn't a ClickHouse response — a proxy/load-balancer landing page, or a
+# different service listening on the host/port — splits into a different shape, so
+# the driver raises a bare `ValueError` ("too many values to unpack") before we ever
+# run a query. The endpoint isn't serving the ClickHouse HTTP interface, so a retry
+# replays the identical failure; we surface it as a connection error rather than
+# leaking the cryptic ValueError.
+NOT_A_CLICKHOUSE_HTTP_RESPONSE = (
+ "The host answered but did not return a valid ClickHouse response, so it isn't serving the "
+ "ClickHouse HTTP interface on that host/port. Check the host, port, and HTTPS setting "
+ "(and any tunnel or proxy in front of it)."
+)
+
+
def _quote_identifier(identifier: str) -> str:
"""Quote a ClickHouse identifier with backticks.
@@ -256,6 +272,11 @@ def _get_client(
time.sleep(wait)
continue
raise ClickHouseConnectionError(message) from e
+ except ValueError as e:
+ # The construction-time server probe got a response it couldn't parse as a
+ # ClickHouse handshake (see NOT_A_CLICKHOUSE_HTTP_RESPONSE). Deterministic, so
+ # never retryable — don't spend the transient-retry budget on it.
+ raise ClickHouseConnectionError(NOT_A_CLICKHOUSE_HTTP_RESPONSE) from e
# Apply tuning settings after connect, not at construction, so a readonly
# source profile that rejects one degrades to the server default instead
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py
index 0430d6e4e9b7..d74cc5e47a4f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py
@@ -20,6 +20,7 @@ from posthog.schema import (
from posthog.exceptions_capture import capture_exception
from products.warehouse_sources.backend.temporal.data_imports.sources.clickhouse.clickhouse import (
+ NOT_A_CLICKHOUSE_HTTP_RESPONSE,
ClickHouseConnectionError,
_get_client,
clickhouse_source,
@@ -85,6 +86,9 @@ ClickHouseErrors: dict[str, str] = {
# host/port (wrong port, a proxy, or a native-protocol port). Same wording
# as the sync-time non-retryable handling.
"returned response code 404": "We reached your ClickHouse host but it returned a 404, so it isn't serving the ClickHouse HTTP interface on that host/port. Please check the host, port, and HTTPS setting (and any tunnel or proxy in front of it).",
+ # `_get_client` raises this when the host answers 2xx with a body that isn't a
+ # ClickHouse response (a proxy/LB page, or a different service on the host/port).
+ "did not return a valid clickhouse response": NOT_A_CLICKHOUSE_HTTP_RESPONSE,
"returned response code 429": _TEMPORARILY_UNAVAILABLE,
"returned response code 502": _TEMPORARILY_UNAVAILABLE,
"returned response code 503": _TEMPORARILY_UNAVAILABLE,
@@ -223,6 +227,12 @@ class ClickHouseSource(SimpleSource[ClickHouseSourceConfig], SSHTunnelMixin, Val
# answers queries with 404, so retrying can't recover. We match only
# 404, not transient gateway codes (502/503/504), which stay retryable.
"returned response code 404": "We reached your ClickHouse host but it returned a 404, so it isn't serving the ClickHouse HTTP interface on that host/port. Please check the host, port, and HTTPS setting (and any tunnel or proxy in front of it).",
+ # `_get_client` wraps the driver's construction-time probe failure ("too many
+ # values to unpack") into this message when the host answers 2xx with a body that
+ # isn't a ClickHouse response. The endpoint isn't serving the ClickHouse HTTP
+ # interface, so retrying replays the identical failure. We match the stable phrase
+ # from the wrapped message, not the volatile per-request URL or host.
+ "did not return a valid ClickHouse response": NOT_A_CLICKHOUSE_HTTP_RESPONSE,
# MEMORY_LIMIT_EXCEEDED — the source ClickHouse server (per-query
# `max_memory_usage` budget or a server-wide OvercommitTracker kill)
# ran out of memory running our extraction query. We already stream
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
index 4597a64515e8..5bc4e65312aa 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
@@ -12,6 +12,7 @@ from products.warehouse_sources.backend.models.external_data_source import Exter
from products.warehouse_sources.backend.temporal.data_imports.sources.clickhouse import clickhouse as ch_module
from products.warehouse_sources.backend.temporal.data_imports.sources.clickhouse.clickhouse import (
_MAX_CONNECT_ATTEMPTS,
+ NOT_A_CLICKHOUSE_HTTP_RESPONSE,
YIELD_TARGET_ROWS,
ClickHouseColumn,
ClickHouseConnectionError,
@@ -669,6 +670,9 @@ class TestClickHouseSourceNonRetryableErrors:
# SSH tunnel to the customer's bastion couldn't be brought up — the import path only
# checks this per-source dict, so the entry must be here to stop it retrying forever.
"Could not establish session to SSH gateway",
+ # Host answered 2xx with a non-ClickHouse body — `_get_client` wraps the driver's
+ # "too many values to unpack" probe error into this message.
+ NOT_A_CLICKHOUSE_HTTP_RESPONSE,
],
)
def test_permanent_errors_are_non_retryable(self, source, error_msg):
@@ -861,6 +865,20 @@ class TestGetClientTransientRetry:
assert mock_get_client.call_count == 1
mock_sleep.assert_not_called()
+ def test_wraps_non_clickhouse_response_probe_error(self):
+ # clickhouse-connect's construction-time probe unpacks the reply into two values;
+ # a non-ClickHouse 2xx body raises a bare ValueError. We wrap it as a connection
+ # error (deterministic, no retry) instead of leaking the cryptic ValueError.
+ probe_error = ValueError("too many values to unpack (expected 2)")
+ with (
+ patch.object(ch_module.time, "sleep") as mock_sleep,
+ patch.object(ch_module, "get_client", side_effect=probe_error) as mock_get_client,
+ ):
+ with pytest.raises(ClickHouseConnectionError, match="did not return a valid ClickHouse response"):
+ self._connect()
+ assert mock_get_client.call_count == 1
+ mock_sleep.assert_not_called()
+
class TestGetClientSessionSettings:
"""`_get_client` applies tuning settings after connect, tolerating a
@@ -917,7 +935,11 @@ class TestTranslateError:
translated = ClickHouseSource._translate_error(msg)
assert translated is not None
assert "404" in translated
- assert translated != GENERIC_CONNECTION_ERROR
+
+ def test_non_clickhouse_response_maps_to_actionable_message(self):
+ # The wrapped probe error must surface the actionable "not serving ClickHouse"
+ # message during onboarding, not fall through to the generic one.
+ assert ClickHouseSource._translate_error(NOT_A_CLICKHOUSE_HTTP_RESPONSE) == NOT_A_CLICKHOUSE_HTTP_RESPONSE
@pytest.mark.parametrize("code", ["429", "502", "503", "504"])
def test_transient_gateway_responses_ask_to_retry(self, code):