Strange socket problem

Jeff Epler jepler at unpythonic.net
Wed Jun 15 14:32:36 CEST 2005


When using os.system(), files that are open in the parent are available
in the child, as you can see here in Linux' listing of the files open by
the child program:

[jepler at sofa jepler]$ python -c 'f = open("/tmp/test", "w"); print f.fileno(); import os; os.system("ls -l /proc/self/fd")'
3
total 5
lrwx------  1 jepler jepler 64 Jun 15 07:25 0 -> /dev/pts/2
lrwx------  1 jepler jepler 64 Jun 15 07:25 1 -> /dev/pts/2
lrwx------  1 jepler jepler 64 Jun 15 07:25 2 -> /dev/pts/2
l-wx------  1 jepler jepler 64 Jun 15 07:25 3 -> /tmp/test
lr-x------  1 jepler jepler 64 Jun 15 07:25 4 -> /proc/3108/fd

You may be able to set the FD_CLOEXEC flag on the files that should not
be passed to children, something like this:
    old = fcntl.fcntl(fd, fcntl.F_GETFD)
    fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
Refer to a real Unix reference for more information on what FD_CLOEXEC
does.

You may want to replace the use of os.system() with fork + close files
+ exec.  Then "myserver.py" will no longer have the listening socket
descriptor of your cherrypy server.

Python 2.4's 'subprocess' module may work better in this respect than
os.system.  It seems to include support for requesting that fds be
closed in the child (the close_fds parameter to subprocess.Popen)

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050615/9e44d580/attachment.sig>


More information about the Python-list mailing list