Skip to content

avoid oprhan connections after socket.ShutDown #1525

Open
@titotarantula2001

Description

@titotarantula2001

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();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions