Tcl Source Code

View Ticket
Login
Ticket UUID: 1306694
Title: Sockets are not working in the child processes
Type: Bug Created on: 2005-09-28 09:32:09
Submitter: rbalakri Assigned to: andreas_kupries
Subsystem: 24. Channel Commands Severity:
Priority: 5 Medium Last modified: 2026-10-21 10:03:37
Status: Closed Closed by: hobbs
Resolution: Fixed Closed on: 2005-09-28 17:57:32
Version: obsolete: 8.4.11
Description:
When a process is forked and if socket is executed
within the child process the socket is not accepting
the incoming connection.

Tcl version: 8.4.11
Platform: Linux

Tcl Version is compiled with threading enabled. The
following is the sample server (daytime server) program
to reproduce the problem,

proc Server {channel clientaddr clientport} {
   puts "Connection from $clientaddr registered"
   puts $channel [clock format [clock seconds]]
   sleep 30
   close $channel
}

if { [fork] } {
       # Parent
        wait
        exit
} else {
       # child
        socket -server Server 9900
        puts "child waiting for sockets"
        vwait forever
}

The corresponding client program,

set server localhost
set sockChan [socket $server 9900]
gets $sockChan line
close $sockChan
puts "The time on $server is $line"

After the server is started and after starting the
client netstat comman gives the following output,

[root@myhost test]# netstat -atp | grep 9900
tcp        0      0 *:9900                      *:*   
                     LISTEN      4309/tcl
tcp        0      0 myhost:36222          myhost:9900 
         ESTABLISHED 4291/tcl
tcp        0      0 myhost:9900           myhost:36222
         ESTABLISHED -

The first one is the server's listening socket, second
is clients socket and third is server's connected
socket. As you can see the sever's socket doesn't have
the application associated with it.
User Comments:
hobbs added on 2005-09-29 00:57:32:
Logged In: YES 
user_id=72656

Fork and threads are not a valid combination.  If you have a
threaded build, please use another thread, not fork.

rbalakri added on 2005-09-29 11:02:31:
Logged In: YES 
user_id=431412

I've threaded server (for each client connection a worker
thread will be created to handle the client request) and I
want to daemoize the server.

For that I'm using the following piece of procedure. Can you
suggest some alternatives if what I'm doing is not correct ?

daemonize
signal ignore SIGHUP
signal unblock {SIGQUIT SIGTERM}
signal trap {SIGQUIT SIGTERM} [subst {shutdown $arg(immediate)}]
my_server <server args>

proc daemonize {} {

    global data

    if {[fork]} {
        exit 0
    }
    id process group set
    signal ignore SIGHUP
    if {[fork]} {
        # child doesn't get exited and gets into a mutex
        # deadlock when --enable-threads is enabled while
TCL compilation
        # see bug ids 923072 and 749639 in sourceforge.net
        kill SIGKILL [pid]
        exit 0
    }
    close stdin
    close stdout
    close stderr
    set fd [open /dev/null r]
    set fd [open /dev/null w]
    set fd [open /dev/null w]
    cd /
    umask 022
    return [id process]
}

proc shutdown { immediate } {
      # Check if all the worker threads are shutdown
      # and then exit the process
}

serhiy.storchaka added on 2026-10-21 10:03:37:

Works since 8.5.15 / 8.6.1: after a fork the notifier thread is restarted in the child ([e733a91cdb], [02909e227f], ticket [a0bc856dcd]), so fileevents on sockets fire there.