Section 13.23

Process closure

Definition

Process closure is the established fact that every process (running program) belonging to a declared piece of work has ended and that no new process can join that work afterwards. The declared work includes the first process started and every process it created directly or indirectly; work it asked other programs to do needs its own check. Closure is checked against a boundary that contains all these processes, such as a cgroup §13.20 (a kernel group that new processes join from their creator), not against the first process alone.

The idea

A harness runs a job, waits for the job’s first process to exit, copies the output file and starts the next run on the same test device. The job had started a background helper, which keeps writing to the output file and sending commands to the device for three more seconds. The copied output is incomplete, and the next run begins on a device that is still busy. The harness checked one process; the work consisted of several.

Closing a building for the night is a fair picture. The caretaker does not stop after seeing the person who unlocked the door leave. The caretaker checks every room, locks the doors so that nobody can enter, and separately asks the contractors working elsewhere for the building whether they have finished. The picture stops being accurate in one respect: processes can leave the building without passing the caretaker, by being adopted by another parent or by starting a new session, so the rooms must be ones they cannot leave on their own, such as a cgroup. The name process closure is used here for a claim in a record of evidence; it is not an operating-system feature.

How it works

Five facts about Unix-like systems such as Linux explain why the first process is not enough.

  1. Creation. Each process has a process identifier (PID), a number. A process creates a child with the system call fork, and the child can then run a different program with exec. Children can create children of their own, to any depth.

  2. Reaping. When a process ends, the kernel keeps a small record holding its exit status §13.24 until its parent collects it with a wait call, which is called reaping. Until then the ended process is a zombie, and its PID stays taken.

  3. Adoption. If a parent ends before its child, the child is adopted by the system’s first process, PID 1, or by the nearest ancestor that has declared itself a subreaper with prctl(PR_SET_CHILD_SUBREAPER). A walk down the process tree from the original launcher no longer finds the child.

  4. Detaching. A program that runs itself in the background as a daemon, a program with no terminal or user attached, typically creates a child, which starts a new session with setsid, creates a grandchild and exits. A session is a set of process groups, and a process group is a set of processes that can be signaled together. The grandchild is in no session or process group of the launcher, so signals sent to the launcher’s process group miss it.

  5. Reuse. Once a process has been reaped, the kernel can give its PID to an unrelated new process, so a check on a PID number can find a stranger. A pidfd avoids this: it is a file descriptor §15.21, the small number a process uses to refer to something it has opened, that refers to one particular process and becomes readable when that process ends.

File descriptors bring a second kind of lifetime. A child inherits copies of its parent’s file descriptors. A pipe, a one-way channel between processes, reports end-of-file to its reader only when every copy of its write end has been closed, in every process. When a harness reads a job’s output through a pipe, end-of-file therefore means that every process holding the write end has closed it or ended; a process that sends its output elsewhere is not covered by this check. Open descriptors can also hold resources that the next run needs, such as a serial port, a lock on a file or a network port.

A cgroup removes these weaknesses. A new process starts in its creator’s cgroup, whatever adoption or new sessions happen later. The file cgroup.events shows populated 0 once no live process remains in the cgroup or below it; zombies do not count as live. Writing 1 into cgroup.kill kills every member with SIGKILL, a signal that cannot be caught. A cgroup with no live process and no child cgroup can be removed, and a removed cgroup cannot gain members. A systemd unit keeps its processes in such a cgroup.

A closure check follows from these facts.

  1. Declare the work: the cgroup or unit that will hold it, the outside services and devices it may use, and the processes whose exit status the record needs.

  2. Put the first process into a new cgroup before it starts any other process.

  3. Collect the needed exit statuses, through the parent’s wait call or the unit’s record.

  4. Wait until populated is 0, with a timeout §9.34; if the timeout expires, kill the cgroup and record the closure as forced.

  5. Remove the cgroup, so that nothing can join it later.

  6. Read each output pipe of the job to end-of-file, and check that no process outside the cgroup holds a device, lock or port that the next run needs.

  7. Confirm separately that outside services and devices have finished the work the job requested.

Worked example

A harness, PID 1000, places a job in a new cgroup gg and reads the job’s standard output through a pipe. The job’s first process J, PID 1001, starts a helper H, PID 1002, which writes to the same pipe. J also starts a daemon: process D, PID 1003, starts a new session, creates D2, PID 1004, and exits at 0.1 s; D2 closes its copy of the pipe and writes to a log file instead. J exits with status 0 at 2 s, H exits at 5 s and D2 at 20 s. Four checks give these answers:

check at 3 s at 6 s at 21 s
harness’s wait for J ended ended ended
process tree below PID 1000 empty empty empty
end-of-file on the pipe no yes yes
populated of gg 1 1 0

At 3 s the first two checks call the job finished while H and D2 run; H and D2 were adopted by PID 1 when their parents ended, so the tree below PID 1000 is empty. At 6 s the pipe check also calls it finished, because H, the last process holding the pipe’s write end, has ended, while D2 still runs. Only the cgroup check reports remaining work, until 20 s. At 21 s the cgroup has 0 members; the harness removes it, and closure holds. A fixed pause, such as waiting 10 s after J ends, would have ended at 12 s and been wrong.

PID numbers also recycle quickly. On a system whose PID limit is 32768 and that creates 1000 processes per second, the numbers wrap around in at most 32768/1000≈32.832768 / 1000 \approx 32.8 seconds. A harness that signals PID 1004 a minute after D2 ended, to clean up, can hit an unrelated process that has since received that number.

Stated precisely

Let W(t)W(t) be the set of live processes that do the declared work at time tt, and let M(g,t)M(g, t) be the set of live members of cgroup gg and of the cgroups below it. Closure holds at time tct_c when three conditions hold.

  1. M(g,tc)M(g, t_c) is empty, which populated equal to 0 shows.

  2. No process can join gg at any time t≥tct \ge t_c, which removing gg ensures.

  3. Every exit status that the record needs has been collected.

Conditions 1 and 2 give W(t)=∅W(t) = \emptyset for every t≥tct \ge t_c only if the containment assumption W(t)⊆M(g,t)W(t) \subseteq M(g, t) holds at all times tt. That assumption holds when the first process joined gg before it created any process, no member moved out of gg, and the work handed nothing to processes outside gg; the cgroup entry gives the permissions that stop a member from moving out. Work outside gg is not covered by closure and needs its own evidence.

Limits and common mistakes

Closure is not success. All processes can end after writing partial or wrong output. Exit statuses, the test oracle §10.5 that judges the output, and the evidence eligibility §13.14 rules are separate questions.

Closure covers processes on one machine. A command still being carried out by a device, a request in flight on a network, or data written to a file but not yet flushed to storage can outlive every process.

Waiting a fixed time proves nothing: the work can outlast any pause chosen in advance. A closure forced by timeout and kill must be recorded as forced, because the work was stopped, not finished.

The process tree, process groups and sessions are not boundaries: adoption and setsid take processes out of them without any permission check. A PID number is not an identity either; use the parent’s wait call, a pidfd or cgroup membership.

A zombie does not count as live, so populated can be 0 while a parent has yet to collect an exit status.

Questions answered

Which unit, PID, descendant, and file-descriptor checks define closure?

Closure, the claim that all processes of a declared piece of work have ended and that none can start again, is defined by four kinds of check that must all pass.

Work that the job asked other services or devices to do lies outside these checks and needs its own evidence.

Related entries: cgroup (§13.20), systemd service versus scope (§13.21), Exit status (§13.24), Timeout versus cancellation (§13.25), File descriptor versus pathname (§15.21). Further reading: [45].