How can you get Socket.Shutdown to create a SocketException?

MSDN states that Socket.Shutdown may throw SocketException. This happened to me recently after the introduction of load balancing between my clients and my server. But I can not reproduce it when testing without load balancing. Can you?

Some background - I have a server application written in C # that uses TCPsockets to interact with clients. The application protocol is very simple for the server: accept the connection, read the request, send a response, wait for the client to finish working (reading 0 bytes expected), shutdown.

This code has been working without problems for many years. However, after the introduction of load balancing in front of several server machines, one of the server processes crashed due to an unhandled SocketExceptionone that was raised when the server called Socket.Shutdown. A specific client was timed, waiting for a server response and tried to close the connection earlier. The server exception message was "An existing connection was forcibly closed by the remote host." This is not unusual for the client, but, obviously, before balancing the load, the server raised this error at another point in the code. However, this is clearly a server error, and the fix is ​​obvious - it handles the exception.

However, using a test client application (also written in C #), I cannot find a sequence of operations that will cause the server to raise an exception at the time Socket.Shutdown. It seems that load balancing did something unusual for packages TCP, but still I don't like to use this as an excuse for not reproducing the problem.

I can run both server and client code in debugging, and I have WireShark monitoring the packets.

On the client side, after the connection is established, the following operations are performed:

Socket.Send() // single call
Socket.Receive() // this one times out in our scenario
Socket.XXX() // various choices as described below

On the server side, after the connection is established, the following operations are performed:

1) Socket.Receive() //multiple calls until complete message is received
2) // Processing...
3) Socket.Write() //single call
4) Socket.Receive() // single call expecting 0 bytes
5) Socket.Shutdown()

Suppose each call is wrapped using try..catch(SocketException)

A) 2, Socket.Shutdown(SocketShutDown.Send), FIN. , ( 3 5), TCP.

B) 2, Socket.Shutdown(SocketShutDown.Both) Socket.Close(), FIN. , 3 , RST- , . RST 4, Socket.Receive , 5 . 4, Socket.Receive ( 0 ), 5 .

C) "Dont Linger" (Linger 0 ), , , , Socket.Shutdown(SocketShutDown.Both) Socket.Close() "RST" . , 3 4 , 5 .

, , Socket.Shutdown, , RST-, , , RST, . ? ?

+3

Source: https://habr.com/ru/post/1768138/


All Articles