Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to stop subprocesses from copying listening sockets

Reply
Thread Tools

How to stop subprocesses from copying listening sockets

 
 
Rick van Hattem
Guest
Posts: n/a
 
      12-26-2008
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
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      12-26-2008
In article <(E-Mail Removed)>,
Rick van Hattem <(E-Mail Removed)> 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".
 
Reply With Quote
 
 
 
 
Rick van Hattem
Guest
Posts: n/a
 
      12-26-2008
On Friday 26 December 2008 04:05:43 Roy Smith wrote:
> In article <(E-Mail Removed)>,
>
> Rick van Hattem <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
can't start Apache on Mac OS X--no listening sockets available? 7stud Python 10 06-26-2007 10:00 AM
IO::Socket Stop listening for new connections after initial connection sgt_b2002@yahoo.com Perl Misc 4 01-19-2006 06:04 PM
Interruptions in live news listening Realone Player listening Thaqalain Computer Support 6 07-16-2005 02:11 PM
Can't stop listening !!! Sebast Computer Support 4 04-14-2005 07:24 PM



Advertisments