Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid oprhan connections after socket.ShutDown #1525

Open
titotarantula2001 opened this issue Oct 23, 2024 · 0 comments
Open

avoid oprhan connections after socket.ShutDown #1525

titotarantula2001 opened this issue Oct 23, 2024 · 0 comments

Comments

@titotarantula2001
Copy link

titotarantula2001 commented Oct 23, 2024

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();
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant