Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 37726af19487821b7ac214352794988a06f6c070
Ticket: 217794ffffffffffffffffffffffffffffffffff
Sockets are incorrectly inherited in child processes on Win-
User & Date: nobody on 2000-10-26 00:43:46
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 927 Bug
    Version: 8.1a2
    SubmitDate: '1998-12-04'
    LastModified: '1998-12-08'
    Severity: MED
    Status: Closed
    Submitter: welch
    ChangedBy: stanton
    RelatedBugIDs: 892
    OS: Windows NT
    OSVersion: 4.0 Service Pack 3
    Machine: X86
    FixedDate: '2000-10-25'
    ClosedDate: '1998-12-08'
    
    
    Name:
    
    Kevin B. Kenny
    
    
    
    Extensions:
    
    None required for this test.
    
    
    
    CustomShell:
    
    None required for this test.
    
    
    
    ReproducibleScript:
    
    # When Tcl launches a child process
    
    # with the 'exec' command on Win-NT,
    
    # the handles of any open sockets
    
    # are inherited in the child.  If the
    
    # parent terminates and the child does
    
    # not, the sockets are never closed.
    
    
    
    # The following code could be added to 'socket.test' in the
    
    # Tcl test suite; it exhibits the three
    
    # cases of this behavior (server
    
    # sockets, client sockets, and accepted
    
    # sockets).
    
    
    
    #########################################################################
    
    ##
    
    # The following tests fail on Win-NT in all releases of Tcl up to#
    
    # and including 8.1a2, but succeed on Unix.  The problem is that#
    
    # NT sets the HANDLE_FLAG_INHERIT bit for sockets to 1 by default,#
    
    # so Tcl has to turn it off.#
    
    ##
    
    #########################################################################
    
    
    
    test socket-12.1 {testing inheritance of server sockets} {
    
        removeFile script1
    
        removeFile script2
    
    
    
        # Script1 is just a 10 second delay.  If the server socket
    
        # is inherited, it will be held open for 10 seconds
    
    
    
        set f [open script1 w]
    
        puts $f {
    
    after 10000 exit
    
    vwait forever
    
        }
    
        close $f
    
    
    
        # Script2 creates the server socket, launches script1,
    
        # waits a second, and exits.  The server socket will now
    
        # be closed unless script1 inherited it.
    
    
    
        set f [open script2 w]
    
        puts $f [list set tcltest $tcltest]
    
        puts $f {
    
    set f [socket -server accept 2828]
    
    proc accept { file addr port } {
    
        close $file
    
    }
    
    exec $tcltest script1 &
    
    close $f
    
    after 1000 exit
    
    vwait forever
    
        }
    
        close $f
    
    
    
        # Launch script2 and wait 5 seconds
    
    
    
        exec $tcltest script2 &
    
        after 5000 { set ok_to_proceed 1 }
    
        vwait ok_to_proceed
    
    
    
        # If we can still connect to the server, the socket got inherited.
    
    
    
        if {[catch {socket localhost 2828} msg]} {
    
    set x {server socket was not inherited}
    
        } else {
    
    close $msg
    
    set x {server socket was inherited}
    
        }
    
    
    
        removeFile script1
    
        removeFile script2
    
        set x
    
    } {server socket was not inherited}
    
    
    
    test socket-12.2 {testing inheritance of client sockets} {
    
        removeFile script1
    
        removeFile script2
    
    
    
        # Script1 is just a 10 second delay.  If the server socket
    
        # is inherited, it will be held open for 10 seconds
    
    
    
        set f [open script1 w]
    
        puts $f {
    
    after 10000 exit
    
    vwait forever
    
        }
    
        close $f
    
    
    
        # Script2 opens the client socket and writes to it.  It then
    
        # launches script1 and exits.  If the child process inherited the
    
        # client socket, the socket will still be open.
    
    
    
        set f [open script2 w]
    
        puts $f [list set tcltest $tcltest]
    
        puts $f {
    
    set f [socket localhost 2829]
    
    exec $tcltest script1 &
    
    puts $f testing
    
    flush $f
    
    after 1000 exit
    
    vwait forever
    
        }
    
        close $f
    
    
    
        # Create the server socket
    
    
    
        set server [socket -server accept 2829]
    
        proc accept { file host port } {
    
    
    
    # When the client connects, establish the read handler
    
    global server
    
    close $server
    
    fileevent $file readable [list getdata $file]
    
    fconfigure $file -buffering line -blocking 0
    
    return
    
        }
    
        proc getdata { file } {
    
    
    
    # Read handler on the accepted socket.
    
    global x
    
    global failed
    
    set status [catch {read $file} data]
    
    if {$status != 0} {
    
        set x {read failed, error was $data}
    
        catch { close $file }
    
    } elseif {[string compare {} $data]} {
    
    } elseif {[fblocked $file]} {
    
    } elseif {[eof $file]} {
    
        if {$failed} {
    
    set x {client socket was inherited}
    
        } else {
    
    set x {client socket was not inherited}
    
        }
    
        catch { close $file }
    
    } else {
    
        set x {impossible case}
    
        catch { close $file }
    
    }
    
    return
    
        }
    
    
    
        # If the socket doesn't hit end-of-file in 5 seconds, the
    
        # script1 process must have inherited the client.
    
    
    
        set failed 0
    
        after 5000 [list set failed 1]
    
    
    
        # Launch the script2 process
    
    
    
        exec $tcltest script2 &
    
    
    
        vwait x
    
        if {!$failed} {
    
    vwait failed
    
        }
    
        removeFile script1
    
        removeFile script2
    
        set x
    
    } {client socket was not inherited}
    
    
    
    test socket-12.3 {testing inheritance of accepted sockets} {
    
        removeFile script1
    
        removeFile script2
    
    
    
        set f [open script1 w]
    
        puts $f {
    
    after 10000 exit
    
    vwait forever
    
        }
    
        close $f
    
    
    
        set f [open script2 w]
    
        puts $f [list set tcltest $tcltest]
    
        puts $f {
    
    set server [socket -server accept 2930]
    
    proc accept { file host port } {
    
        global tcltest
    
        puts $file {test data on socket}
    
        exec $tcltest script1 &
    
        after 1000 exit
    
    }
    
    vwait forever
    
        }
    
        close $f
    
    
    
        # Launch the script2 process and connect to it.  See how long
    
        # the socket stays open
    
    
    
        exec $tcltest script2 &
    
    
    
        after 1000 set ok_to_proceed 1
    
        vwait ok_to_proceed
    
    
    
        set f [socket localhost 2930]
    
        fconfigure $f -buffering full -blocking 0
    
        fileevent $f readable [list getdata $f]
    
    
    
        # If the socket is still open after 5 seconds, the script1 process
    
        # must have inherited the accepted socket.
    
    
    
        set failed 0
    
        after 5000 set failed 1
    
    
    
        proc getdata { file } {
    
    
    
    # Read handler on the client socket.
    
    global x
    
    global failed
    
    set status [catch {read $file} data]
    
    if {$status != 0} {
    
        set x {read failed, error was $data}
    
        catch { close $file }
    
    } elseif {[string compare {} $data]} {
    
    } elseif {[fblocked $file]} {
    
    } elseif {[eof $file]} {
    
        if {$failed} {
    
    set x {accepted socket was inherited}
    
        } else {
    
    set x {accepted socket was not inherited}
    
        }
    
        catch { close $file }
    
    } else {
    
        set x {impossible case}
    
        catch { close $file }
    
    }
    
    return
    
        }
    
        
    
        vwait x
    
    
    
        removeFile script1
    
        removeFile script2
    
        set x
    
    } {accepted socket was not inherited}
    
    
    
    
    
    
    
    
    
    ObservedBehavior:
    
    The three tests above all fail,
    
    reporting that the socket was inherited
    
    in the child process.  (All three pass
    
    on Solaris.)
    
    
    
    
    
    
    
    DesiredBehavior:
    
    The three tests should all pass.
    
    
    
    
    
    
    
    Patch:
    
    *** tclWinSock.c.origFri Feb 20 18:19:56 1998
    
    --- tclWinSock.cThu Dec  3 17:01:54 1998
    
    ***************
    
    *** 985,990 ****
    
    --- 985,997 ----
    
          }
    
      
    
          /*
    
    +      * Win-NT has a misfeature that sockets are inherited in child
    
    +      * processes by default.  Turn off the inherit bit.
    
    +      */
    
    + 
    
    +     SetHandleInformation( (HANDLE) sock, HANDLE_FLAG_INHERIT, 0 );
    
    + 
    
    +     /*
    
           * Set kernel space buffering
    
           */
    
      
    
    ***************
    
    *** 1486,1491 ****
    
    --- 1493,1505 ----
    
          if (newSocket == INVALID_SOCKET) {
    
              return;
    
          }
    
    + 
    
    +     /*
    
    +      * Win-NT has a misfeature that sockets are inherited in child
    
    +      * processes by default.  Turn off the inherit bit.
    
    +      */
    
    + 
    
    +     SetHandleInformation( (HANDLE) newSocket, HANDLE_FLAG_INHERIT, 0 );
    
      
    
          /*
    
           * Add this socket to the global list of sockets.
    
    
    
    
    
    
    
    
    
    PatchFiles:
    
    tclWnSock.c
    
    
    
    
    
    
    
    
    
    FOR THE ATTENTION OF SCOTT STANTON:
    We have discussed this problem in
    a telephone conversation on
    2 December 1998
    
    
  4. foundin changed to: "obsolete: 8.1a2"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "69. Other"
  10. title changed to:
    Sockets are incorrectly inherited in child processes on Win-
    
  11. type changed to: "Bug"