Skip to content

TcpClient.Dispose used concurrently with ConnectAsync may not Dispose of the Socket #17611

@stephentoub

Description

@stephentoub

TcpClient is not documented to have thread-safe members, and in general Dispose implementations should not be used concurrently with other members on the containing types. However, as TcpClient is a thin wrapper for a Socket, code may end up calling Dispose on the TcpClient in order to cancel in-progress async operations, such as ConnectAsync. However, on Unix, ConnectAsync is implemented by creating a new Socket for each address it attempts to connect to, and that Socket isn't stored into the TcpClient until after a connection has been successfully established. As a result, Dispose can't be used to cancel the connect operation, as the Socket won't have been stored yet, and in such cases the Socket won't get disposed of.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions