From patchwork Sun May 23 02:33:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12368 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 510D5C3201 for ; Sun, 23 May 2021 02:33:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EA5C16891A; Sun, 23 May 2021 04:33:49 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ITlKT5ds"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 16F3960510 for ; Sun, 23 May 2021 04:33:49 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 84A982A8; Sun, 23 May 2021 04:33:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1621737228; bh=L5kTxSM6vCCkM0eLdem83a0TViHxARO0NvRH8ABhBHk=; h=From:To:Cc:Subject:Date:From; b=ITlKT5dsZ6ti0Um/mtTNlbBWGJhq+kPwbfSbixNoTDT8DksDMGw+auEoQxEm6yZ9h 2zmc5QWB7m++bv0QgVgYCZaF0p0Giw4B/h2CVQcO7tZqJfSo/DDm3dc4DYJbiZpxG3 BupngHMBaXS9tN8xzOPIRQ6QTBKWAvJ599o/suhg= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sun, 23 May 2021 05:33:42 +0300 Message-Id: <20210523023342.13141-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] android: camera_worker: Process all queued requests when stopping 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" When stopping the camera worker, queuedRequest() calls may have queued asynchronous function invocation messages to the worker thread, and some of those messages may not have been processed yet. The messages will stay in the thread's queue until the camera worker is restarted (when the camera service will start a new capture session). At that point, they will be dispatched, which will cause a crash due to the CaptureRequest passed to processRequest() having been deleted by CameraDevice::stop() calling descriptors_.clear(). Fix this by forcing dispatching of all function invocation messages when stopping the camera worker thread. Note that this is inherently racy, as more queueRequest() calls may arrive from the camera service while we're stopping. This race condition will be addressed by a subsequent patch series. Signed-off-by: Laurent Pinchart Tested-by: Hirokazu Honda Reviewed-by: Jacopo Mondi Reviewed-by: Hirokazu Honda --- I haven't tested this patch, as I'm currently rebuilding a new Soraka image. Hiro, could you give it a try to see if it fixes the problem you've reported ? --- src/android/camera_worker.cpp | 14 ++++++++++---- src/android/camera_worker.h | 6 ++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/android/camera_worker.cpp b/src/android/camera_worker.cpp index 300ddde03645..9f727826e23f 100644 --- a/src/android/camera_worker.cpp +++ b/src/android/camera_worker.cpp @@ -52,18 +52,24 @@ void CaptureRequest::queue() */ CameraWorker::CameraWorker() { - worker_.moveToThread(&thread_); + worker_.moveToThread(this); } void CameraWorker::start() { - thread_.start(); + Thread::start(); } void CameraWorker::stop() { - thread_.exit(); - thread_.wait(); + exit(); + wait(); +} + +void CameraWorker::run() +{ + exec(); + dispatchMessages(Message::Type::InvokeMessage); } void CameraWorker::queueRequest(CaptureRequest *request) diff --git a/src/android/camera_worker.h b/src/android/camera_worker.h index 64b1658b61b4..e289ef9b8655 100644 --- a/src/android/camera_worker.h +++ b/src/android/camera_worker.h @@ -42,7 +42,7 @@ private: std::unique_ptr request_; }; -class CameraWorker +class CameraWorker : private libcamera::Thread { public: CameraWorker(); @@ -52,6 +52,9 @@ public: void queueRequest(CaptureRequest *request); +protected: + void run() override; + private: class Worker : public libcamera::Object { @@ -63,7 +66,6 @@ private: }; Worker worker_; - libcamera::Thread thread_; }; #endif /* __ANDROID_CAMERA_WORKER_H__ */