Skip to content

Keep APM and sync UploadFile/DownloadFile callbacks on the threadpool#1805

Open
Rob-Hague wants to merge 2 commits into
sshnet:developfrom
Rob-Hague:progressreport
Open

Keep APM and sync UploadFile/DownloadFile callbacks on the threadpool#1805
Rob-Hague wants to merge 2 commits into
sshnet:developfrom
Rob-Hague:progressreport

Conversation

@Rob-Hague

@Rob-Hague Rob-Hague commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Changes to support IProgress<> callback on UploadAsync/DownloadAsync meant wrapping the Action<> callback on existing methods in a Progress<>, which posts the callback onto the current synchronisation context rather than the threadpool. For the legacy APM methods (Begin[..]), let's just preserve their old behaviour.

For the synchronous methods, posting to the synchronisation context is probably the worst choice (because if there is one, the method itself is running there). We can either revert to the threadpool as well, or take the opportunity to invoke the callback synchronously, which is a behavioural change but probably the least surprising behaviour for a synchronous method.

edit: after a nudging from copilot, I remembered that the UploadFile callbacks fire upon receiving sftp status response and on the receiving listener thread, so invoking them synchronously does not have the desired effect. For now just keep the behaviour as in prior releases for all methods.

Changes to support IProgress<> callback on UploadAsync/DownloadAsync meant wrapping
the Action<> callback on existing methods in a Progress<>, which posts the callback
onto the current synchronisation context rather than the threadpool. For the legacy
APM methods (Begin[..]), let's just preserve their old behaviour.

For the synchronous methods, posting to the synchronisation context is probably the
worst choice (because if there is one, the method itself is running there). We can
either revert to the threadpool as well, or take the opportunity to invoke the
callback synchronously, which is a behavioural change but probably the least
surprising behaviour for a synchronous method.

Copilot AI 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.

Pull request overview

Adjusts how SftpClient routes progress callbacks so that legacy APM (Begin*) methods keep their historical “thread pool regardless of SynchronizationContext” behavior, while sync UploadFile/DownloadFile no longer post progress back onto the current synchronization context.

Changes:

  • Replaced Progress<T> wrappers for APM methods with a custom ThreadPoolProgress<T> to avoid SynchronizationContext capture.
  • Changed sync UploadFile/DownloadFile callback wrapping to use a custom SynchronousProgress<T>.
  • Simplified progress reporting sites to report inline via ?.Report(...) with a freshly constructed progress report value.

Comment thread src/Renci.SshNet/SftpClient.cs
Comment thread src/Renci.SshNet/SftpClient.cs Outdated
@Rob-Hague Rob-Hague marked this pull request as draft June 20, 2026 17:49
@Rob-Hague Rob-Hague closed this Jun 20, 2026
We could call the Download callback synchronously easily enough, but the Upload progress
reports are being made on the message listener thread upon request ack. A more involved
scheme could drain callbacks to fire during the read loop. For now just make it all the
same behaviour as in 2025.1.0.
@Rob-Hague Rob-Hague reopened this Jun 23, 2026
@Rob-Hague Rob-Hague changed the title Tweak internal IProgress usage for APM and sync UploadFile/DownloadFile Keep APM and sync UploadFile/DownloadFile callbacks on the threadpool Jun 23, 2026
@Rob-Hague Rob-Hague requested a review from Copilot June 23, 2026 18:16

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

Comment thread src/Renci.SshNet/SftpClient.cs
@Rob-Hague Rob-Hague marked this pull request as ready for review June 23, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants