Skip to content

Commit c6ba594

Browse files
committed
Make RequestWrite deterministic wrt. exception handling
1 parent 3422faa commit c6ba594

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/Renci.SshNet/Sftp/SftpSession.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Globalization;
45
using System.Text;
56
using System.Threading;
@@ -914,6 +915,8 @@ public void RequestWrite(byte[] handle,
914915
AutoResetEvent wait,
915916
Action<SftpStatusResponse> writeCompleted = null)
916917
{
918+
Debug.Assert((wait is null) != (writeCompleted is null), "Should have one parameter or the other.");
919+
917920
SshException exception = null;
918921

919922
var request = new SftpWriteRequest(ProtocolVersion,
@@ -925,22 +928,27 @@ public void RequestWrite(byte[] handle,
925928
length,
926929
response =>
927930
{
928-
writeCompleted?.Invoke(response);
929-
930-
exception = GetSftpException(response);
931-
wait?.SetIgnoringObjectDisposed();
931+
if (writeCompleted is not null)
932+
{
933+
writeCompleted.Invoke(response);
934+
}
935+
else
936+
{
937+
exception = GetSftpException(response);
938+
wait.SetIgnoringObjectDisposed();
939+
}
932940
});
933941

934942
SendRequest(request);
935943

936944
if (wait is not null)
937945
{
938946
WaitOnHandle(wait, OperationTimeout);
939-
}
940947

941-
if (exception is not null)
942-
{
943-
throw exception;
948+
if (exception is not null)
949+
{
950+
throw exception;
951+
}
944952
}
945953
}
946954

0 commit comments

Comments
 (0)