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(snowflake): stop the query timebomb cancelling long syncs (#70271)

Tom Owers373e264b5ad4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/snowflake/snowflake.py b/products/warehouse_sources/backend/temporal/data_imports/sources/snowflake/snowflake.py
index 2bf6547c7c0c..1a875b62ce02 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/snowflake/snowflake.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/snowflake/snowflake.py
@@ -87,12 +87,21 @@ SNOWFLAKE_SYSTEM_SCHEMA = "INFORMATION_SCHEMA"
# thread. The sync activities are threaded and the heartbeater runs on a separate event-loop task,
# so the stall isn't surfaced by a missed heartbeat — the activity just runs until Temporal's
# `start_to_close_timeout` cancels the thread mid socket-read, surfacing a misleading
-# `WantReadError`/`CancelledError`. Bounding it turns the stall into a fast, retryable
-# `OperationalError` well before the activity is cancelled. It applies per request, so it never caps
-# a long-running streaming sync. Kept comfortably under the schema-discovery activity's 10-minute
-# `start_to_close_timeout` while leaving ample room for legitimate per-request round-trips and retries.
+# `WantReadError`/`CancelledError`. Bounding it turns the stall into a fast, retryable error well
+# before the activity is cancelled. It applies per HTTP request, so it never caps the total wall-clock
+# of a long-running query.
_SNOWFLAKE_NETWORK_TIMEOUT_SECONDS = 300
+# The connector also reuses `network_timeout` as a client-side query "timebomb": it arms a timer on
+# `cursor.execute()` and cancels the running query once the timeout elapses, raising
+# `ProgrammingError` 000604 (57014, "SQL execution was cancelled by the client due to a timeout").
+# That's harmless for the short metadata queries, but it must not cancel a legitimately long
+# full-table data scan — so those pass this explicit, much larger per-query `timeout`, overriding the
+# timebomb while leaving the retry budget above intact. It mirrors the import activity's 6h
+# `start_to_close_timeout`, which is the real ceiling: the connector defers to Temporal rather than
+# pre-empting a sync that is still making progress.
+_SNOWFLAKE_QUERY_TIMEOUT_SECONDS = 6 * 60 * 60
+
def _split_display_name(display_name: str, default_schema: Optional[str]) -> tuple[Optional[str], str]:
"""Split a `schema.table` display name into `(schema, table)`.
@@ -532,7 +541,7 @@ class SnowflakeImplementation(
try:
query = f"SELECT COUNT(*) FROM ({inner_query}) as t"
- cursor.execute(query, inner_query_args)
+ cursor.execute(query, inner_query_args, timeout=_SNOWFLAKE_QUERY_TIMEOUT_SECONDS)
row = cursor.fetchone()
if row is None:
@@ -611,7 +620,7 @@ class SnowflakeImplementation(
row_filters=row_filters,
)
logger.debug(f"Snowflake query: {query.format(params)}")
- streaming_cursor.execute(query, params)
+ streaming_cursor.execute(query, params, timeout=_SNOWFLAKE_QUERY_TIMEOUT_SECONDS)
# We cant control the batch size from snowflake when using the arrow function
# https://github.com/snowflakedb/snowflake-connector-python/issues/1712
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/snowflake/tests/test_snowflake.py b/products/warehouse_sources/backend/temporal/data_imports/sources/snowflake/tests/test_snowflake.py
index c2470c98a5e7..76ba2c79d32f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/snowflake/tests/test_snowflake.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/snowflake/tests/test_snowflake.py
@@ -18,6 +18,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_
from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.postgres import source_requires_ssl
from products.warehouse_sources.backend.temporal.data_imports.sources.snowflake.snowflake import (
_SNOWFLAKE_NETWORK_TIMEOUT_SECONDS,
+ _SNOWFLAKE_QUERY_TIMEOUT_SECONDS,
SnowflakeImplementation,
_build_query,
_parse_clustering_key_leading_column,
@@ -561,6 +562,9 @@ class TestGetRowsToSync:
def test_returns_count(self, impl, cursor, logger):
cursor.fetchone.return_value = (321,)
assert impl.get_rows_to_sync(cursor, "SELECT 1", (), logger) == 321
+ # The COUNT(*) probe scans the same table, so it carries the same long per-query timeout —
+ # otherwise a large table trips the connector timebomb and the probe spuriously logs a 604.
+ assert cursor.execute.call_args.kwargs["timeout"] == _SNOWFLAKE_QUERY_TIMEOUT_SECONDS
def test_returns_zero_on_exception(self, impl, cursor, logger):
# Sync must never bail because the COUNT(*) probe failed
@@ -605,6 +609,11 @@ class TestBuildPipeline:
assert list(response.items()) == [b"batch-1", b"batch-2"]
# Pin a single timestamp unit so mixed ns/us batches don't break pyarrow assembly.
streaming_cursor.fetch_arrow_batches.assert_called_once_with(force_microsecond_precision=True)
+ # The streaming scan must run with a per-query timeout well above `network_timeout`, or the
+ # connector's timebomb cancels a legitimately long full-table sync with ProgrammingError
+ # 000604/57014. Guards the regression that reused the 300s `network_timeout` as the cap.
+ assert streaming_cursor.execute.call_args.kwargs["timeout"] == _SNOWFLAKE_QUERY_TIMEOUT_SECONDS
+ assert _SNOWFLAKE_QUERY_TIMEOUT_SECONDS > _SNOWFLAKE_NETWORK_TIMEOUT_SECONDS
def test_multi_schema_row_routes_to_qualified_namespace(self, impl):
# A blank-namespace source pins each row's schema via the dotted schema_name.