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(warehouse): reopen mysql streaming connection dropped during pre-stream setup (#72118)

Tom Owers14ccfc940fec
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 41d4e7a3414b..bc16b4df21e6 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
@@ -1352,6 +1352,23 @@ class MySQLImplementation(SQLSourceImplementation[MySQLSourceConfig, pymysql.Con
# state for the streaming execute() below.
self.explain_query(ss_cursor, query, args, logger)
+ # The best-effort preamble above (the session-timeout SET and the diagnostic
+ # EXPLAIN) runs on this streaming connection. A transient drop during either
+ # force-closes the socket silently, which would leave the streaming execute
+ # below raising an opaque `InterfaceError(0, '')` — pymysql's signal that the
+ # socket is already gone — that masks the recoverable lost connection. Reopen
+ # once so the real query runs on a live socket instead of failing the attempt
+ # on a blip a fresh connection recovers from.
+ if not streaming_connection.open:
+ logger.warning(
+ "MySQL connection dropped during pre-stream setup; reopening before streaming query"
+ )
+ # Detach the cursor bound to the dead socket first so its later teardown
+ # can't drain the freshly reopened connection (see _release_streaming_cursor).
+ _release_streaming_cursor(ss_cursor)
+ streaming_connection.connect()
+ ss_cursor = streaming_connection.cursor(SSCursor)
+
ss_cursor.execute(query, args)
column_names = [column[0] for column in ss_cursor.description or []]
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 1f35a01ab92a..417907801e77 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
@@ -842,6 +842,33 @@ class TestStreamingCursorTeardown:
assert ss_cursor.connection is None
+class TestStreamingReopensDroppedConnection:
+ """A transient drop during the best-effort pre-stream setup (SET SESSION / EXPLAIN)
+ force-closes the socket. The streaming query must reopen it, otherwise the execute
+ runs on a dead socket and surfaces pymysql's opaque, unactionable
+ `InterfaceError(0, '')` instead of recovering from the blip."""
+
+ def test_reopens_when_preamble_dropped_the_connection(self, build_pipeline_mocks):
+ mock_connect, _, ss_cursor = build_pipeline_mocks
+ mock_connection = mock_connect.return_value
+ mock_connection.open = False
+
+ _drain_source()
+
+ mock_connection.connect.assert_called_once()
+ assert ss_cursor.execute.called
+
+ def test_does_not_reopen_a_live_connection(self, build_pipeline_mocks):
+ mock_connect, _, ss_cursor = build_pipeline_mocks
+ mock_connection = mock_connect.return_value
+ mock_connection.open = True
+
+ _drain_source()
+
+ mock_connection.connect.assert_not_called()
+ assert ss_cursor.execute.called
+
+
class TestIsBadPlanError:
def test_matches_error_2013(self):
assert _is_bad_plan_error(pymysql.err.OperationalError(2013, "Lost connection to MySQL server during query"))