Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 6e8bf120e33b38ce4f18fa48a2f2b160a237cdea5f0f897e914afa60bc3c0de8
Ticket: b6d0d8cc2c82dbb366d1fb3f32afd2a027a41135
(windows) socket close: graceful shutdown vs. lingering (eternal TIME_WAIT issue).
User & Date: sebres on 2020-04-24 18:40:16
Changes

  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"