Skip to content

Fix concurrent SFTP directory transfers dropping parent hook's connection overrides - #73647

Merged
potiuk merged 8 commits into
apache:mainfrom
abhishekmauryaKsolves:fix-sftp-concurrent-connection-overrides
Oct 5, 2026
Merged

potiuk merged 8 commits into
apache:mainfrom
abhishekmauryaKsolves:fix-sftp-concurrent-connection-overrides

Conversation

@abhishekmauryaKsolves

Copy link
Copy Markdown
Contributor

Closes #73585

What

SFTPHook.store_directory_concurrently() and retrieve_directory_concurrently() built each worker hook as SFTPHook(ssh_conn_id=self.ssh_conn_id), discarding every other constructor override on the parent hook — remote_host, port, username, password, key_file, proxy settings, timeouts, and no_host_key_check. Workers fell back to whatever the underlying connection defined instead of the parent hook's effective, already-resolved settings.

With an explicit remote_host set on the parent hook, for example, the directory listing happened against that host but the worker transfers ran against the connection's original host.

How

Added SFTPHook._build_worker_hook(), a helper that constructs a new SFTPHook from the parent hook's effective connection settings (constructor overrides merged with the connection), and used it in both store_directory_concurrently() and retrieve_directory_concurrently() instead of re-constructing a bare SFTPHook(ssh_conn_id=...) per worker.

Testing

Added two regression tests in test_sftp.py:

  • test_build_worker_hook_inherits_parent_overrides — unit-tests _build_worker_hook() directly, asserting it copies remote_host, port, username, password, key_file, conn_timeout, host_proxy_cmd, no_host_key_check, allow_host_key_change, and look_for_keys.
  • test_store_and_retrieve_directory_concurrently_use_parent_overrides — patches _build_worker_hook and asserts every worker hook built during a concurrent store/retrieve transfer carries the parent's effective remote_host/port/username.

Full providers/sftp unit test suite passes locally (127 passed).


…ransfer workers

SFTPHook.store_directory_concurrently() and retrieve_directory_concurrently()
built each worker hook as SFTPHook(ssh_conn_id=self.ssh_conn_id), discarding
every other constructor override on the parent hook (remote_host, port,
username, password, key_file, proxy settings, timeouts, no_host_key_check,
etc.). Workers fell back to the connection's raw defaults instead of the
parent hook's effective, already-resolved settings.

Add SFTPHook._build_worker_hook(), which builds a worker hook from the
parent's effective connection settings, and use it in both
store_directory_concurrently() and retrieve_directory_concurrently().

Closes apache#73585
@abhishekmauryaKsolves abhishekmauryaKsolves changed the title fix(sftp): propagate parent hook connection overrides to concurrent-transfer workers Fix concurrent SFTP directory transfers dropping parent hook's connection overrides Sep 24, 2026

@rjgoyln rjgoyln left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall approach looks good to me. I just have one potentially blocking concern below: re-passing the parent hook's already-resolved key_file to the worker constructor can cause _build_worker_hook() to raise in cases where the previous construction succeeded.

I also left two non-blocking comments on the tests — one on the assertions in the second regression test, and one on the mock spec.

Just my thoughts, and please feel free to resolve any of these if you think they're not applicable.


Drafted-by: Claude Code (Opus 5); reviewed by @rjgoyln before posting

abhishekmauryaKsolves added 2 commits September 29, 2026 09:41
- Re-resolve key_file when pkey is set on the worker hook, avoiding the
  key_file/private_key guard in SSHHook that could make worker construction
  raise even though the parent hook succeeded.
- Use MagicMock(spec=Connection) in test_build_worker_hook_inherits_parent_overrides
  so the mock stays honest against the real Connection interface.
- Simplify test_store_and_retrieve_directory_concurrently_use_parent_overrides
  to a wiring test (assert _build_worker_hook is called once per worker);
  value-level assertions are already covered by
  test_build_worker_hook_inherits_parent_overrides.
…verrides' into fix-sftp-concurrent-connection-overrides
@abhishekmauryaKsolves

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed all three points in 4fec830:

  1. key_file/pkey guard — Fixed. key_file is now None if self.pkey else self.key_file on the worker hook, so the guard in SSHHook doesn't trip when a private_key extra has already resolved to pkey on the parent.

  2. Mock spec — Updated to MagicMock(spec=Connection) in test_build_worker_hook_inherits_parent_overrides.

  3. Weak assertions — Simplified test_store_and_retrieve_directory_concurrently_use_parent_overrides to a wiring test that just asserts _build_worker_hook is called once per worker; value-level checks are already covered by test_build_worker_hook_inherits_parent_overrides.

All 127 tests pass locally.

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Worker hooks for concurrent transfers now get the parent hook's resolved settings (keys, host-key policy, proxy, username) instead of re-reading only the connection, so every transfer uses the same authentication and host verification as the parent. The regression tests cover both store and retrieve. Thanks!


Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting

@potiuk
potiuk merged commit dbee3c6 into apache:main Oct 5, 2026
83 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent SFTP directory transfers drop the parent hook's connection overrides

3 participants