subprocess + python-daemon - bug/problem?

Ben Finney ben+python at benfinney.id.au
Thu Sep 17 05:21:32 CEST 2009


Ben Finney <ben+python at benfinney.id.au> writes:

> I'm not familiar enough with the nuances of the ‘subprocess’ module to
> know what might be going wrong here. I'd like to know whether it might
> be a problem in the ‘python-daemon’ library.

My test case for this is now::

=====
#! /usr/bin/python

import daemon
import os
import sys
import subprocess

fake_console = open("fake_console.txt", "w+")
daemon.DaemonContext(stdout=fake_console, stderr=fake_console).open()

sys.stdout.write("Parent daemon process.\n")
os.system("echo Child process via 'os.system'.")
subprocess.Popen(["echo", "Child process via 'subprocess.Popen'."]).wait()
sys.stdout.write("Parent daemon process done.\n")
=====

causing (in the ‘fake_console.txt’ output file)::

=====
Child process via os.system.
Child process via 'subprocess.Popen'.
Parent daemon process.
Traceback (most recent call last):
  File "/home/bignose/Projects/python/python-daemon/python-daemon.devel/bin/clegg-example", line 13, in <module>
    subprocess.Popen(["echo", "Child process via 'subprocess.Popen'."]).wait()
  File "/usr/lib/python2.5/subprocess.py", line 1184, in wait
    pid, sts = self._waitpid_no_intr(self.pid, 0)
  File "/usr/lib/python2.5/subprocess.py", line 1014, in _waitpid_no_intr
    return os.waitpid(pid, options)
OSError: [Errno 10] No child processes
=====


Ben Finney <ben+python at benfinney.id.au> writes:

> Joel Martin <nospam at martintribe.org> writes:
>
> > I'm running python-2.6.2 which supposedly has the fix for #1731717.
> > However I still still the problem with subprocess after daemonizing.
> > I've narrowed it down to just the setting of the SIGCLD signal.
[…]

> Thank you, this *really* helps narrow down the problem. I don't know
> if I'll simply be removing the handling as you suggest, but it
> certainly makes the range of solutions much clearer.

I'm also glad to see a test case that causes exactly the same error with
or without the presence of a ‘daemon.DaemonContext’.

Further research shows that handling of ‘SIGCLD’ (or ‘SIGCLD’) is fairly
OS-specific, with “ignore it” or “handle it specifically” being correct
on different systems. I think Python's default handling of this signal
is already good (modulo bug #1731717 to be addressed in ‘subprocess’).

So I will apply a change similar to Joel Martin's suggestion, to default
to avoid touching the ‘SIGCLD’ signal at all, and with extra notes in
the documentation that anyone using child processes needs to be wary of
signal handling.

This causes the above test case to succeed; the output file contains::

=====
Child process via os.system.
Child process via 'subprocess.Popen'.
Parent daemon process.
Parent daemon process done.
=====

-- 
 \     “Reality must take precedence over public relations, for nature |
  `\                            cannot be fooled.” —Richard P. Feynman |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list