Re: [modwsgi] Closing all open file descriptors in mod_wsgi

70 views
Skip to first unread message

Graham Dumpleton

unread,
Nov 29, 2012, 4:41:56 AM11/29/12
to modwsgi



On 29 November 2012 01:35, caelumvox <mattm...@gmail.com> wrote:
Hello

I am using apache2, mod_wsgi, and django.  In my Django code, I do lots of os.system() and subprocess.Popen() calls which may create background processes.  My question is, is it possible to configure mod_wsgi to close open file descriptors before doing an exec()?  Would apache have some sort of option like this?

No, there is not such ability in Apache or mod_wsgi.
 
I would think that this capability could only be available in daemon mode.  Could daemon mode close all file descriptors except 0, 1, 2, and the domain socket to apache?

No. There is no way to know which file descriptors it is safe to close and one even has to be careful about assuming 0, 1 and 2 will behave like a normal stdin, stdout and stderr.
 
I know of the subprocess.Popen(..., close_fds=True) argument in python, but I am trying to avoid having to pass this argument for each call.

Overall best practice is that you should do sub process creation from web server processes. This isn't just because of potential for open file descriptors for sockets, log files and shared memory, but also because you can inherit signals in a blocked state that can interfere with the execution of sub processes. This latter issue make it very hard to run certain Java applications as sub processes which rely on signals for some aspect of GC.

The recommended approach is to have a backend service with a less complicated inherited environment and communicate with it to perform the task, including execution of sub processes as necessary.

Graham

caelumvox

unread,
Nov 30, 2012, 5:50:27 PM11/30/12
to mod...@googlegroups.com
This makes sense.  Thank you.
Reply all
Reply to author
Forward
0 new messages