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): coerce connection port to int (#75255)

Tom Owersb12e03db5aaa
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 049062b102ab..abe1d4facade 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
@@ -810,7 +810,9 @@ class MySQLImplementation(SQLSourceImplementation[MySQLSourceConfig, pymysql.Con
with self._ssh_tunnel_endpoint(config) as (host, port):
kwargs: dict[str, Any] = {
"host": host,
- "port": port,
+ # pymysql rejects a non-int port; config.port can arrive as a string when the
+ # config is built directly rather than through the int-coercing from_dict.
+ "port": int(port),
"database": config.database,
"user": config.user,
"password": config.password,
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 8e6a7d240ac0..8e820f1e1f14 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
@@ -2137,3 +2137,30 @@ class TestConnectSSHTunnel:
mock_connect.assert_not_called()
non_retryable = MySQLSource().get_non_retryable_errors()
assert any(pattern in str(exc_info.value) for pattern in non_retryable.keys())
+
+
+class TestConnectPortCoercion:
+ def test_string_port_is_coerced_to_int(self, mocker):
+ # A config built directly (not through the int-coercing `from_dict`) can carry a string
+ # port, which pymysql rejects with `ValueError: port should be of type int`.
+ config = MySQLSourceConfig(
+ host="localhost",
+ port="3306", # type: ignore[arg-type]
+ database="d",
+ user="u",
+ password="p",
+ schema="mydb",
+ using_ssl=False,
+ ssh_tunnel=None,
+ )
+ mock_connect = mocker.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.pymysql.connect",
+ return_value=MagicMock(),
+ )
+
+ with MySQLImplementation().connect(config):
+ pass
+
+ passed_port = mock_connect.call_args.kwargs["port"]
+ assert passed_port == 3306
+ assert isinstance(passed_port, int)