libcamera: process: Reap the child process in the destructor
diff mbox series

Message ID 20261121185207.1268373-2-dpcat237@gmail.com
State New
Headers show
Series
  • libcamera: process: Reap the child process in the destructor
Related show

Commit Message

Denys Pasishnyi Nov. 21, 2026, 6:52 p.m. UTC
Process::~Process() kills the child process but never waits for it, as
noted by the "\todo wait for child process to exit" it carried. The only
place a child is reaped is onPidfdNotify(), which runs from the event
loop. When a Process is destroyed before the event loop has handled the
exit notification of its child, the killed child stays a zombie until
the parent process exits.

This leaks one zombie for every Process destroyed that way, and the
zombies accumulate in a long-lived process that creates and destroys
many of them. On an OV01A10 camera with the software ISP, current master
leaves one softisp_ipa_proxy zombie per camera start/stop cycle of a
GStreamer pipeline (7 zombies after 7 cycles in one run).

Wait for the child in the destructor, using waitid() on the pidfd. The
child has just been sent SIGKILL, so the wait only lasts until the kernel
has terminated it, and a child that was already reaped by
onPidfdNotify() no longer has a valid pidfd_, so nothing is waited for
in that case.

Extend the process unit test to check that destroying a Process whose
child is still running, or has exited without being noticed by the event
loop, leaves no child to reap. The test fails without this change.

Signed-off-by: Denys Pasishnyi <dpcat237@gmail.com>
---

Notes:
    Tested on a Dell XPS 9320 (Intel IPU6, OV01A10 sensor) with the simple
    pipeline handler and the software ISP, on top of master adfa719f, using
    a GStreamer libcamerasrc pipeline that starts and stops the camera in a
    single process:
    
      master:                            one run: 7 zombie softisp_ipa_proxy
                                         children after 7 cycles
      with this and the stop patch:      0 zombies after 8 and after 20 cycles
      with the stop patch only:          20 zombies after 20 cycles
    
    The new checks in test/process/process_test.cpp fail without the change
    to src/libcamera/process.cpp and pass with it. The rest of the meson test
    suite gives the same results as master (the one failing test, gstreamer
    multi_stream_test, also fails without the patch). Not tested on other
    platforms.
    
    This patch was developed with the help of an AI tool (Claude Code) and
    reviewed by the submitter.

 src/libcamera/process.cpp     | 21 +++++++++++++++-
 test/process/process_test.cpp | 46 +++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 1 deletion(-)


base-commit: adfa719f8ed67d18fd8a0739fc088a1f8b9e7252

Patch
diff mbox series

diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp
index eb5a363a..55b83f69 100644
--- a/src/libcamera/process.cpp
+++ b/src/libcamera/process.cpp
@@ -9,6 +9,7 @@ 
 
 #include <algorithm>
 #include <dirent.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <string.h>
@@ -119,7 +120,25 @@  Process::Process()
 Process::~Process()
 {
 	kill();
-	/* \todo wait for child process to exit */
+
+	/*
+	 * Reap the child process if it hasn't been already by onPidfdNotify(),
+	 * which is the case when the Process is destroyed before the event loop
+	 * got a chance to notice that the child exited. Otherwise the child is
+	 * left as a zombie until this process exits, which accumulates in a
+	 * long-lived process that creates and destroys many Process instances.
+	 * The child has just been sent SIGKILL, so the wait only blocks until
+	 * the kernel has terminated it.
+	 */
+	if (pidfd_.isValid()) {
+		siginfo_t info;
+		int ret;
+
+		do {
+			ret = waitid(static_cast<idtype_t>(P_PIDFD), pidfd_.get(),
+				     &info, WEXITED);
+		} while (ret < 0 && errno == EINTR);
+	}
 }
 
 /**
diff --git a/test/process/process_test.cpp b/test/process/process_test.cpp
index fe76666e..762e6027 100644
--- a/test/process/process_test.cpp
+++ b/test/process/process_test.cpp
@@ -6,7 +6,9 @@ 
  */
 
 #include <array>
+#include <errno.h>
 #include <iostream>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #include <libcamera/base/event_dispatcher.h>
@@ -76,6 +78,50 @@  protected:
 			return TestFail;
 		}
 
+		/*
+		 * Test that destroying a Process that is still running reaps
+		 * the child process, without leaving a zombie behind.
+		 */
+		{
+			Process proc;
+
+			ret = proc.start(self(), args);
+			if (ret) {
+				cerr << "failed to start process" << endl;
+				return TestFail;
+			}
+		}
+
+		/*
+		 * The test process has no other child, so waitpid() reports
+		 * ECHILD when none is left to reap.
+		 */
+		if (waitpid(-1, nullptr, WNOHANG) != -1 || errno != ECHILD) {
+			cerr << "child process not reaped on destruction" << endl;
+			return TestFail;
+		}
+
+		/*
+		 * Test the same for a child that has already exited but that
+		 * the event loop hasn't noticed yet.
+		 */
+		{
+			Process proc;
+
+			ret = proc.start(self(), args);
+			if (ret) {
+				cerr << "failed to start process" << endl;
+				return TestFail;
+			}
+
+			usleep(200000);
+		}
+
+		if (waitpid(-1, nullptr, WNOHANG) != -1 || errno != ECHILD) {
+			cerr << "exited child process not reaped on destruction" << endl;
+			return TestFail;
+		}
+
 		return TestPass;
 	}