This is an automated email from the ASF dual-hosted git repository.

Miretpl pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new bb50310d823 Cache FTPS connections only after setup succeeds (#74183)
bb50310d823 is described below

commit bb50310d82303b767b24fa13e054fbd43896ab77
Author: Andrew Rukin <[email protected]>
AuthorDate: Tue Nov 3 22:50:15 2026 +0300

    Cache FTPS connections only after setup succeeds (#74183)
    
    Cache FTPS clients only after connection setup completes. Close clients on 
setup failure without masking the original exception, and verify that 
subsequent calls create a fresh connection.
    
    Co-authored-by: drewrukin <[email protected]>
    Co-authored-by: Przemysław Mirowski 
<[email protected]>
---
 .../ftp/src/airflow/providers/ftp/hooks/ftp.py     | 17 ++++++++++----
 providers/ftp/tests/unit/ftp/hooks/test_ftp.py     | 27 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/providers/ftp/src/airflow/providers/ftp/hooks/ftp.py 
b/providers/ftp/src/airflow/providers/ftp/hooks/ftp.py
index a63a0b4f608..14f3be0327d 100644
--- a/providers/ftp/src/airflow/providers/ftp/hooks/ftp.py
+++ b/providers/ftp/src/airflow/providers/ftp/hooks/ftp.py
@@ -21,6 +21,7 @@ import datetime
 import ftplib  # nosec: B402
 import logging
 from collections.abc import Callable
+from contextlib import suppress
 from typing import Any, cast
 
 from airflow.providers.common.compat.sdk import BaseHook
@@ -327,13 +328,19 @@ class FTPSHook(FTPHook):
             params.password = cast("str", params.password)
             params.login = cast("str", params.login)
             if encoding:
-                self.conn = ftplib.FTP_TLS(
+                conn = ftplib.FTP_TLS(
                     params.host, params.login, params.password, 
context=context, encoding=encoding
                 )  # nosec: B321
             else:
-                self.conn = ftplib.FTP_TLS(params.host, params.login, 
params.password, context=context)  # nosec: B321
-            self.conn.set_pasv(pasv)
-            # Without prot_p() ftplib transfers file payloads over cleartext 
sockets even though the control connection is TLS.
-            self.conn.prot_p()
+                conn = ftplib.FTP_TLS(params.host, params.login, 
params.password, context=context)  # nosec: B321
+            try:
+                conn.set_pasv(pasv)
+                # Without prot_p() file payloads use cleartext despite TLS on 
the control connection.
+                conn.prot_p()
+            except BaseException:
+                with suppress(Exception):
+                    conn.close()
+                raise
+            self.conn = conn
 
         return self.conn
diff --git a/providers/ftp/tests/unit/ftp/hooks/test_ftp.py 
b/providers/ftp/tests/unit/ftp/hooks/test_ftp.py
index e6be24698be..7f3aefbd932 100644
--- a/providers/ftp/tests/unit/ftp/hooks/test_ftp.py
+++ b/providers/ftp/tests/unit/ftp/hooks/test_ftp.py
@@ -17,6 +17,7 @@
 # under the License.
 from __future__ import annotations
 
+from ftplib import FTP_TLS
 from io import StringIO
 from unittest import mock
 
@@ -262,6 +263,32 @@ class TestIntegrationFTPHook:
         conn.login.assert_called_once_with("user", "pass123")
         conn.set_pasv.assert_called_once_with(True)
 
+    @pytest.mark.parametrize("connection_id", ["ftp_passive", "ftp_encoding"])
+    @pytest.mark.parametrize("failed_step", ["set_pasv", "prot_p"])
+    @pytest.mark.parametrize("cleanup_fails", [False, True])
+    @mock.patch("ftplib.FTP_TLS", autospec=True)
+    def test_ftps_discards_failed_setup(self, mock_ftp, connection_id, 
failed_step, cleanup_fails):
+        failed = mock_ftp.return_value
+        healthy = mock.create_autospec(FTP_TLS, instance=True)
+        mock_ftp.side_effect = [failed, healthy]
+        setup_error = OSError("setup failed")
+        getattr(failed, failed_step).side_effect = setup_error
+        if cleanup_fails:
+            failed.close.side_effect = OSError("cleanup failed")
+        hook = fh.FTPSHook(connection_id)
+
+        with pytest.raises(OSError, match="setup failed") as error:
+            hook.get_conn()
+
+        assert error.value is setup_error
+        assert hook.conn is None
+        failed.close.assert_called_once_with()
+        assert hook.get_conn() is healthy
+        healthy.set_pasv.assert_called_once_with(True)
+        healthy.prot_p.assert_called_once_with()
+        assert hook.get_conn() is healthy
+        assert mock_ftp.call_count == 2
+
     @mock.patch("ftplib.FTP_TLS")
     def test_ftps_passive_mode(self, mock_ftp):
         from airflow.providers.ftp.hooks.ftp import FTPSHook

Reply via email to