From aade354748153cc534fa8a1e45ddfd0e0a2dc8b9 Mon Sep 17 00:00:00 2001 From: Rob Hague Date: Sat, 23 Sep 2023 16:20:59 +0100 Subject: [PATCH] Use ExceptionDispatchInfo in more places (#1182) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Wojciech Nagórski --- .../KeyboardInteractiveAuthenticationMethod.cs | 3 ++- src/Renci.SshNet/PasswordAuthenticationMethod.cs | 3 ++- src/Renci.SshNet/Sftp/SftpFileReader.cs | 5 +++-- src/Renci.SshNet/SshCommand.cs | 4 +++- src/Renci.SshNet/SubsystemSession.cs | 13 +++++++++---- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs b/src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs index b559808d..1e0a743c 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 @@ namespace Renci.SshNet 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 ffa8213d..bebfd3c4 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 @@ namespace Renci.SshNet 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 9d44bb33..c28dd8ac 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 @@ namespace Renci.SshNet.Sftp if (_exception is not null) { - throw _exception; + ExceptionDispatchInfo.Capture(_exception).Throw(); } if (_isEndOfFileRead) @@ -102,7 +103,7 @@ namespace Renci.SshNet.Sftp // 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 13c00992..a4b861cd 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 @@ namespace Renci.SshNet 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 b89d30fd..86a081bb 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 @@ namespace Renci.SshNet 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 @@ namespace Renci.SshNet 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 @@ namespace Renci.SshNet 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 @@ namespace Renci.SshNet 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: