[{"id":28584,"web_url":"https://patchwork.libcamera.org/comment/28584/","msgid":"<87a5owb6xi.fsf@redhat.com>","date":"2024-01-23T11:55:21","subject":"Re: [PATCH v2 04/12] libcamera: thread: Ensure deferred deletion of\n\tall objects before stopping","submitter":{"id":177,"url":"https://patchwork.libcamera.org/api/people/177/","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"content":"Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:\n\n> Objects can be scheduled for deletion with Object::deleteLater(), which\n> queues a deferred deletion to the thread's event loop. As the\n> deleteLater() function is meant to be called from a different thread,\n> this may race with thread termination, and deferred deletions queued\n> just before calling Thread::exit() may not be processed by the event\n> loop. Make sure they get processed when finishing the thread, before\n> stopping.\n>\n> This eliminates the race condition that occurs when calling\n> Object::deleteLater() followed by Thread::exit() from the same thread.\n> Calling deleteLater() from neither the thread the object is bound to or\n> the thread calling Thread::exit() is still inherently racy.\n>\n> The change fixes a failure in the object-delete unit test.\n>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\n\n> ---\n> Changes 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(-)\n>\n> diff --git a/src/libcamera/base/object.cpp b/src/libcamera/base/object.cpp\n> index 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\n> diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp\n> index 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();","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 4DE9EC323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 23 Jan 2024 11:55:31 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2172262947;\n\tTue, 23 Jan 2024 12:55:30 +0100 (CET)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 20B24628E9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 23 Jan 2024 12:55:28 +0100 (CET)","from mail-wr1-f70.google.com (mail-wr1-f70.google.com\n\t[209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-333-sSMQBQA0M4CUbLoZHraoJw-1; Tue, 23 Jan 2024 06:55:24 -0500","by mail-wr1-f70.google.com with SMTP id\n\tffacd0b85a97d-337cef39f65so2185367f8f.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 23 Jan 2024 03:55:23 -0800 (PST)","from nuthatch (nat-pool-brq-t.redhat.com. [213.175.37.10])\n\tby smtp.gmail.com with ESMTPSA id\n\tbl1-20020adfe241000000b0033935779a23sm5613913wrb.89.2024.01.23.03.55.21\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 23 Jan 2024 03:55:21 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"BsnLSg0n\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1706010927;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=NVHP+giJzmbYs8hxleU8v0W7huKsNwB9dEzT3Sv9dAo=;\n\tb=BsnLSg0ndglA03ICJix1vgG0jaP5ms2VmP1R9Xj4jbPCoAYTvcrKbyBR21cc9+sMMujy85\n\twZe0dwy0+G+O3+pj1GSeneF9ZmxjEZbofDvY+gb5gcDbpdw6RfpTnomorosIIUXanbIU3W\n\tO5sDru4trnI4sKL3R+b21KytGsPeFGg=","X-MC-Unique":"sSMQBQA0M4CUbLoZHraoJw-1","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1706010922; x=1706615722;\n\th=mime-version:user-agent:message-id:date:references:in-reply-to\n\t:subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=NVHP+giJzmbYs8hxleU8v0W7huKsNwB9dEzT3Sv9dAo=;\n\tb=Bq+WC32aFDcaVGx6o5dbZ9JdVpR9iQTjkkbqDIpAJZcshSob0lZ6XquOZGvFYYgb7T\n\tMyVzAUaUC0W/L7TcnNCHhr5zkIjrxsO3rZFyfWWGJF+eYhhYftYwrRpKlwG7x8VHLQji\n\ttHDAzced2a0dqk2vlEx5oPl5XL7JekBLOI8PxbneQhLAro4aw8bUGX/G96lDRJyeo/T7\n\tIHfQ+QCrA3qv0beVDn/HmYdo5IuHsOcTo1E4AXene6xMWDEzeceaz0G3GWNU9kDB1bbL\n\tvKL6dYvTCzv7eUGn4n8noKjxyr0SprROBYcPh3hcvO6ryEvBJy1KsQOoSHMKI839REiI\n\tc0tQ==","X-Gm-Message-State":"AOJu0YxmNu8uGzPiLshyeRxZI8oZQnVBNn131BKbtjXRYNf4UVkhUs5c\n\tWuAhJcwGEmieoP1f96txd33gk6q2ekqAkEwG9SZvh0L+u2GW2BredZ2vhU6i1tZkynbI21e3nqt\n\tW7AgOVfmwu2PHceeHSm69Chn3VCVvf1sbzAM58ojg/j9phA2YcOBK2mPHuPsMnY1XT8dfNAKWYR\n\t6V7BQuVcV1YNPX/TeNXg9KOByOmO6dYDhuNPCA8V6zgbwjaTHfLIEnNHQ=","X-Received":["by 2002:a05:600c:1c18:b0:40e:44a9:c2d3 with SMTP id\n\tj24-20020a05600c1c1800b0040e44a9c2d3mr55091wms.164.1706010922400; \n\tTue, 23 Jan 2024 03:55:22 -0800 (PST)","by 2002:a05:600c:1c18:b0:40e:44a9:c2d3 with SMTP id\n\tj24-20020a05600c1c1800b0040e44a9c2d3mr55083wms.164.1706010922063; \n\tTue, 23 Jan 2024 03:55:22 -0800 (PST)"],"X-Google-Smtp-Source":"AGHT+IHRYQC/kCv3rHUbMho58OFzcKCnLV2mcxw2M5qNFq1FyYBbXDSXWumIOGbZvEKNnVdXNpoHRQ==","From":"Milan Zamazal <mzamazal@redhat.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Subject":"Re: [PATCH v2 04/12] libcamera: thread: Ensure deferred deletion of\n\tall objects before stopping","In-Reply-To":"<20240123011249.22716-5-laurent.pinchart@ideasonboard.com>\n\t(Laurent Pinchart's message of \"Tue, 23 Jan 2024 03:12:41 +0200\")","References":"<20240123011249.22716-1-laurent.pinchart@ideasonboard.com>\n\t<20240123011249.22716-5-laurent.pinchart@ideasonboard.com>","Date":"Tue, 23 Jan 2024 12:55:21 +0100","Message-ID":"<87a5owb6xi.fsf@redhat.com>","User-Agent":"Gnus/5.13 (Gnus v5.13)","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain","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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]