Search code examples
c#tcphttpwebrequestnetwork-protocolswebproxy

How to fully terminate HttpWebRequest


Even though i am properly terminating everything when i check existing HTTP connections i see they are not terminated

For example when i open 200 concurrent connections by starting different tasks

I see

158 Established HTTP connections
927 TimeWait
95 SynSent
24 LastAck
6 CloseWait
34 FinWait

The worse part is, the number of TimeWait keep increasing each minute

So how can i prevent such issue to happen?

After a while the windows become unable to make any new requests

This problem occurs when i use webproxies : Too many proxy connection kills window's resolving hosts ability

Here when i use 200 connections with different proxies

enter image description here


Solution

  • Connections in TimeWait state can generate a performance problem.

    First, take a look at TCP State diagram,

    https://en.wikipedia.org/wiki/File:Tcp_state_diagram_fixed_new.svg

    This is a state of a TCP connection after a machine’s TCP has sent the ACK segment in response to a FIN segment received from its peer (details in RFC 793 defining TCP back in 1981 http://www.ietf.org/rfc/rfc793.txt). During this state the socket resources, including the TCB (TCP Control Block) and the port of course, are not released to the OS. After a timeout expires, socket resources are released to the OS. The original reason is to deal with the Two Generals problem that can happen between peers in an unreliable medium. The connection will be in TimeWait until a configurable timeout which has a default value that is dependent on the operating system.

    These links can help you to set the TcpTimedWaitDelay parameter in Windows:

    https://technet.microsoft.com/en-us/library/cc938217.aspx http://msdn.microsoft.com/en-us/library/ee377084%28v=bts.10%29.aspx

    It says the default value is 240 seconds but I'm my tests I experienced lower times (between 60 and 120).

    Anyway, today networks are more reliable and web services requiring high performance and throughput should reduce this value. I would suggest set it just to 5 seconds. If you want to be more conservative, set it to 30 seconds.

    Other parameter that could be useful for you is the max number of ephemeral ports Windows allows a client to open. Windows Server by default limits the maximum number of ephemeral TCP ports. In some Windows, this value could be 5000. You can change this behavior by setting the value MaxUserPort in the registry.