Random “An existing connection was forcibly closed by the remote host.” after a TCP reset

后端 未结 4 1693
你的背包
你的背包 2021-02-19 19:14

I have two parts, a client and a server. And I try to send data (size > 5840 Bytes) from the client to the server and then the server sends the data back. I loop this a number o

相关标签:
4条回答
  • 2021-02-19 19:18

    I solved the same problem with increasing cache limit of iis root entry. you can also disable caching. Hope it helps.

    0 讨论(0)
  • 2021-02-19 19:23

    We had this problem when using HttpClient random errors like

    System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
     ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
     ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
    

    or

     ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
    

    Problem in Explicit Congestion Notification, or ECN, in the TCP stack. This is also known as ECN Capability. Explicit Congestion Notification is an extension to the Internet Protocol and to the Transmission Control Protocol and is defined in RFC 3168. ECN allows end-to-end notification of network congestion without dropping packets. If ECN Capability is enabled, you can disable it:

    netsh interface tcp set global ecncapability=disabled
    

    A reboot is not required.

    0 讨论(0)
  • 2021-02-19 19:31

    I am no expert but don't you think calling stream.Close() on Client will close stream while server is still trying to write at

    // Send back a response.
    stream.Write(bytes, 0, size);
    

    Also you may want to put some data to mark end of it, so that server knows and stops reading.

    0 讨论(0)
  • 2021-02-19 19:36

    Two problems that I see:

    1. You are assuming that a read for size bytes will actually read size bytes. It does not. TCP is a streaming protocol. A read reads at least one byte. That's the only guarantee given.
    2. Your code will randomly deadlock. The client writes data to the server. The server echos it back. But the client will not read until it has written everything. If it writes more than the network buffers can take this will deadlock. The client must read and write concurrently. Probably, you need another thread/task for reading the data back. A good pattern to solve that issue would be to start one writer task, one reader task and to Task.WhenAll/WaitAll to join them back.

    I'm not sure under what exact circumstances a TCP stack would send a RST. Could be due to the deadlock causing a timeout.

    You are not swallowing any exceptions, right?

    Normally, closing a connection performs an orderly shutdown in the background. But I'm unsure about what happens when the other side is still writing at this point. Maybe the answer is that an RST is generated by the receiver of the writes. Surely, the TCP spec would answer this question. If this answer is to be trusted then indeed a Shutdown(Read)/Close followed by an incoming write would RST the connection.

    Fix both issues and report back with your findings.

    0 讨论(0)
提交回复
热议问题