Tweak internal IProgress usage for APM and sync UploadFile/DownloadFile#1805
Closed
Rob-Hague wants to merge 1 commit into
Closed
Tweak internal IProgress usage for APM and sync UploadFile/DownloadFile#1805Rob-Hague wants to merge 1 commit into
Rob-Hague wants to merge 1 commit 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.
Comment on lines
1094
to
1097
| if (uploadCallback != null) | ||
| { | ||
| uploadProgress = new Progress<UploadFileProgressReport>(r => uploadCallback(r.TotalBytesUploaded)); | ||
| uploadProgress = new SynchronousProgress<UploadFileProgressReport>(r => uploadCallback(r.TotalBytesUploaded)); | ||
| } |
Comment on lines
+2652
to
+2692
| /// <summary> | ||
| /// An <see cref="IProgress{T}"/> implementation that posts callbacks to the threadpool. | ||
| /// </summary> | ||
| private sealed class ThreadPoolProgress<T> : IProgress<T> | ||
| { | ||
| private readonly Action<T> _handler; | ||
|
|
||
| public ThreadPoolProgress(Action<T> handler) | ||
| { | ||
| Debug.Assert(handler != null); | ||
| _handler = handler!; | ||
| } | ||
|
|
||
| void IProgress<T>.Report(T value) | ||
| { | ||
| _ = ThreadPool.QueueUserWorkItem(static state => | ||
| { | ||
| var (handler, value) = ((Action<T>, T))state!; | ||
| handler(value); | ||
| }, | ||
| (_handler, value)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// An <see cref="IProgress{T}"/> implementation that invokes callbacks synchronously. | ||
| /// </summary> | ||
| private sealed class SynchronousProgress<T> : IProgress<T> | ||
| { | ||
| private readonly Action<T> _handler; | ||
|
|
||
| public SynchronousProgress(Action<T> handler) | ||
| { | ||
| Debug.Assert(handler != null); | ||
| _handler = handler!; | ||
| } | ||
|
|
||
| void IProgress<T>.Report(T value) | ||
| { | ||
| _handler.Invoke(value); | ||
| } |
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.