From patchwork Sat Nov 21 18:52:07 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denys Pasishnyi X-Patchwork-Id: 29066 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 599D3C3355 for ; Sat, 21 Nov 2026 18:52:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1A09C6A97F; Sat, 21 Nov 2026 19:52:35 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="thGrfcbH"; dkim-atps=neutral Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7033C6A80C for ; Sat, 21 Nov 2026 19:52:33 +0100 (CET) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-4a214c5c4c0so18190935e9.3 for ; Sat, 21 Nov 2026 10:52:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795287153; x=1795891953; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=/g9rFDCAdNQ+xX1QJ6Vfns/uPPmjJMURvWqO0rMUi4Y=; b=thGrfcbH+3VZyCPNV8dPfqO0sHwUHKIR6vO6Zq5MxlhztwDiLLfuXUPvcpgpibLbDp IUthNvJ+MK/QD+73PRxeSqcD4waaJQCOqm9v+MASlq3VApX89eBUjfIVLUMfhCY99km0 z8VzEQW+W8nq4mz36xEjcYtxuXpNrfS7YUNXLnUXifqMGYJ6BivW+cZpVLp7f0qi9dgc fCubVU5vZDCiAZvPrKZfLZ2jrcJMqZxI7eKTCFRiB6XqCJ38mRT5Rvm+SFLjQjJ2/od3 Oerky3QcBWCucS0JJO7HfLNlRxN+KM3EVfOgyIiMweTO/iJCHakFK9rqNcJD0XYlUS0x c83Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795287153; x=1795891953; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/g9rFDCAdNQ+xX1QJ6Vfns/uPPmjJMURvWqO0rMUi4Y=; b=FWExolIh2wpXGi5fx27jSdGNjbv3vlF9Y+i6YtcuGvuOPrTlJi0ytFBtypzTCbbGRV Znbela+TJ3FtMZ6EISk80J2DN2O84O++Ns0tx22Pa20hlKgaqvNPq9H4rK/oLirBMl5D ZRsWfbY323YUMWIt0r5HrQRy09AVNeQh1W9Y3F1h+H98rWiTeJH9ISIIeIuG1vjw4JeP G13A/JVZtX3uxNFMs1UkBQHSy61B+uh9oFyKxID/keXoTYEklLNczpRf5O4qzqVke4Ao EZMdGChcg4HVX6epo884C6L7a9KNgrxvWaO7FUmOdqCTp1aaFYfnK2yM5nAnKfZ14Z8B 7Dag== X-Gm-Message-State: APuU4Z8fsLxjx7uMnxHw4ReTctocsLP0kBSrpcAqOFe/Q139IoLfqE3z TTReifgV66BvQGTtZf2ZvAEM9xQ5nh0ae+w5Di3kiB5lI2Mxzjc44THslcRwBw== X-Gm-Gg: AZFdTZeS2Ss2bYUORLaivVTa19K67CAGKz/+0/L+wOUCpZuW1YKLbRuWfNzX26j8M6D /7Q1h4XfdcCGwS6eY4HhthVRiRJxzL2VLdt2bd680bQHdalFxXRewYlnjB+b4hE8YrU3XYa2px3 9C491ykoQyugjTlasHak40Vv14EdCOAxhJINwt50CR9Ze/Igkr+psi85YDTIfF7Rp5NIIGp6H41 RMSnjqixuYpnb6g/3sz01gmtcJt0JTLUGLxjKwkJX2WCoG/WmipfXrVPT+nJAPXuJJNMgprvjc4 5SzmasurDSgf1dbep7BNvDQna3bauCaV6unVeVWMYPprwzPRO4BpSdSg6B1TLu1Au8+YSyN/9VG VmhLLoW93vQGHbJuKKnCl2HuXazOjOp89+gTgztUw6yPOL3KBzfjXpz4TW2O3HhkUj6l9fvZEO7 ufsKhq8q1dQwA2buj0SiNYbsTlQaTbci2i14GFMLdqwQZ13D9VBZ7FpwFBEeD5f7lUlwL47IsnS F3c8qd36He/sUEpkqfpyb/mLyZ2csXwNHFvpVn+y6ws6lV2WELk5mwxMnU0QTiakMSofPXXexi0 Rm625cv3F+Vay+GJ0+sAHEfaEFG0y5V3dJmffg+Nw1oi9JKSLiJue18vsA0MESJaqwIz/eYJwQo yYwuYMsfngh3PylMBExgg X-Received: by 2002:a05:600c:1993:b0:4bb:8711:fcb9 with SMTP id 5b1f17b1804b1-4bb88941bcemr142379225e9.33.1795287152707; Sat, 21 Nov 2026 10:52:32 -0800 (PST) Received: from denys-Ubuntu.home (207.pool90-175-214.dynamic.orange.es. [90.175.214.207]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6b9add26sm12921686f8f.44.2026.11.21.10.52.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Nov 2026 10:52:32 -0800 (PST) From: Denys Pasishnyi To: libcamera-devel@lists.libcamera.org Cc: Denys Pasishnyi Subject: [PATCH] libcamera: process: Reap the child process in the destructor Date: Sat, 21 Nov 2026 19:52:07 +0100 Message-ID: <20261121185207.1268373-2-dpcat237@gmail.com> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 --- 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 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 #include +#include #include #include #include @@ -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(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 +#include #include +#include #include #include @@ -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; }