Repository navigation
SFTP: add async retrieve_file / store_file / mkdir to SFTPHookAsync and introduce SFTPClientPool - #64465
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds new async file-transfer capabilities to SFTPHookAsync and introduces a reusable SFTPClientPool intended to manage and reuse asyncssh SFTP connections, aligning the provider with async usage patterns described in AIP-98.
Changes:
- Added async
retrieve_file(),store_file(),mkdir(), and updated asynclist_directory()implementation inSFTPHookAsync. - Introduced
SFTPClientPool(async connection pooling + context manager) and added initial unit tests for it. - Added
aiofilesas a provider dependency and documented it.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 15 comments.
Show a summary per file
| File | Description |
|---|---|
| providers/sftp/src/airflow/providers/sftp/hooks/sftp.py | Adds new async SFTP file operations and changes async directory listing behavior. |
| providers/sftp/src/airflow/providers/sftp/pools/sftp.py | Introduces the SFTPClientPool singleton/pool implementation. |
| providers/sftp/src/airflow/providers/sftp/pools/init.py | Initializes new pools package (license header only). |
| providers/sftp/tests/conftest.py | Adds a fixture for mocking async SFTP connections. |
| providers/sftp/tests/unit/sftp/hooks/test_sftp.py | Updates async hook tests and adds coverage for new async methods. |
| providers/sftp/tests/unit/sftp/pools/test_sftp.py | Adds initial tests for SFTPClientPool. |
| providers/sftp/tests/unit/sftp/pools/init.py | Initializes new test package (license header only). |
| providers/sftp/pyproject.toml | Adds aiofiles dependency. |
| providers/sftp/docs/index.rst | Documents the new aiofiles dependency. |
…TPHook counterpart and added SFTPClientPool
bb49144 to
5c5d08c
Compare
…which branch is chosen in store_file method
…connections are already in use
potiuk
left a comment
There was a problem hiding this comment.
Thanks for the thorough rework — every blocking item from the prior round is addressed, and the per-event-loop state design (_LoopState + WeakKeyDictionary[loop, _LoopState] + lazy init in _get_loop_state()) is exactly the right shape. The new regression tests in test_pool_works_across_separate_asyncio_run_calls, test_pool_per_loop_state_isolation, and the symlink-cycle / mid-walk disappear coverage in walktree lock in the harder-to-reason-about behaviour cleanly.
LGTM, approving.
A few small follow-ups I noticed on the way through — none are blockers, take them in this PR or a separate one as you prefer:
pools/sftp.py::_create_connectionleaks the SSH connection ifstart_sftp_client()raises after_get_conn()returns. Wrap intry/exceptandssh_conn.close()on failure.pools/sftp.py::_close_connection_paircallsssh.close()without awaitingssh.wait_closed(); matters for long-running processes like the Triggerer where transports can linger.hooks/sftp.py::list_directoryline 945 has a trailingreturn Nonethat is unreachable — both branches already return inside their blocks.tests/hooks/test_sftp.py::test_list_directory_path_does_not_existdoesn't actually exercise theSFTPNoSuchFilebranch; the assertion passes for both[]andNone. Settingreaddir.side_effect = SFTPNoSuchFile(...)and assertingfiles is Nonewould lock in the documented contract.tests/conftest.py— thesftp_hook_mockedfixture annotation claims the second tuple element is anSFTPClient, but it's the async CM returned bystart_sftp_client(). Thefrom airflow… import SFTPHookAsyncon line 38 can be promoted out of the function body.
Drafted-by: Claude Opus 4.7 (1M context); reviewed by @potiuk before posting
After #64465 changed the production code from ``sftp = await ssh_conn.start_sftp_client()`` to ``async with ssh_conn.start_sftp_client() as sftp:`` (and likewise for ``sftp.open()``), the ``sftp_hook_mocked`` fixture in ``providers/sftp/tests/conftest.py`` started returning coroutines from those calls — ``AsyncMock(spec=SSHClientConnection)`` auto-makes ``start_sftp_client`` an ``AsyncMock`` whose call yields a coroutine, not an async context manager — and every ``TestSFTPHookAsync`` test that uses the fixture failed with ``TypeError: 'coroutine' object does not support the asynchronous context manager protocol``. In real asyncssh, both ``start_sftp_client()`` and ``SFTPClient.open()`` are decorated so a call returns an object that is both awaitable and async-context-manageable; the production code only uses the context-manager half. Override the auto-spec'd AsyncMocks with sync MagicMocks that return a context-manager mock with ``__aenter__`` / ``__aexit__`` configured. Restores 28 ``TestSFTPHookAsync`` tests in ``providers/sftp/tests/unit/sftp/hooks/test_sftp.py`` (blocking CI on unrelated PRs since #64465 merged).
This Pr will allow to use same code as show in examples of AIP-98.
Was generative AI tooling used to co-author this PR?
ChatGPT 5 for helping setup unit tests, especially for the SFTPClientPool.
{pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.