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): widen transient connect-retry window to match postgres (#64740)

Tom Owersb7836de5bdd1
diff --git a/posthog/temporal/data_imports/sources/mysql/mysql.py b/posthog/temporal/data_imports/sources/mysql/mysql.py
index aa8a200ae3f8..863384f222f5 100644
--- a/posthog/temporal/data_imports/sources/mysql/mysql.py
+++ b/posthog/temporal/data_imports/sources/mysql/mysql.py
@@ -282,8 +282,11 @@ def _is_bad_plan_error(e: pymysql.err.OperationalError) -> bool:
return code in (_LOST_CONNECTION_DURING_QUERY_CODE, _OUT_OF_SORT_MEMORY_CODE)
-# Number of times `connect` will open a fresh pymysql connection before giving up.
-_MAX_CONNECT_ATTEMPTS = 3
+# Number of times `connect` will open a fresh pymysql connection before giving up. Matches the
+# Postgres source's `_retry_on_connection_dropped` budget so the in-process window spans the few
+# seconds a real failover / idle cull / tunnel hiccup takes to recover, rather than exhausting in
+# ~3s and surfacing the drop as error-tracking noise.
+_MAX_CONNECT_ATTEMPTS = 5
def _is_transient_connect_drop(e: BaseException) -> bool:
@@ -310,7 +313,7 @@ def _connect_with_transient_retry(kwargs: dict[str, Any]) -> pymysql.Connection:
Mirrors the in-process connect retry the Postgres source uses: a momentary
drop while establishing the connection recovers on a fresh attempt, so retry
- it here with a short backoff instead of failing schema discovery / sync setup
+ it here with a bounded backoff instead of failing schema discovery / sync setup
on the first blip and surfacing it as captured error-tracking noise.
"""
attempt = 0
@@ -327,7 +330,7 @@ def _connect_with_transient_retry(kwargs: dict[str, Any]) -> pymysql.Connection:
max_attempts=_MAX_CONNECT_ATTEMPTS,
exc_info=e,
)
- time.sleep(attempt)
+ time.sleep(min(2 * attempt, 30))
def _release_streaming_cursor(cursor: SSCursor) -> None:
diff --git a/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py b/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py
index da9cf3b42b86..5186c210292b 100644
--- a/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py
+++ b/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py
@@ -875,23 +875,31 @@ class TestIsTransientConnectDrop:
class TestConnectTransientRetry:
- def test_retries_transient_drop_then_succeeds(self, mocker):
+ @pytest.mark.parametrize(
+ "fail_count,expected_sleeps",
+ [
+ # A single blip recovers on the second attempt.
+ (1, [2]),
+ # A drop that recovers on the 4th attempt — past the old 3-attempt window — must still
+ # be absorbed in-process rather than surfacing as error-tracking noise.
+ (3, [2, 4, 6]),
+ ],
+ )
+ def test_retries_transient_drop_then_succeeds(self, mocker, fail_count, expected_sleeps):
sleep = mocker.patch("posthog.temporal.data_imports.sources.mysql.mysql.time.sleep")
conn = MagicMock()
conn.__enter__.return_value = conn
+ drop = pymysql.err.OperationalError(2013, "Lost connection to MySQL server during query")
mock_connect = mocker.patch(
"posthog.temporal.data_imports.sources.mysql.mysql.pymysql.connect",
- side_effect=[
- pymysql.err.OperationalError(2013, "Lost connection to MySQL server during query"),
- conn,
- ],
+ side_effect=[drop] * fail_count + [conn],
)
with MySQLImplementation().connect(_make_config()) as yielded:
assert yielded is conn
- assert mock_connect.call_count == 2
- sleep.assert_called_once_with(1)
+ assert mock_connect.call_count == fail_count + 1
+ assert [c.args[0] for c in sleep.call_args_list] == expected_sleeps
def test_gives_up_after_max_attempts(self, mocker):
mocker.patch("posthog.temporal.data_imports.sources.mysql.mysql.time.sleep")