runtime: TcpClient.LingerState Property does not behave as expected and documents w/ .NET core 2.1

According to the documentation for TcpClient.LingerState Property it should be possible to control the way a tcpClient.Close() behaves.

Specifically, by setting tcpClient.LingerState = new LingerOption (true, 0), the behavior is said to be [my bold-italic]:

Discards any pending data and Winsock resets the connection.

Now, this is not the behavior I’ve observed. And it seems others are having the similar challenges: Stackoverflow: What should I do to completely close the TcpClient connection with mcu?.

My own observations on .NET Core 2.1, using Wireshark to see what goes on, is that no TCP RST is send over the wire when tcpClient.Close() is called even with LingerState set toLingerOption (true, 0).

TCP FIN is send with tcpClient.Close(), but not RST. The only way I’ve managed to to force the RST to be send over the wire is by closing the running application. Consequently, any attempt of reconnect after a close to the same end point fails unless the application is closed and restarted. This is even true if the TcpClient is disposed (tcpClient.Dispose()) and a new TcpClient instance is created.

Is this by design? And if so, are there any work-arounds for managing scenarios where a hard TCP connection reset (i.e. RST) is desirable immediately?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

@wfurt I have nothing new to add.

I never managed to figure out what caused the issue, but it seems fixed now.