[{"id":32082,"web_url":"https://patchwork.libcamera.org/comment/32082/","msgid":"<20241108235214.GC17916@pendragon.ideasonboard.com>","date":"2024-11-08T23:52:14","subject":"Re: [PATCH] CameraManager: Ensure we cleanup on failure","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Kieran,\n\nThank you for the patch.\n\nOn Fri, Nov 08, 2024 at 11:34:15PM +0000, Kieran Bingham wrote:\n> If the CameraManager fails to initialise at startup during\n> CameraManager::Private::init(), then the run() function will prepare the\n> thread for operations, but the thread will immediately close without\n> executing any cleanup.\n> \n> This can leave instantiated objects such as the EventNotifier registered\n> by the udev enumerator constructed in a thread which no longer exists.\n> The destructor of those objects can then fire an assertion that they are\n> being executed from an incorrect thread while performing their cleanup.\n> \n> Ensure that the failure path does not result in reporting thread\n> ownership assertions by performing cleanup correctly on the\n> CameraManager thread before it closes.\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> ---\n> This is the result of debugging an awkward error on an unrelated failure\n> path that caused the CameraManager to fail to construct:\n> \n> \"\"\"\n> camera_sensor_legacy.cpp:850 'ar0144 2-0010': The analogue crop rectangle has been defaulted to the active area size\n> [1:49:26.079788375] [922]  WARN RkISP1Agc agc.cpp:46 'AeMeteringMode' parameter not found in tuning file\n> [1:49:26.079829125] [922]  WARN RkISP1Agc agc.cpp:69 No metering modes read from tuning file; defaulting to matrix\n> [1:49:26.080311125] [921] ERROR Camera camera_manager.cpp:350 Failed to start camera manager: No such file or directory\n> Failed to start camera manager: No such file or directory\n> [1:49:26.080547875] [921] ERROR Object object.cpp:252 EventNotifier can't be enabled from another thread\n> [1:49:26.080582375] [921] FATAL default object.cpp:253 assertion \"false\" failed in assertThreadBound()\n> Backtrace:\n> /usr/lib/libcamera-base.so.0.3(_ZN9libcamera6Object17assertThreadBoundEPKc+0xe4) [0xffffa35fd884]\n> /usr/lib/libcamera-base.so.0.3(_ZN9libcamera13EventNotifier10setEnabledEb+0x24) [0xffffa3601b34]\n> /usr/lib/libcamera-base.so.0.3(_ZN9libcamera13EventNotifierD2Ev+0x50) [0xffffa3601bf0]\n> /usr/lib/libcamera-base.so.0.3(_ZN9libcamera13EventNotifierD0Ev+0x18) [0xffffa3601cb8]\n> /usr/lib/libcamera.so.0.3(_ZN9libcamera20DeviceEnumeratorUdevD2Ev+0x38) [0xffffa3798698]\n> /usr/lib/libcamera.so.0.3(_ZN9libcamera20DeviceEnumeratorUdevD0Ev+0x18) [0xffffa3798738]\n> /usr/lib/libcamera.so.0.3(+0x997cc) [0xffffa36d97cc]\n> /usr/lib/libcamera.so.0.3(_ZN9libcamera13CameraManagerD1Ev+0x134) [0xffffa36d6f04]\n> /usr/lib/libcamera.so.0.3(_ZN9libcamera13CameraManagerD0Ev+0x18) [0xffffa36d6f78]\n> cam(+0x1ba60) [0xaaaad8d9ba60]\n> cam(+0x8b64) [0xaaaad8d88b64]\n> /usr/lib/libc.so.6(+0x284b4) [0xffffa30984b4]\n> /usr/lib/libc.so.6(__libc_start_main+0x9c) [0xffffa309858c]\n> cam(+0x8fb0) [0xaaaad8d88fb0]\n> Aborted (core dumped)\n> \"\"\"\n> \n> Whilst the error \"No such file or directory\" reported above was out of\n> tree code causing the camera manager to fail - libcamera should not then\n> fail with an assertion after that while shutting down and calling\n> destructors.\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  src/libcamera/camera_manager.cpp | 4 +++-\n>  1 file changed, 3 insertions(+), 1 deletion(-)\n> \n> diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp\n> index 6280b2b3e485..a8b36bcea2d4 100644\n> --- a/src/libcamera/camera_manager.cpp\n> +++ b/src/libcamera/camera_manager.cpp\n> @@ -83,8 +83,10 @@ void CameraManager::Private::run()\n>  \tmutex_.unlock();\n>  \tcv_.notify_one();\n>  \n> -\tif (ret < 0)\n> +\tif (ret < 0) {\n> +\t\tcleanup();\n\nThe cleanup() function can deal with a partial initialization, so this\nseems fine to me.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>  \t\treturn;\n> +\t}\n>  \n>  \t/* Now start processing events and messages. */\n>  \texec();","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id C75EDBE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  8 Nov 2024 23:52:24 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DAD59657B4;\n\tSat,  9 Nov 2024 00:52:23 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D70165474\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat,  9 Nov 2024 00:52:22 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E68FB6DF;\n\tSat,  9 Nov 2024 00:52:11 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"jAfF2p5I\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1731109932;\n\tbh=nyKNonu1P5I72lgX/fTB5L16n+NxuPjgweEag5Awc/c=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=jAfF2p5I6QROKugMPGywjIctHZOcqswbC0c/OPu+d7jPRLuLZmsct+cnt72EjBbHm\n\tPg7GZkCD+bADX28alvDPdZjJdFb98lrffSFlcFK8ltF0r3R+/HDLAGrjK2urZ7IOI/\n\tJ7YR5NzUUHyqmJa9DECXtF2qc6mOBKIjtJLWJzh4=","Date":"Sat, 9 Nov 2024 01:52:14 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"libcamera devel <libcamera-devel@lists.libcamera.org>","Subject":"Re: [PATCH] CameraManager: Ensure we cleanup on failure","Message-ID":"<20241108235214.GC17916@pendragon.ideasonboard.com>","References":"<20241108233415.25782-1-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20241108233415.25782-1-kieran.bingham@ideasonboard.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":32105,"web_url":"https://patchwork.libcamera.org/comment/32105/","msgid":"<spxzasb6vc6pc2ctmp4wdvljnsddwxmaxfb2p6ohdt7k2p4syg@5ae5hrg3tiqj>","date":"2024-11-11T13:45:40","subject":"Re: [PATCH] CameraManager: Ensure we cleanup on failure","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Kieran\n\nOn Fri, Nov 08, 2024 at 11:34:15PM +0000, Kieran Bingham wrote:\n> If the CameraManager fails to initialise at startup during\n> CameraManager::Private::init(), then the run() function will prepare the\n> thread for operations, but the thread will immediately close without\n> executing any cleanup.\n>\n> This can leave instantiated objects such as the EventNotifier registered\n> by the udev enumerator constructed in a thread which no longer exists.\n> The destructor of those objects can then fire an assertion that they are\n> being executed from an incorrect thread while performing their cleanup.\n>\n> Ensure that the failure path does not result in reporting thread\n> ownership assertions by performing cleanup correctly on the\n> CameraManager thread before it closes.\n>\n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n>\n\nSeems sane to me!\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\nThanks\n  j\n\n> ---\n> This is the result of debugging an awkward error on an unrelated failure\n> path that caused the CameraManager to fail to construct:\n>\n> \"\"\"\n> camera_sensor_legacy.cpp:850 'ar0144 2-0010': The analogue crop rectangle has been defaulted to the active area size\n> [1:49:26.079788375] [922]  WARN RkISP1Agc agc.cpp:46 'AeMeteringMode' parameter not found in tuning file\n> [1:49:26.079829125] [922]  WARN RkISP1Agc agc.cpp:69 No metering modes read from tuning file; defaulting to matrix\n> [1:49:26.080311125] [921] ERROR Camera camera_manager.cpp:350 Failed to start camera manager: No such file or directory\n> Failed to start camera manager: No such file or directory\n> [1:49:26.080547875] [921] ERROR Object object.cpp:252 EventNotifier can't be enabled from another thread\n> [1:49:26.080582375] [921] FATAL default object.cpp:253 assertion \"false\" failed in assertThreadBound()\n> Backtrace:\n> /usr/lib/libcamera-base.so.0.3(_ZN9libcamera6Object17assertThreadBoundEPKc+0xe4) [0xffffa35fd884]\n> /usr/lib/libcamera-base.so.0.3(_ZN9libcamera13EventNotifier10setEnabledEb+0x24) [0xffffa3601b34]\n> /usr/lib/libcamera-base.so.0.3(_ZN9libcamera13EventNotifierD2Ev+0x50) [0xffffa3601bf0]\n> /usr/lib/libcamera-base.so.0.3(_ZN9libcamera13EventNotifierD0Ev+0x18) [0xffffa3601cb8]\n> /usr/lib/libcamera.so.0.3(_ZN9libcamera20DeviceEnumeratorUdevD2Ev+0x38) [0xffffa3798698]\n> /usr/lib/libcamera.so.0.3(_ZN9libcamera20DeviceEnumeratorUdevD0Ev+0x18) [0xffffa3798738]\n> /usr/lib/libcamera.so.0.3(+0x997cc) [0xffffa36d97cc]\n> /usr/lib/libcamera.so.0.3(_ZN9libcamera13CameraManagerD1Ev+0x134) [0xffffa36d6f04]\n> /usr/lib/libcamera.so.0.3(_ZN9libcamera13CameraManagerD0Ev+0x18) [0xffffa36d6f78]\n> cam(+0x1ba60) [0xaaaad8d9ba60]\n> cam(+0x8b64) [0xaaaad8d88b64]\n> /usr/lib/libc.so.6(+0x284b4) [0xffffa30984b4]\n> /usr/lib/libc.so.6(__libc_start_main+0x9c) [0xffffa309858c]\n> cam(+0x8fb0) [0xaaaad8d88fb0]\n> Aborted (core dumped)\n> \"\"\"\n>\n> Whilst the error \"No such file or directory\" reported above was out of\n> tree code causing the camera manager to fail - libcamera should not then\n> fail with an assertion after that while shutting down and calling\n> destructors.\n>\n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  src/libcamera/camera_manager.cpp | 4 +++-\n>  1 file changed, 3 insertions(+), 1 deletion(-)\n>\n> diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp\n> index 6280b2b3e485..a8b36bcea2d4 100644\n> --- a/src/libcamera/camera_manager.cpp\n> +++ b/src/libcamera/camera_manager.cpp\n> @@ -83,8 +83,10 @@ void CameraManager::Private::run()\n>  \tmutex_.unlock();\n>  \tcv_.notify_one();\n>\n> -\tif (ret < 0)\n> +\tif (ret < 0) {\n> +\t\tcleanup();\n>  \t\treturn;\n> +\t}\n>\n>  \t/* Now start processing events and messages. */\n>  \texec();\n> --\n> 2.47.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id C4872BE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 11 Nov 2024 13:45:49 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C80A6657D8;\n\tMon, 11 Nov 2024 14:45:48 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E894465486\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 11 Nov 2024 14:45:46 +0100 (CET)","from ideasonboard.com (mob-5-90-143-109.net.vodafone.it\n\t[5.90.143.109])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 90C6F6AF;\n\tMon, 11 Nov 2024 14:45:34 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"sMplAed3\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1731332735;\n\tbh=IKJ3NIkLTXi8uzP9FvaughIMRXjpcxkBN6p0ozEo3aw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=sMplAed37WYwYj6t0EAcFtFQT/K7NjVhh4bvAQedX/4PFy4kpYRxEHsd9hr42iykT\n\tJ9yrxg2I8rEItNGDV8andY93fwxkp7jMEqTQiuDnoMSuNfnuzsjGDu5fOvQg/QBo3o\n\tayitP3D8wbkKIXHqUNPq8Zug0gxPj8h+U3CLoJRg=","Date":"Mon, 11 Nov 2024 14:45:40 +0100","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"libcamera devel <libcamera-devel@lists.libcamera.org>","Subject":"Re: [PATCH] CameraManager: Ensure we cleanup on failure","Message-ID":"<spxzasb6vc6pc2ctmp4wdvljnsddwxmaxfb2p6ohdt7k2p4syg@5ae5hrg3tiqj>","References":"<20241108233415.25782-1-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20241108233415.25782-1-kieran.bingham@ideasonboard.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]