Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 3845e1809dc6e7042d348a90c26f08fcaae67740
Ticket: 219205ffffffffffffffffffffffffffffffffff
missing file events on server socket under Windows
User & Date: ashmael on 2001-03-23 08:39:22
Changes

  1. icomment:
    Logged In: YES 
    user_id=178287
    
    I believe I finally managed to track down this elusive bug.
    
    Background: Tcl uses WSAAsyncSelect() (winsock API) to tell 
    winsock to call a particular function (SocketProc()) when 
    an event such as incoming data occurs.  This function 
    basically checks what sort of event occurred and sets some 
    flags to be checked later in Tcl's idle/event handling loop.
    Eventually this loop calls another function (SocketEventProc
    ()) that verifies that the condition for the event (well, 
    FD_READ events, at least) is still met before signalling 
    back to Tcl (via Tcl_NotifyChannel()) to trigger the 
    function we specified (via Tcl_CreateChannelHandler()) as 
    our own handler for the event.
    
    Now, in various places the Tcl socket drivers disable the 
    WSAAsyncSelect() handler before doing some things and re-
    enabling it.  (Re-enabling also causes any existing 
    conditions to re-generate events -- ie., the handler 
    specified to WSAAsyncSelect() is called).  Before this 
    verification step mentioned above (involving a call to the 
    regular non-event-driven select() function) is one such 
    place where the WSAAsyncSelect() handler is disabled
    temporarily.
    
    However, the code was such that it's only RE-enabled after 
    the select() call when the socket no longer has data to be 
    read (select() returns 0). If there's data on the socket 
    (because, perhaps, multiple packets came in very quickly, 
    before the channel handler could read the first and clear
    the event), then the WSAAsyncSelect() handler is apparently 
    not re-enabled.
    
    I suspect, though I haven't verified, that in 8.0.5 and 
    earlier the problem didn't occur for one of a few possible 
    reasons:
    
    1. The particular codepath that left the event handler 
    disabled was basically never called (ie., at this point 
    there was never data left to be read and select() would 
    always return 0).
    
    2. -Or- the WSAAsyncSelect() handler was being re-enabled 
    later on in the event handling loop but isn't in later 
    versions.
    
    3. -Or- 8.0.5 and earlier called WSAAsyncSelect() directly 
    at this point, rather than calling SendMessage() to trigger 
    a later call to WSAAsyncSelect() to re-enable the handler, 
    which may cause a race condition of sorts.  (Though in this 
    case, the patch below would probably just be lucky that it 
    works for me).
    
    At any rate, here's the change that worked for me to fix 
    this problem (in "diff -c" format; will also upload to 
    patches section).  The little program I wrote to recreate 
    and debug this problem screams along happily without any 
    apparent dropping of events. Just one line that's inside an 
    else {} block that apparently shouldn't be (the SendMessage
    () call, which triggers a later call to WSAAsyncSelect(), 
    is moved so it is called regardless of the select() return 
    value):
    
    <pre>
    *** ./orig/tcl8.2.3/win/tclWinSock.cSun Aug  1 15:09:29 
    1999
    --- ./tcl8.2.3/win/tclWinSock.cThu Mar 22 16:44:48 2001
    ***************
    *** 853,862 ****
      if ((*winSock.select)(0, &readFds, NULL, NULL, 
    &timeout) != 0) {
          mask |= TCL_READABLE;
      } else {
    -     SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
    -     (WPARAM) SELECT, (LPARAM) infoPtr);
          infoPtr->readyEvents &= ~(FD_READ);
      }
          }
          if (events & (FD_WRITE | FD_CONNECT)) {
      mask |= TCL_WRITABLE;
    --- 853,862 ----
      if ((*winSock.select)(0, &readFds, NULL, NULL, 
    &timeout) != 0) {
          mask |= TCL_READABLE;
      } else {
          infoPtr->readyEvents &= ~(FD_READ);
      }
    + SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
    + (WPARAM) SELECT, (LPARAM) infoPtr);
          }
          if (events & (FD_WRITE | FD_CONNECT)) {
      mask |= TCL_WRITABLE;
    </pre>
    
    
  2. login: "ashmael"
  3. mimetype: "text/plain"
  4. username: "ashmael"