Keep APM and sync UploadFile/DownloadFile callbacks on the threadpool#1805
Open
Rob-Hague wants to merge 2 commits into
Open
Keep APM and sync UploadFile/DownloadFile callbacks on the threadpool#1805Rob-Hague wants to merge 2 commits into
Rob-Hague wants to merge 2 commits into
Conversation
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.
Contributor
There was a problem hiding this comment.
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 customThreadPoolProgress<T>to avoidSynchronizationContextcapture. - Changed sync
UploadFile/DownloadFilecallback wrapping to use a customSynchronousProgress<T>. - Simplified progress reporting sites to report inline via
?.Report(...)with a freshly constructed progress report value.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.