Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 818a8c889662cfcacd1b55d9b8c3453bfc39c91a
Ticket: 3574493fffffffffffffffffffffffffffffffff
TclpFinalizeSockets hangs on windows
User & Date: ralfixx on 2012-10-04 15:06:15
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    This seems related to the change introduced by  ID: 1437595.
    
    When running the attached code on Linux, the program opens the server
    socket, waits one second and then exits as expected.
    
    When running the same code on Windows-7 against tcl 8.3, the program
    behaves as above on Linux.
    
    When running the same code on Windows-7 against tcl 8.5 (Activestate, 
    or non-threaded makes no difference) the program blocks
    in the _exit() call.  
    
    The reason is to be seen in TclFinalize() where on
    Windows it waits for the socket thread to exit when unloading the TCL
    DLL, but the corresponding thread either does no longer exist, or does
    not get the Message, or does not respond to it:
    
    win/tclWin32Dll.c:
      DLLMain()
            case DLL_PROCESS_DETACH:
            ...
        Tcl_Finalize();
    =>
    generic/TclEvent.c
     Tcl_Finalize()
       => Tcl_FinalizeThread()
          => TclFinalizeIOSubsystem();
             => TclpFinalizeSockets();
    
    =>
    win/tclWinSock.c
     TclpFinalizeSockets()
     => PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0);
       // this blocks:
        WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
    
    The PostMessage() return code is 0 at that point, indicating an error, 
    the corresponding error is 1400/invalid window handle.  
    If I change the code to
    
       if (res) WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
    
    the program terminates as expected.  It seems an error to call
    WaitForSingleObject() if the PostMessage() call did not succeed, since
    the Wait() relies on the other thread to signal the Wait().  
    If the other thread does not get the call, it cannot signal...
    
    Proposed patch against tcl8.5.12:
    
    --- tcl8.5.12/win/tclWinSock.c2012/09/18 18:43:471.1
    +++ tcl8.5.12/win/tclWinSock.c2012/10/04 15:04:50
    @@ -461,14 +461,14 @@
         if (tsdPtr != NULL) {
     if (tsdPtr->socketThread != NULL) {
         if (tsdPtr->hwnd != NULL) {
    -PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0);
    +int res = PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0);
     
     /*
      * Wait for the thread to exit. This ensures that we are
      * completely cleaned up before we leave this function.
      */
     
    -WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
    +if (res) WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
     tsdPtr->hwnd = NULL;
         }
         CloseHandle(tsdPtr->socketThread);
    
    
  4. foundin changed to: "None"
  5. is_private changed to: "0"
  6. priority changed to: "5 Medium"
  7. resolution changed to: "None"
  8. status changed to: "Open"
  9. submitter changed to: "ralfixx"
  10. subsystem changed to: "None"
  11. title changed to: "TclpFinalizeSockets hangs on windows "
  12. type changed to: "Bug"