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(mysql): retry a vtgate dial timeout hit during metadata discovery (#95645)

Tom Owersc8a57675a9f2
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
index 8c25bb090537..bd806c552ebc 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
@@ -528,15 +528,17 @@ def _is_transient_packet_sequence_error(e: BaseException) -> bool:
return any(_PACKET_SEQUENCE_ERROR_PHRASE in str(arg) for arg in e.args)
-# Vitess/PlanetScale vtgate surfaces a backend tablet it can't reach at connect time as pymysql
+# Vitess/PlanetScale vtgate surfaces a backend tablet it can't reach as pymysql
# OperationalError(1815, 'internal connection error: dial tcp <addr>: connect: connection timed
# out, after N attempts, reqid=...'): the vtgate handshake succeeds but dialing the tablet behind
# it times out — a failover, a restart, or a momentary network blip that a fresh attempt recovers
# from. 1815 is MySQL's generic ER_INTERNAL_ERROR, so key on the Go-network `dial tcp` +
# `connection timed out` signature (no plain MySQL error carries the `dial tcp` token) rather than
-# the bare code; the volatile tablet address, attempt count, and reqid stay untouched. This is the
-# connect-time sibling of the `code = Unavailable` tablet-unavailable case, which instead lands on
-# the first query after connect (see `_is_transient_tablet_unavailable`).
+# the bare code; the volatile tablet address, attempt count, and reqid stay untouched. Like the
+# `code = Unavailable` tablet-unavailable case (see `_is_transient_tablet_unavailable`), this can
+# land either at connect time or on the first query against a freshly opened connection (e.g.
+# `get_table_metadata`'s information_schema lookup), so both `_connect_with_transient_retry` and
+# `_retry_on_transient_tablet_unavailable` check it.
_VITESS_DIAL_TOKEN = "dial tcp"
_VITESS_DIAL_TIMEOUT_TOKEN = "connection timed out"
@@ -712,8 +714,8 @@ def _retry_on_transient_tablet_unavailable(
whole operation (which reopens the connection) with a bounded backoff instead of
failing sync setup on the first blip and surfacing it as captured error-tracking
noise. Non-transient errors re-raise immediately — the predicates only match the gRPC
- `Unavailable` status, a mid-reparent primary, or a plain peer-reset connection drop,
- all self-healing.
+ `Unavailable` status, a mid-reparent primary, a dial-timeout reaching a backend tablet,
+ a TiProxy failover, or a plain peer-reset connection drop, all self-healing.
"""
attempt = 0
while True:
@@ -724,6 +726,7 @@ def _retry_on_transient_tablet_unavailable(
if attempt >= max_attempts or not (
_is_transient_tablet_unavailable(e)
or _is_transient_vitess_reparent(e)
+ or _is_transient_vitess_dial_timeout(e)
or _is_transient_tiproxy_unavailable(e)
or _is_transient_metadata_query_reset(e)
):
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
index dd197438ff06..f8566ff008e0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
@@ -1753,6 +1753,22 @@ class TestRetryOnTransientTabletUnavailable:
assert operation.call_count == 2
+ def test_retries_vitess_dial_timeout_then_succeeds(self, mocker):
+ # A vtgate dial timeout to a backend tablet (error 1815) can land on a metadata query
+ # against an already-open connection, not just at connect time — `connect()` succeeding
+ # doesn't retry it, so this wrapper must.
+ mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.time.sleep")
+ dial_timeout = pymysql.err.OperationalError(
+ 1815,
+ "internal connection error: dial tcp 10.0.0.1:8083: connect: connection timed out, "
+ "after 1 attempts, reqid=csYTzBMNB2hB8111yzcg4A",
+ )
+ operation = MagicMock(side_effect=[dial_timeout, "ok"])
+
+ assert _retry_on_transient_tablet_unavailable(operation, MagicMock()) == "ok"
+
+ assert operation.call_count == 2
+
def test_gives_up_after_max_attempts(self, mocker):
mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.time.sleep")
operation = MagicMock(side_effect=self._unavailable())