Overview
| Artifact ID: | 2d23486b16e9cd355714c129a702bbde1449ea1904f0f3a945b84573f023e66e |
|---|---|
| Ticket: | b6d0d8cc2c82dbb366d1fb3f32afd2a027a41135
(windows) socket close: graceful shutdown vs. lingering (eternal TIME_WAIT issue). |
| User & Date: | sebres on 2019-09-16 14:43:12 |
Changes
- comment changed to:
Initially reported by @daviddem on Stack Overflow in <a href = "https://stackoverflow.com/questions/57928174/tcl-close-does-not-gracefully-terminate-tcp-ip-connection">"tcl close does not gracefully terminate tcp/ip connection"</a> (and in tclchat). Current implementation of socket subsystem for Windows leaves the socket after <code>close</code> linger (remain open after a <code>closesocket</code> call), so lets flooding with sockets in <code>TIME_WAIT</code> state up to free descriptor or port range until TcpTimedWaitDelay is reached for that. This may be especially worse because the the maximum number of file descriptors allowed per process is defined by macro FD_SETSIZE (default 1024 on Windows) and it can be reached very fast by many concurrent connection going "half" closed related to the issue (either on client or on server resp. both sides). The simplest test-case looks like this: <pre style="pending-left: 10pt"> % puts [exec netstat -n | grep -c TIME_WAIT] 2 % timerate { time { close [socket localhost 80] } 50; after 100; puts [exec netstat -n | grep -c TIME_WAIT] } 1000 52 102 152 </pre> As one can see, <b>every socket</b> entering TIME_WAIT, so linger in a bit strange manner - remain open after a <code>closesocket</code> call (corresponding MS, to enable queued data to be sent)...<br/> Just there is nothing to send (excepting notifying packet "socket going closed"). MS docs (<a href = "https://docs.microsoft.com/en-us/windows/win32/winsock/graceful-shutdown-linger-options-and-socket-closure-2">"Graceful Shutdown, Linger Options, and Socket Closure"</a> and <a href = "https://docs.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-linger">"LINGER (winsock.h)"</a>) are realy confusing, but...<br/> I found how one can "solve" the entering TIME_WAIT state... Added this implicit before <code>closesocket</code>: <pre style="pending-left: 10pt"> /* set SO_DONTLINGER to 0 (yes, 0 :) - forces the socket will not remain open */ BOOL val = 0; setsockopt(infoPtr->socket, SOL_SOCKET, SO_DONTLINGER, (const char *)&val, sizeof(BOOL)); </pre> and it looks like the flooding with such half-closed sockets is stopped... <pre style="pending-left: 10pt"> % puts [exec netstat -n | grep -c TIME_WAIT] 1 % timerate { time { close [socket localhost 80] } 50; after 100; puts [exec netstat -n | grep -c TIME_WAIT] } 1000 1 1 1 </pre> Just I don't think it can be called as "graceful" shutdown now, because I assume it does not send any pending data anymore, (but I don't know how one can do it properly without providing more options to control that by developer)...<br/> There is also another weird MS sentence in the docu: <blockquote> <i>Note that enabling a nonzero timeout on a nonblocking socket is not recommended.</i> </blockquote> And are our (win-native) sockets not always nonblocking? I think so... at least in the <code>CreateSocket</code> <code>FIONBIO</code> is set to 1. So it is more to investigate. But as interim solution we can implement "-linger" option (so setting it to 0 would avoid that, and any other integer value would specify setting a linger timeout, where -1 can mean default system timeout is used).<br/> One could also well safe provide this option firstly using some define TCL_FEATURE_SOCKET_LINGER (similar TCL_FEATURE_KEEPALIVE_NAGLE). - login: "sebres"
- mimetype: "text/plain"
- title changed to:
(windows) socket close: graceful shutdown vs. lingering (eternal TIME_WAIT issue).