fix: handles an object disposed ex

- the SSH Stream is occasionally disposed, when this happens we catch and recreate the client
- removed the line where we set the Timer to null at disconnect, as there is no other place that the timer gets recreated
- added try/catch w/ logging in the KillClient method for safety's sake
This commit is contained in:
Nick Genovese
2023-08-16 10:08:06 -04:00
parent 402af2ac17
commit 751d157f07

View File

@@ -249,8 +249,6 @@ namespace PepperDash.Core
Debug.Console(1, this, "Creating new SshClient");
ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
Client = new SshClient(connectionInfo);
Client.ErrorOccurred -= Client_ErrorOccurred;
Client.ErrorOccurred += Client_ErrorOccurred;
//Attempt to connect
@@ -320,7 +318,7 @@ namespace PepperDash.Core
if (ReconnectTimer != null)
{
ReconnectTimer.Stop();
ReconnectTimer = null;
// ReconnectTimer = null;
}
KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY);
@@ -333,14 +331,23 @@ namespace PepperDash.Core
{
KillStream();
try
{
if (Client != null)
{
Client.ErrorOccurred -= Client_ErrorOccurred;
Client.Disconnect();
Client.Dispose();
Client = null;
ClientStatus = status;
Debug.Console(1, this, "Disconnected");
}
}
catch (Exception ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Client:{0}", ex);
}
}
/// <summary>
/// Anything to do with reestablishing connection on failures
@@ -374,6 +381,8 @@ namespace PepperDash.Core
/// Kills the stream
/// </summary>
void KillStream()
{
try
{
if (TheStream != null)
{
@@ -384,6 +393,11 @@ namespace PepperDash.Core
Debug.Console(1, this, "Disconnected stream");
}
}
catch (Exception ex)
{
Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Stream:{0}", ex);
}
}
/// <summary>
/// Handles the keyboard interactive authentication, should it be required.
@@ -478,23 +492,34 @@ namespace PepperDash.Core
if (Client != null && TheStream != null && IsConnected)
{
if (StreamDebugging.TxStreamDebuggingIsEnabled)
Debug.Console(0, this, "Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text));
Debug.Console(0,
this,
"Sending {0} characters of text: '{1}'",
text.Length,
ComTextHelper.GetDebugText(text));
TheStream.Write(text);
TheStream.Flush();
}
else
{
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text");
}
}
catch (ObjectDisposedException ex)
{
Debug.Console(0, this, "Exception: {0}", ex.Message);
Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace);
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
ReconnectTimer.Reset();
}
catch (Exception ex)
{
Debug.Console(0, "Exception: {0}", ex.Message);
Debug.Console(0, "Stack Trace: {0}", ex.StackTrace);
Debug.Console(0, this, "Exception: {0}", ex.Message);
Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace);
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing");
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed");
}
}
@@ -519,9 +544,20 @@ namespace PepperDash.Core
Debug.Console(1, this, "Client is null or disconnected. Cannot Send Bytes");
}
}
catch
catch (ObjectDisposedException ex)
{
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed. Disconnected, closing");
Debug.Console(0, this, "Exception: {0}", ex.Message);
Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace);
KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED);
ReconnectTimer.Reset();
}
catch (Exception ex)
{
Debug.Console(0, this, "Exception: {0}", ex.Message);
Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace);
Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed");
}
}