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): stop re-raising passthrough errors as their own cause (#97595)

Andy Zhaoe5cf3a6ae2d7
diff --git a/ee/clickhouse/test/test_error.py b/ee/clickhouse/test/test_error.py
index 7f88aa7b1934..bd5cdfd523d2 100644
--- a/ee/clickhouse/test/test_error.py
+++ b/ee/clickhouse/test/test_error.py
@@ -1,4 +1,5 @@
import pytest
+from unittest.mock import MagicMock
from clickhouse_driver.errors import NetworkError, ServerException, SocketTimeoutError
@@ -272,3 +273,16 @@ def test_memory_limit_wraps_by_which_ceiling_was_hit(message, expected_per_query
)
def test_failing_to_open_a_clickhouse_connection_is_transient(error):
assert isinstance(wrap_clickhouse_query_error(error), CH_TRANSIENT_ERRORS)
+
+
+def test_a_driver_error_the_wrapper_passes_through_is_not_its_own_cause():
+ error = SocketTimeoutError("(clickhouse.example.com:9440)")
+ client = MagicMock()
+ client.__enter__.return_value = client
+ client.execute.side_effect = error
+
+ with pytest.raises(SocketTimeoutError) as raised:
+ sync_execute("SELECT 1", sync_client=client)
+
+ assert raised.value is error
+ assert raised.value.__cause__ is None
diff --git a/posthog/clickhouse/client/execute.py b/posthog/clickhouse/client/execute.py
index cc6c1c998cb0..d118a56a554c 100644
--- a/posthog/clickhouse/client/execute.py
+++ b/posthog/clickhouse/client/execute.py
@@ -577,6 +577,10 @@ def sync_execute(
chargeable=str(tags.chargeable or "0"),
).inc()
err = wrap_clickhouse_query_error(e)
+ # The wrapper returns the same object for anything that is not a ServerException. Raising
+ # that with `from e` makes the exception its own __cause__.
+ if err is e:
+ raise
raise err from e
finally:
execution_time = perf_counter() - start_time