Skip to content
Tcl Source Code History Of Ticket b6d0d8cc2c
Login

History Of Ticket b6d0d8cc2c

Artifacts Associated With Ticket b6d0d8cc2c

  1. Ticket change [07dc6027c5] (rid 175199) by sebres on 2019-09-13 22:15:54:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized 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> linder (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 linder 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 "-linder" option (so setting it to 0 would avoid that, and any other integer value would specify setting a linder 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_LINDER (similar TCL_FEATURE_KEEPALIVE_NAGLE).
      
    5. is_private initialized to: "0"
    6. login: "sebres"
    7. priority initialized to: "5 Medium"
    8. resolution initialized to: "None"
    9. severity initialized to: "Important"
    10. status initialized to: "Open"
    11. submitter initialized to: "sebres"
    12. subsystem initialized to: "25. Channel System"
    13. title initialized to:
      (windows) socket close graceful shitdown vs. lingering (TIME-WAIT issue).
      
    14. type initialized to: "Bug"
  2. Ticket change [630dcd89d3] (rid 175252) by oehhar on 2019-09-15 16:47:25:

    1. icomment:
      Serges,
      this sounds sensible, thank you.
      
      David Graveraux might be a wizard to give more light to it.
      
      Apparently, this is an internal WIndows "feature", that Windows wants to give itself time to empty its buffers.
      
      A new parameter might be sensible.
      Also, the -nagle" parameter was always there but commented out.
      
      Thank you,
      Harald
      
    2. login: "oehhar"
    3. mimetype: "text/x-fossil-wiki"
    4. username: "oehhar"
  3. Ticket change [0e7e0d645e] (rid 175253) by oehhar on 2019-09-16 07:09:00:

    1. login: "oehhar"
    2. mimetype: "text/plain"
    3. title changed to:
      (windows) socket close graceful shutdown vs. lingering (TIME-WAIT issue).
      
  4. Ticket change [d78c9723cd] (rid 175254) by sebres on 2019-09-16 10:56:24:

    1. icomment:
      <i>> Apparently, this is an internal WIndows "feature", that Windows wants to give itself time to empty its buffers.</i>
      
      Agree and I thought also it's "normal" Windows behavior, unless I saw every single socket in the time-wait state without necessary sending buffers up-to default timeout (TcpTimedWaitDelay). This cannot be called "normal" anymore and either graceful shutdown should be implemented differently (at least up-to last receive after some send/flush) or there is really an option needed to control that by developer.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  5. Ticket change [2d23486b16] (rid 175263) by sebres on 2019-09-16 14:43:12:

    1. 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).
      
    2. login: "sebres"
    3. mimetype: "text/plain"
    4. title changed to:
      (windows) socket close: graceful shutdown vs. lingering (eternal TIME_WAIT issue).
      
  6. Ticket change [6e8bf120e3] (rid 182437) by sebres on 2020-04-24 18:40:16:

    1. icomment:
      Further investigations shows that the kind of close doing from other side (peer) is also very important to fulfill graceful shutdown - if it is not following the strict rules of graceful disconnect process, many sockets would enter TIME-WAIT state (regardless proper lingering set for them).
      
      For instance here is the diff illustrating bad (red) and good (green) server accept procedure (in sense of graceful disconnect), if a client peer closing it properly at its side (with WSASendDisconnect, wait for FD_CLOSE, etc):
      <code><pre>
      socket -server {apply {{ch args} {
      <b style="color:red">-  close $ch</b>
      <b style="color:green">+  chan event $ch readable [list apply {{ch} {
      +    if {[catch {string length [read $ch 1024]} s] || $s == 0} {
      +      close $ch
      +    }
      +  }} $ch]</b>
      }}} $port
      </pre></code>
      To signal the client that it is disconnected immediately (red case), one possibility would be to use hard reset (linger with l_onoff = 1, and l_linger = 0), but this is possible only if no data was ever sends, so exactly for this case (otherwise this can cause a loss of sent but not yet transmitted/received data).<br/>
      Additionally the TcpThreadActionProc makes the fix more complex, because the close of channel causes that CutChannel is invoked (without possibility to distinguish close and detach) before TcpCloseProc will be executed, so it removes the socket from co-thread ("windowed" handler, that handles this socket normally).
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  7. Add attachment "test-sock-time-wait - b6d0d8cc2c.tcl" [e21218783f] (rid 182570) by sebres on 2020-04-27 10:38:03.

  8. Ticket change [9f480202f7] (rid 182574) by sebres on 2020-04-27 10:51:35:

    1. icomment:
      I think, I fixed it in [b960d1b71e] as good as possible:
      
      on close it would firstly try a graceful disconnect and don't linger if in success case (as well as would perform a hard reset in case if socket get closed without data sent/received).
      
      Test script is attached...<br/>
      I don't see growing sockets in TIME-WAIT state anymore (regardless how it is closed on server side).
      
      Tests (socket.test and *-io.test) passed too.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
    4. resolution changed to: "Fixed"
    5. status changed to: "Pending"