Skip to content
Tcl Source Code Artifact [2d23486b16]
Login

Artifact [2d23486b16]

Artifact 2d23486b16e9cd355714c129a702bbde1449ea1904f0f3a945b84573f023e66e:

Ticket change [2d23486b16] - Ticket [b6d0d8cc2c82dbb3|b6d0d8cc2c] <i>(windows) socket close: graceful shutdown vs. lingering (eternal TIME_WAIT issue).</i> status still Open with 4 other changes by sebres on 2019-09-16 14:43:12.
D 2019-09-16T14:43:12.992
J comment Initially\sreported\sby\s@daviddem\son\sStack\sOverflow\sin\s<a\shref\s=\s"https://stackoverflow.com/questions/57928174/tcl-close-does-not-gracefully-terminate-tcp-ip-connection">"tcl\sclose\sdoes\snot\sgracefully\sterminate\stcp/ip\sconnection"</a>\s(and\sin\stclchat).\r\n\r\nCurrent\simplementation\sof\ssocket\ssubsystem\sfor\sWindows\sleaves\sthe\ssocket\safter\s<code>close</code>\slinger\s(remain\sopen\safter\sa\s<code>closesocket</code>\scall),\sso\slets\sflooding\swith\ssockets\sin\s<code>TIME_WAIT</code>\sstate\sup\sto\sfree\sdescriptor\sor\sport\srange\suntil\sTcpTimedWaitDelay\sis\sreached\sfor\sthat.\r\n\r\nThis\smay\sbe\sespecially\sworse\sbecause\sthe\sthe\smaximum\snumber\sof\sfile\sdescriptors\sallowed\sper\sprocess\sis\sdefined\sby\smacro\sFD_SETSIZE\s(default\s1024\son\sWindows)\sand\sit\scan\sbe\sreached\svery\sfast\sby\smany\sconcurrent\sconnection\sgoing\s"half"\sclosed\srelated\sto\sthe\sissue\s(either\son\sclient\sor\son\sserver\sresp.\sboth\ssides).\r\n\r\nThe\ssimplest\stest-case\slooks\slike\sthis:\r\n<pre\sstyle="pending-left:\s10pt">\r\n\s\s%\sputs\s[exec\snetstat\s-n\s|\sgrep\s-c\sTIME_WAIT]\r\n\s\s2\r\n\s\s%\stimerate\s{\stime\s{\sclose\s[socket\slocalhost\s80]\s}\s50;\safter\s100;\sputs\s[exec\snetstat\s-n\s|\sgrep\s-c\sTIME_WAIT]\s}\s1000\r\n\s\s52\r\n\s\s102\r\n\s\s152\r\n</pre>\r\nAs\sone\scan\ssee,\s<b>every\ssocket</b>\sentering\sTIME_WAIT,\sso\slinger\sin\sa\sbit\sstrange\smanner\s-\sremain\sopen\safter\sa\s<code>closesocket</code>\scall\s(corresponding\sMS,\sto\senable\squeued\sdata\sto\sbe\ssent)...<br/>\r\nJust\sthere\sis\snothing\sto\ssend\s(excepting\snotifying\spacket\s"socket\sgoing\sclosed").\r\n\r\nMS\sdocs\s(<a\shref\s=\s"https://docs.microsoft.com/en-us/windows/win32/winsock/graceful-shutdown-linger-options-and-socket-closure-2">"Graceful\sShutdown,\sLinger\sOptions,\sand\sSocket\sClosure"</a>\sand\s<a\shref\s=\s"https://docs.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-linger">"LINGER\s(winsock.h)"</a>)\sare\srealy\sconfusing,\sbut...<br/>\r\nI\sfound\show\sone\scan\s"solve"\sthe\sentering\sTIME_WAIT\sstate...\sAdded\sthis\simplicit\sbefore\s<code>closesocket</code>:\r\n<pre\sstyle="pending-left:\s10pt">\r\n\s\s/*\sset\sSO_DONTLINGER\sto\s0\s(yes,\s0\s:)\s-\sforces\sthe\ssocket\swill\snot\sremain\sopen\s*/\r\n\s\sBOOL\sval\s=\s0;\r\n\s\ssetsockopt(infoPtr->socket,\sSOL_SOCKET,\sSO_DONTLINGER,\s(const\schar\s*)&val,\ssizeof(BOOL));\r\n</pre>\r\nand\sit\slooks\slike\sthe\sflooding\swith\ssuch\shalf-closed\ssockets\sis\sstopped...\r\n<pre\sstyle="pending-left:\s10pt">\r\n\s\s%\sputs\s[exec\snetstat\s-n\s|\sgrep\s-c\sTIME_WAIT]\r\n\s\s1\r\n\s\s%\stimerate\s{\stime\s{\sclose\s[socket\slocalhost\s80]\s}\s50;\safter\s100;\sputs\s[exec\snetstat\s-n\s|\sgrep\s-c\sTIME_WAIT]\s}\s1000\r\n\s\s1\r\n\s\s1\r\n\s\s1\r\n</pre>\r\nJust\sI\sdon't\sthink\sit\scan\sbe\scalled\sas\s"graceful"\sshutdown\snow,\sbecause\sI\sassume\sit\sdoes\snot\ssend\sany\spending\sdata\sanymore,\s(but\sI\sdon't\sknow\show\sone\scan\sdo\sit\sproperly\swithout\sproviding\smore\soptions\sto\scontrol\sthat\sby\sdeveloper)...<br/>\r\nThere\sis\salso\sanother\sweird\sMS\ssentence\sin\sthe\sdocu:\r\n<blockquote>\r\n<i>Note\sthat\senabling\sa\snonzero\stimeout\son\sa\snonblocking\ssocket\sis\snot\srecommended.</i>\r\n</blockquote>\r\nAnd\sare\sour\s(win-native)\ssockets\snot\salways\snonblocking?\sI\sthink\sso...\sat\sleast\sin\sthe\s<code>CreateSocket</code>\s<code>FIONBIO</code>\sis\sset\sto\s1.\r\n\r\nSo\sit\sis\smore\sto\sinvestigate.\sBut\sas\sinterim\ssolution\swe\scan\simplement\s"-linger"\soption\s(so\ssetting\sit\sto\s0\swould\savoid\sthat,\sand\sany\sother\sinteger\svalue\swould\sspecify\ssetting\sa\slinger\stimeout,\swhere\s-1\scan\smean\sdefault\ssystem\stimeout\sis\sused).<br/>\r\nOne\scould\salso\swell\ssafe\sprovide\sthis\soption\sfirstly\susing\ssome\sdefine\sTCL_FEATURE_SOCKET_LINGER\s(similar\sTCL_FEATURE_KEEPALIVE_NAGLE).
J login sebres
J mimetype text/plain
J title (windows)\ssocket\sclose:\sgraceful\sshutdown\svs.\slingering\s(eternal\sTIME_WAIT\sissue).
K b6d0d8cc2c82dbb366d1fb3f32afd2a027a41135
U sebres
Z a98ce3f899a658b563e26c5631ec0088