Connection Abort on Key Auth causes problem on reconnect
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 4.4k
- Forks
- 993
- Avg merge
- 9d 21h
- Merged PRs (30d)
- 1
Description
I'm using SSH.NET v2016.1.0 to connect to many UNIX servers. We have logic to retry connecting if an attempt fails. One of the servers uses keyboard authentication and intermittently aborts the connection during login. We have not found the cause of abort but that is not of concern here. When an abort occurs, the ConnectionInfo object is not being properly disposed when the client is Disconnected or Disposed. This is causing subsequent connection attempts to fail with client is not connected errors or errant events to be logged.
Renci.SshNet.Common.SshConnectionException: An established connection was aborted by the server.
at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
at Renci.SshNet.KeyboardInteractiveAuthenticationMethod.Authenticate(Session session)
at Renci.SshNet.ClientAuthentication.TryAuthenticate(ISession session, AuthenticationState authenticationState, String[] allowedAuthenticationMethods, SshAuthenticationException& authenticationException)
at Renci.SshNet.ClientAuthentication.Authenticate(IConnectionInfoInternal connectionInfo, ISession session)
at Renci.SshNet.ConnectionInfo.Authenticate(ISession session, IServiceFactory serviceFactory)
at Renci.SshNet.Session.Connect()
at Renci.SshNet.BaseClient.Connect()
We have worked around by recreating the ConnectionInfo object on each attempt and unsubscribing handlers for the AuthenticationPrompt and AuthenticationBanners events before Disposing the client when a failure occurs.
private void DisposeClient(BaseClient client)
{
if (client != null)
{
if (client.ConnectionInfo != null)
{
// Unregister events
foreach (AuthenticationMethod authenticationMethod in client.ConnectionInfo.AuthenticationMethods)
{
if (authenticationMethod is KeyboardInteractiveAuthenticationMethod)
{
((KeyboardInteractiveAuthenticationMethod)authenticationMethod).AuthenticationPrompt -= KeyboardInteractive_AuthenticationPrompt;
break;
}
}
client.ConnectionInfo.AuthenticationBanner -= ConnectionInfo_AuthenticationBanner;
}
client.Dispose();
client = null;
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing BaseClient.Dispose and Disconnect through ConnectionInfo, Session.Connect, and KeyboardInteractiveAuthenticationMethod.Authenticate, focusing on the aborted-login path shown in the stack trace. Check how authentication event handlers and connection state are retained after an abort. Done means disposing or reconnecting after a failed keyboard-authentication attempt does not produce stale events or client-is-not-connected errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100