Fix deadlock in SftpClient.UploadFile upon error #1643
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.
When the server returns an error during UploadFile, unless the response is quick enough that the exception is captured before SftpSession.RequestWrite finishes...
SSH.NET/src/Renci.SshNet/Sftp/SftpSession.cs
Lines 941 to 944 in d08c4aa
... then the error is not propagated because SftpClient.UploadFile does not have any handling for failure (StatusCode != Ok):
SSH.NET/src/Renci.SshNet/SftpClient.cs
Lines 2475 to 2480 in d08c4aa
This causes deadlock because the expectedResponses is only decremented in this path and will never hit zero.
The first commit is the fix for the deadlock (handle failure codes in UploadFile). The second commit tidies up SftpSession.RequestWrite to fix the racy exception behaviour as pointed out in #1640 (the exception handling only makes sense here when given a wait handle).
I wrote an ugly test for this (Rob-Hague@9a7526a) but it only fails on
develop
after cherry-picking the second commit because the test is single-threaded so we encounter the race in RequestWrite.closes #957
closes #1640