sshnet / sshnet/SSH.NET

SSHClient: ForwardedPortLocal: memory issue

Open
#775 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
4.4k
Forks
993
Avg merge
9d 21h
Merged PRs (30d)
1

Description

Hello,

I am using the following code below. I call this ConnectSSH() every 5 seconds (as a test to see how it performs)
After I let this run a night, i see that the used memory increased significantly, and it keeps rising.

After some investigation on my own, i notice that when I remove the "forwardedportlocal" object, (so I don't make such an object, and I obviously don't start it) Then there is no memory issue. The client is created, and then disposed cleanly.
I can see this happening the diagnostic tool of visual studio.

I already added portfwld.stop() and portfwld.dispose() together with of course SshClient.disconnect() and SshClient.dispose()
but that did not help.

I made a version in vb.net and also one in c#, but the results are the same.

I also Commented the connection to the database to show that this is not the issue.

I hope someone can give me some insight of what is happening, or perhaps what I am doing wrong?

    class mySQLviaSHH
    {
        
        public void ConnectSSH()
        {
            using (var SSHClient = new SshClient("myhost", "administrator", "mypassword"))
            {
                try
                {
                    // Console.WriteLine("Trying SSH connection...")
                    SSHClient.Connect();

                    if (SSHClient.IsConnected)
                        Console.WriteLine("SSH connection is active");
                    else
                        Console.WriteLine("SSH connection has failed");

                    Console.WriteLine("Trying port forwarding...");

                    var portFwld = new ForwardedPortLocal("127.0.0.1", "127.0.0.1", 3306);
                    SSHClient.AddForwardedPort(portFwld);
                    portFwld.Start();

                    if (portFwld.IsStarted)
                        Console.WriteLine("Port forwarded: {0}", portFwld.ToString());
                    else
                        Console.WriteLine("Port forwarding has failed.");
                }
                catch (SshException e)
                {
                    Console.WriteLine("SSH client connection error: {0}", e.Message);
                }
                
            }
        }


    }
Public Sub ConnectSSH()
            Using SSHClient = New SshClient("myhost", "administrator", "mypassword")
                Try
                    Console.WriteLine("Trying SSH connection...")
                    SSHClient.Connect()

                    If SSHClient.IsConnected Then
                        Console.WriteLine("SSH connection is active")
                    Else
                        Console.WriteLine("SSH connection has failed")
                    End If

                    Console.WriteLine(vbCrLf & "Trying port forwarding...")

                    Dim portFwld = New ForwardedPortLocal("127.0.0.1", "127.0.0.1", 3306)
                    SSHClient.AddForwardedPort(portFwld)
                    portFwld.Start()

                    If portFwld.IsStarted Then
                        Console.WriteLine("Port forwarded: {0}", portFwld.ToString())

                        '        'Dim dbConnect As DBConnect = New DBConnect(portFwld.BoundHost, "wcs_sch", "root", "dbpassword", "3306")
                        '        'Dim ct = dbConnect.Count("wcs_msg_host")
                        '        'Console.WriteLine(ct.ToString())
                    Else
                        Console.WriteLine("Port forwarding has failed.")
                    End If

                Catch e As SshException
                    Console.WriteLine("SSH client connection error: {0}", e.Message)
                Catch e As System.Net.Sockets.SocketException
                    Console.WriteLine("Socket connection error: {0}", e.Message)
                End Try

            End Using

        End Sub

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ConnectSSH entry point and the repeated ForwardedPortLocal creation shown in the report. Reproduce the overnight or repeated-call scenario, then trace the port-forward lifecycle and disposal behavior; done means repeated connections no longer show steadily increasing memory use.

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
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.