Skip to content

AsyncTcpClient.Disconnect() - Can throw, cannot access disposed object if called when already disposed. #13

@LiamKarlMitchell

Description

@LiamKarlMitchell

https://github.com/ygoe/AsyncTcpClient/blob/58acf5f03be9691a171d31df0c7ebc87521311dc/AsyncTcpClient/AsyncTcpClient.cs#L284C4-L284C39

Only call if not disposed, present & connected ?

/// <summary>
/// Closes the socket connection normally. This does not release the resources used by the
/// <see cref="AsyncTcpClient"/>.
/// </summary>
public void Disconnect()
{
    if (tcpClient == null)
    {
        return;
    }

    try
    {
        if (tcpClient.Connected && tcpClient.Client.Connected)
        {
            tcpClient.Client.Disconnect(false);
        }
    }
    catch (ObjectDisposedException)
    {
        // Already disposed, nothing to do
    }
}

/// <summary>
/// Releases the managed and unmanaged resources used by the <see cref="AsyncTcpClient"/>.
/// Closes the connection to the remote host and disables automatic reconnecting.
/// </summary>
public void Dispose()
{
    AutoReconnect = false;
    tcpClient?.Dispose();
    tcpClient = null;
    stream = null;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions