How to stop subprocesses from copying listening sockets

Discussion in 'Python' started by Rick van Hattem, Dec 26, 2008.

  1. Hi everyone,

    Recently I've started building a program that spawns new processes when
    requested via http, since the http interface doesn't need to be fancy I've
    just used the BaseHTTPServer module for this, but... it seems I'm running
    into a little problem. When spawning a new process (which forks itself into a
    daemon, but isn't too relevant in this case) the listening socket is copied
    to the new process. Evidently this results in the base process not being able
    to restart since the new spawned process starts listening on the port.

    Here's a simple example of what I mean.
    Assuming we've got the server like this:
    import subprocess
    import BaseHTTPServer

    server = BaseHTTPServer.HTTPServer(('', 1234),
    BaseHTTPServer.BaseHTTPRequestHandler)

    subprocess.Popen(['python', 'the_daemon.py'])


    With the_daemon.py being this:
    import time
    time.sleep(60)

    And we run the main server and kill it after that (i.e. using netstat to find
    the PID), we'll see that the new process which does nothing besides wait for
    60 second will listen on port 1234 (use netstat to confirm).

    Anyone has an idea on how to circumvent this issue?

    Kind regards,

    Rick van Hattem

    PS: tested both on Fedora 8 using Python 2.5.1 and Gentoo Linux 2008 using
    Python 2.5.2
     
    Rick van Hattem, Dec 26, 2008
    #1
    1. Advertising

  2. Rick van Hattem

    Roy Smith Guest

    In article <>,
    Rick van Hattem <> wrote:

    > Recently I've started building a program that spawns new processes when
    > requested via http, since the http interface doesn't need to be fancy I've
    > just used the BaseHTTPServer module for this, but... it seems I'm running
    > into a little problem. When spawning a new process (which forks itself into a
    > daemon, but isn't too relevant in this case) the listening socket is copied
    > to the new process.


    The standard solution to this problem is to close all descriptors after
    forking and before doing the exec. You can tell subprocess.Popen() to do
    this by call it with "close_fds=True".
     
    Roy Smith, Dec 26, 2008
    #2
    1. Advertising

  3. On Friday 26 December 2008 04:05:43 Roy Smith wrote:
    > In article <>,
    >
    > Rick van Hattem <> wrote:
    > > Recently I've started building a program that spawns new processes when
    > > requested via http, since the http interface doesn't need to be fancy
    > > I've just used the BaseHTTPServer module for this, but... it seems I'm
    > > running into a little problem. When spawning a new process (which forks
    > > itself into a daemon, but isn't too relevant in this case) the listening
    > > socket is copied to the new process.

    >
    > The standard solution to this problem is to close all descriptors after
    > forking and before doing the exec. You can tell subprocess.Popen() to do
    > this by call it with "close_fds=True".


    Thank you very much, I completely forgot about the close_fds argument.

    That will fix the problem nicely :)
     
    Rick van Hattem, Dec 26, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jane Austine
    Replies:
    4
    Views:
    448
    Colin Brown
    Nov 13, 2003
  2. Fuzzyman
    Replies:
    0
    Views:
    351
    Fuzzyman
    Dec 22, 2003
  3. Marcos

    managing multiple subprocesses

    Marcos, Feb 3, 2005, in forum: Python
    Replies:
    4
    Views:
    609
    Fredrik Lundh
    Feb 7, 2005
  4. 7stud
    Replies:
    10
    Views:
    1,050
    Bruno Desthuilliers
    Jun 26, 2007
  5. Replies:
    4
    Views:
    321
Loading...

Share This Page