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 Vitess vtgate dial-timeout on connect (#67616)

Tom Owers3c7d1d34afec
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 ba032cb78887..52a2d6afa71b 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
@@ -398,6 +398,27 @@ 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
+# 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`).
+_VITESS_DIAL_TOKEN = "dial tcp"
+_VITESS_DIAL_TIMEOUT_TOKEN = "connection timed out"
+
+
+def _is_transient_vitess_dial_timeout(e: BaseException) -> bool:
+ """Return True if a Vitess vtgate timed out dialing its backend tablet — a transient blip."""
+ if not isinstance(e, pymysql.err.OperationalError):
+ return False
+ args_text = " ".join(str(arg) for arg in e.args)
+ return _VITESS_DIAL_TOKEN in args_text and _VITESS_DIAL_TIMEOUT_TOKEN in args_text
+
+
def _connect_with_transient_retry(kwargs: dict[str, Any]) -> pymysql.Connection:
"""Open a pymysql connection, retrying a transient drop or timeout on connect.
@@ -416,6 +437,7 @@ def _connect_with_transient_retry(kwargs: dict[str, Any]) -> pymysql.Connection:
_is_transient_connect_drop(e)
or _is_transient_connect_timeout(e)
or _is_transient_packet_sequence_error(e)
+ or _is_transient_vitess_dial_timeout(e)
):
raise
structlog.get_logger().warning(
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 09a407f73e4e..73a18b0a8ceb 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
@@ -28,6 +28,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysq
_is_transient_connect_timeout,
_is_transient_packet_sequence_error,
_is_transient_tablet_unavailable,
+ _is_transient_vitess_dial_timeout,
_release_streaming_cursor,
_retry_on_transient_tablet_unavailable,
_safe_convert_date,
@@ -961,6 +962,42 @@ class TestIsTransientPacketSequenceError:
assert not _is_transient_packet_sequence_error(pymysql.err.InternalError())
+class TestIsTransientVitessDialTimeout:
+ @pytest.mark.parametrize(
+ "message",
+ [
+ # The shape a Vitess/PlanetScale vtgate surfaces at connect time when it can't dial the
+ # backend tablet in time — the tablet address, attempt count, and reqid all vary, the
+ # Go `dial tcp ... connection timed out` signature is the stable signal.
+ "internal connection error: dial tcp 10.0.0.1:8083: connect: connection timed out, "
+ "after 1 attempts, reqid=csYTzBMNB2hB8111yzcg4A",
+ "internal connection error: dial tcp 192.0.2.5:3306: connect: connection timed out",
+ ],
+ )
+ def test_matches_dial_timeout(self, message):
+ assert _is_transient_vitess_dial_timeout(pymysql.err.OperationalError(1815, message))
+
+ @pytest.mark.parametrize(
+ "code,message",
+ [
+ # A refused dial returns immediately and is more often a persistent config problem, so
+ # it must not be absorbed here — only the timeout flavour above is treated as transient.
+ (1815, "internal connection error: dial tcp 10.0.0.1:8083: connect: connection refused"),
+ # Config/credential errors stay untouched, even when they mention a timeout.
+ (2003, "Can't connect to MySQL server on 'db.example.com' (timed out)"),
+ (1045, "Access denied for user"),
+ ],
+ )
+ def test_does_not_match_non_dial_timeout_errors(self, code, message):
+ assert not _is_transient_vitess_dial_timeout(pymysql.err.OperationalError(code, message))
+
+ def test_does_not_match_error_without_args(self):
+ assert not _is_transient_vitess_dial_timeout(pymysql.err.OperationalError())
+
+ def test_does_not_match_non_operational_error(self):
+ assert not _is_transient_vitess_dial_timeout(ValueError("dial tcp 10.0.0.1: connection timed out"))
+
+
class TestConnectTransientRetry:
@pytest.mark.parametrize(
"fail_count,expected_sleeps",
@@ -1026,6 +1063,28 @@ class TestConnectTransientRetry:
assert mock_connect.call_count == 2
sleep.assert_called_once_with(2)
+ def test_retries_vitess_dial_timeout_then_succeeds(self, mocker):
+ sleep = mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.time.sleep")
+ conn = MagicMock()
+ conn.__enter__.return_value = conn
+ mock_connect = mocker.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.pymysql.connect",
+ side_effect=[
+ pymysql.err.OperationalError(
+ 1815,
+ "internal connection error: dial tcp 10.0.0.1:8083: connect: connection timed out, "
+ "after 1 attempts, reqid=csYTzBMNB2hB8111yzcg4A",
+ ),
+ conn,
+ ],
+ )
+
+ with MySQLImplementation().connect(_make_config()) as yielded:
+ assert yielded is conn
+
+ assert mock_connect.call_count == 2
+ sleep.assert_called_once_with(2)
+
def test_retries_packet_sequence_error_then_succeeds(self, mocker):
sleep = mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.time.sleep")
conn = MagicMock()