socket's strange behavior with subprocesses

Colin Brown cbrown at
Thu Nov 13 21:04:38 CET 2003

"Jane Austine" <janeaustine50 at> wrote in message
news:ba1e306f.0311121646.70779793 at
> "Colin Brown" <cbrown at> wrote in message
news:<3fb28c56$1 at>...
> > "Jane Austine" <janeaustine50 at> wrote in message
> > news:ba1e306f.0311120837.6e6eddf2 at
> I tried win32 API primitives for creating subprocesses: win32all's
> CreateProcess. I used the Process class in in the
> "demos" directory. However, it didn't work with sockets perfectly.
> Say, a socket server python script is launched via CreateProcess. It
> then launches sub-processes. And I kill(via TerminateProcess) the
> socket server process. The subprocesses remain alive(as expected). I
> try to connect to the 'dead server port'. I expect almost immediate
> "connect refused" error, but it doesn't come up until the subprocesses
> are all gone and client hangs forever.
> Jane

If I am interpreting what you are saying here correctly you have:

    => [create_process]
        => Sub_process1
                subprocess2 (of subprocess1) started

Sub_process1 terminated, but socket connection held until subprocess2

This is what I would expect based on my findings. Subprocess2 has inherited
the socket handle when it was created (assuming you used os.system,
os.spawn* or os.popen*). You would have to use the correct incantation of
create_process to launch subprocess2.

I have attached the code I used in place of os.system.


# perform equivalent of os.system without open file handles

import win32process,win32event

def system(cmd):
    handles = win32process.CreateProcess \
    status = win32event.WaitForSingleObject(handles[0],win32event.INFINITE)
    if status == win32event.WAIT_ABANDONED:
        raise 'win32.system WAIT_ABANDONED'
    elif status == win32event.WAIT_FAILED:
        raise 'win32.system WAIT_FAILED'
    elif status == win32event.WAIT_IO_COMPLETION:
        raise 'win32.system WAIT_IO_COMPLETION'
    elif status == win32event.WAIT_OBJECT_0:
    elif status == win32event.WAIT_TIMEOUT:
        raise 'win32.system WAIT_TIMEOUT'
        raise 'win32.system - unknown event status = '+str(status)

More information about the Python-list mailing list