{"id":19452,"url":"https://patchwork.libcamera.org/api/1.1/patches/19452/?format=json","web_url":"https://patchwork.libcamera.org/patch/19452/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240123011249.22716-5-laurent.pinchart@ideasonboard.com>","date":"2024-01-23T01:12:41","name":"[v2,04/12] libcamera: thread: Ensure deferred deletion of all objects before stopping","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"f76cdcd673149e2e689882d33f6f3e1c6873eb26","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/19452/mbox/","series":[{"id":4157,"url":"https://patchwork.libcamera.org/api/1.1/series/4157/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4157","date":"2024-01-23T01:12:37","name":"libcamera: Hardening against thread race conditions","version":2,"mbox":"https://patchwork.libcamera.org/series/4157/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19452/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19452/checks/","tags":{},"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 BB774C32BE\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 23 Jan 2024 01:12:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 89C2F62955;\n\tTue, 23 Jan 2024 02:12:53 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 793F06294A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 23 Jan 2024 02:12:51 +0100 (CET)","from pendragon.ideasonboard.com (89-27-53-110.bb.dnainternet.fi\n\t[89.27.53.110])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 33CB4E4;\n\tTue, 23 Jan 2024 02:11:38 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"XRUGVev6\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1705972298;\n\tbh=v+MZe0izlr8b1qKd6q3sOfkw+6ac0wk5IMNQfH6J0LY=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=XRUGVev6EsMac/5eQqO8SZ5CZcZdSLaVTaW/tB+zMKzgelWOzBqJ3ywCJy9ZgzLLG\n\tQUfJxbXvxR5HIttnaX7gQtuMtNIAysFdZKHsJsQ6FiE+s0eAx/uuXVh1o7L6/2W0bu\n\tcYvIZGdt14Wm5KayDnlEhPknq4oB42oFpQ0R4KI0=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[PATCH v2 04/12] libcamera: thread: Ensure deferred deletion of all\n\tobjects before stopping","Date":"Tue, 23 Jan 2024 03:12:41 +0200","Message-ID":"<20240123011249.22716-5-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20240123011249.22716-1-laurent.pinchart@ideasonboard.com>","References":"<20240123011249.22716-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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>"},"content":"Objects can be scheduled for deletion with Object::deleteLater(), which\nqueues a deferred deletion to the thread's event loop. As the\ndeleteLater() function is meant to be called from a different thread,\nthis may race with thread termination, and deferred deletions queued\njust before calling Thread::exit() may not be processed by the event\nloop. Make sure they get processed when finishing the thread, before\nstopping.\n\nThis eliminates the race condition that occurs when calling\nObject::deleteLater() followed by Thread::exit() from the same thread.\nCalling deleteLater() from neither the thread the object is bound to or\nthe thread calling Thread::exit() is still inherently racy.\n\nThe change fixes a failure in the object-delete unit test.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges since v1:\n\n- Expand commit message\n- Update the Object::deleteLater() documentation\n---\n src/libcamera/base/object.cpp | 5 +++--\n src/libcamera/base/thread.cpp | 6 ++++++\n 2 files changed, 9 insertions(+), 2 deletions(-)","diff":"diff --git a/src/libcamera/base/object.cpp b/src/libcamera/base/object.cpp\nindex 1fce5a2af9af..14399d750e03 100644\n--- a/src/libcamera/base/object.cpp\n+++ b/src/libcamera/base/object.cpp\n@@ -116,8 +116,9 @@ Object::~Object()\n  * event loop that the object belongs to. This ensures the object is destroyed\n  * from the right context, as required by the libcamera threading model.\n  *\n- * If this function is called before the thread's event loop is started, the\n- * object will be deleted when the event loop starts.\n+ * If this function is called before the thread's event loop is started or after\n+ * it has stopped, the object will be deleted when the event loop (re)starts. If\n+ * this never occurs, the object will be leaked.\n  *\n  * Deferred deletion can be used to control the destruction context with shared\n  * pointers. An object managed with shared pointers is deleted when the last\ndiff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp\nindex 75693c92a0b1..4ac72036aa69 100644\n--- a/src/libcamera/base/thread.cpp\n+++ b/src/libcamera/base/thread.cpp\n@@ -371,6 +371,12 @@ void Thread::run()\n \n void Thread::finishThread()\n {\n+\t/*\n+\t * Objects may have been scheduled for deletion right before the thread\n+\t * exited. Ensure they get deleted now, before the thread stops.\n+\t */\n+\tdispatchMessages(Message::Type::DeferredDelete);\n+\n \tdata_->mutex_.lock();\n \tdata_->running_ = false;\n \tdata_->mutex_.unlock();\n","prefixes":["v2","04/12"]}