socket's strange behavior with subprocesses

Jane Austine janeaustine50 at
Wed Nov 12 17:37:01 CET 2003

Running Python 2.3 on Win XP

It seems like socket is working interdependently with subprocesses of
the process which created socket.

#the server side
>>> import socket
>>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> s.bind(('localhost',9000))
>>> s.listen(5)
>>> z=s.accept()
>>> import os
>>> f=os.popen('notepad.exe') #notepad appears on the screen
>>> z[0]
<socket._socketobject object at 0x0096C390>
>>> z[0].recv(6) 
>>> z[0].send('hello world')

#the client side
>>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> s.connect(('localhost',9000))
>>> s.send('foobar')
>>> print s.recv(1024)
hello world

Now when the client requests to recv 1024 bytes, and since there is no
more to read from the socket it blocks.

#client side
>>> s.recv(1024) #it hangs

and the server side tries to close the socket:

#server side
>>> z[0].close()
>>> #yes, it seems to have worked.

Alas, the client side doesn't wake up! It doesn't wake up unless the
notepad is exited first; only after that, 'Connection reset by peer'
is raised. What does the socket has to do with subprocesses?

More information about the Python-list mailing list