sshnet / sshnet/SSH.NET

avoid oprhan connections after socket.ShutDown

Open
#1,525 0 comments 0 reactions 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

2024-10-23 16_44_26-Debug
using TcpView you will see many orphan connections, loosing their process(id). After some time (1 Min), the connection will be close by the OS. (use-case: The image shows the connections, login/logout in to a Sftp-Server every 5 sec, to check for new files)

To avoid oprhan connections [TimeWait) add a delay of 100ms! I think, the wait leads to a direct thread-change to fullfill the sending the disconnect message!?

Insert a delay of 100ms between
-> TryDisconnectSend
add the delay of 100ms
-> socket.ShutDown()

In Session.cs
...

    private void Disconnect(DisconnectReason reason, string message)
    {
        // transition to disconnecting state to avoid throwing exceptions while cleaning up, and to
        // ensure any exceptions that are raised do not overwrite the exception that is set
        _isDisconnecting = true;

        // send disconnect message to the server if the connection is still open (IsConnected is not correct!) 
        // and the disconnect message has not yet been sent
        //
        // note that this should also cause the listener loop to be interrupted as
        // the server should respond by closing the socket

        _socketDisposeLock.Wait();
        var socketConnected = _socket != null && _socket.IsConnected();
        _socketDisposeLock.Release();
        if (socketConnected || IsConnected)
        {
            using (var delayEvent = new ManualResetEvent(initialState: false))
            {
                TrySendDisconnect(reason, message);
                delayEvent.WaitOne(TimeSpan.FromMilliseconds(100));
            }
        }

        // disconnect socket, and dispose it
        SocketDisconnectAndDispose();
    }

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 in Session.cs at Disconnect, focusing on the TrySendDisconnect and SocketDisconnectAndDispose sequence. Reproduce the repeated SFTP login/logout scenario and inspect connections with TcpView; done means validating that the disconnect sequence no longer leaves orphan connections before the operating system closes them.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.