Silencing the output of Python's subprocess.Popen

I’m learning Python these days while writing an script to automate the testing of ATF under multiple virtual machines. I had this code in a shell script, but it is so ugly and clumsy that I don’t even dare to add it to the repository. Hopefully, the new version in Python will be more robust and versatile enough to be published.

One of the things I’ve been impressed by is the subprocess module and, in special, its Popen class. By using this class, it is trivial to spawn subprocesses and perform some IPC with them. Unfortunately, Popen does not provide any way to silence the output of the children. As I see it, it’d be nice if you’d pass an IGNORE flag as the stdout/stderr behavior, much like you can currently set those to PIPE or set stderr to STDOUT.

January 5, 2009 · Tags: process, python
Continue reading (about 2 minutes)

Testing the process-tree killing algorithm

Now that you know the procedure to kill a process tree, I can explain how the automated tests for this feature work. In fact, writing the tests is what was harder due to all the race conditions that popped up and due to my rusty knowledge of tree algorithms.

Basically, the testing procedure works like this:

  1. Spawn a complete tree of processes based on a configurable degree D and height H.
  2. Make each child tell the root process its PID so that the root process can have a list of all its children, be them direct or indirect, for control purposes.
  3. Wait until all children have reported their PID and are ready to be killed.
  4. Execute the kill-tree algorithm on the root process.
  5. Wait until the children have died.
  6. Check that none of the PIDs gathered in point 2 are still alive (which could be, but reparented to init(8) if they were not properly killed). If some are, the recursive kill failed.

The tricky parts were 3 and 5.

January 17, 2008 · Tags: atf, process
Continue reading (about 3 minutes)

How to kill a tree of processes

Yesterday I mentioned the need for a way to kill a tree of processes in order to effectively implement timeouts for test cases. Let’s see how the current algorithm in ATF works:

  1. The root process is stopped by sending a SIGSTOP to it so that it cannot spawn any new children while being processed.
  2. Get the whole list of active processes and filter them to only get those that are direct children of the root process.
  3. Iterate over all the direct children and repeat from 1, recursively.
  4. Send the real desired signal (typically SIGTERM) to the root process.

There are two major caveats in the above algorithm. First, point 2. There is no standard way to get the list of processes of a Unix system, so I have had to code three different implementations so far for this trivial requirement: one for NetBSD’s KVM, one for Mac OS X’s sysctl kern.proc node and one for Linux’s procfs.

January 16, 2008 · Tags: atf, portability, process
Continue reading (about 3 minutes)

Implementing timeouts for test cases

One of the pending to-do entries for ATF 0.4 is (was, mostly) the ability to define a timeout for a test case after which it is forcibly terminated.  The idea behind this feature is to prevent broken tests from stalling the whole test suite run, something that is already needed by the factor(6) tests in NetBSD.  Given that I want to release this version past weekend, I decided to work on this instead of delaying it because… you know, this sounds pretty simple, right? Hah!

January 15, 2008 · Tags: atf, netbsd, process
Continue reading (about 3 minutes)