diff --git a/src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs b/src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs index b559808d1..1e0a743ce 100644 --- a/src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs +++ b/src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Runtime.ExceptionServices; using System.Threading; using Renci.SshNet.Abstractions; @@ -78,7 +79,7 @@ public override AuthenticationResult Authenticate(Session session) if (_exception != null) { - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); } return _authenticationResult; diff --git a/src/Renci.SshNet/PasswordAuthenticationMethod.cs b/src/Renci.SshNet/PasswordAuthenticationMethod.cs index ffa8213d8..bebfd3c47 100644 --- a/src/Renci.SshNet/PasswordAuthenticationMethod.cs +++ b/src/Renci.SshNet/PasswordAuthenticationMethod.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.ExceptionServices; using System.Text; using System.Threading; @@ -114,7 +115,7 @@ public override AuthenticationResult Authenticate(Session session) if (_exception != null) { - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); } return _authenticationResult; diff --git a/src/Renci.SshNet/Sftp/SftpFileReader.cs b/src/Renci.SshNet/Sftp/SftpFileReader.cs index 9d44bb330..c28dd8ac5 100644 --- a/src/Renci.SshNet/Sftp/SftpFileReader.cs +++ b/src/Renci.SshNet/Sftp/SftpFileReader.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Runtime.ExceptionServices; using System.Threading; using Renci.SshNet.Abstractions; @@ -80,7 +81,7 @@ public byte[] Read() if (_exception is not null) { - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); } if (_isEndOfFileRead) @@ -102,7 +103,7 @@ public byte[] Read() // throw when exception occured in read-ahead, or the current instance is already disposed if (_exception != null) { - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); } var data = nextChunk.Data; diff --git a/src/Renci.SshNet/SshCommand.cs b/src/Renci.SshNet/SshCommand.cs index 13c00992a..a4b861cda 100644 --- a/src/Renci.SshNet/SshCommand.cs +++ b/src/Renci.SshNet/SshCommand.cs @@ -1,6 +1,7 @@ using System; using System.Globalization; using System.IO; +using System.Runtime.ExceptionServices; using System.Text; using System.Threading; @@ -483,7 +484,8 @@ private void WaitOnHandle(WaitHandle waitHandle) switch (signaledElement) { case 0: - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); + break; case 1: // Specified waithandle was signaled break; diff --git a/src/Renci.SshNet/SubsystemSession.cs b/src/Renci.SshNet/SubsystemSession.cs index b89d30fd1..86a081bbf 100644 --- a/src/Renci.SshNet/SubsystemSession.cs +++ b/src/Renci.SshNet/SubsystemSession.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.Runtime.ExceptionServices; using System.Threading; using Renci.SshNet.Abstractions; @@ -241,7 +242,8 @@ public void WaitOnHandle(WaitHandle waitHandle, int millisecondsTimeout) switch (result) { case 0: - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); + break; case 1: throw new SshException("Connection was closed by the server."); case 2: @@ -286,7 +288,8 @@ public bool WaitOne(WaitHandle waitHandle, int millisecondsTimeout) switch (result) { case 0: - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); + return false; // unreached case 1: throw new SshException("Connection was closed by the server."); case 2: @@ -340,7 +343,8 @@ public int WaitAny(WaitHandle waitHandle1, WaitHandle waitHandle2, int milliseco switch (result) { case 0: - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); + return -1; // unreached case 1: throw new SshException("Connection was closed by the server."); case 2: @@ -377,7 +381,8 @@ public int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout) switch (result) { case 0: - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); + return -1; // unreached case 1: throw new SshException("Connection was closed by the server."); case 2: