From patchwork Wed Dec 2 14:16:20 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 29132 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 7B061C3300 for ; Wed, 2 Dec 2026 14:16:33 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 23E456AADD; Wed, 2 Dec 2026 15:16:29 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="eWOmmN3z"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C69746AAD1 for ; Wed, 2 Dec 2026 15:16:26 +0100 (CET) Received: from pb-laptop.local (185.182.215.118.nat.pool.zt.hu [185.182.215.118]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id E63B54BB for ; Wed, 2 Dec 2026 15:13:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1796220828; bh=Di9u++UgcOcXW6MprQ/V1daHKX5jWzl9GU/zXr1VaS4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=eWOmmN3zXh6iVgHT1eOqjB8QJBBaGXg3Rfi1aReSPFT+RAXkyhXdgwZUVAKDkucUT g1+wh4fav8RvUkssplc4j6HsEipIGtDiV5Kn89IWYYXjCSW6+AbJ+MyGKextvYuYTB t0DJ7PgnhmQIR/sbSg+jyyPNG/X50tN2Ft5cUEnA= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2 1/4] libcamera: v4l2_videodevice: streamOff(): Handle device disconnection Date: Wed, 2 Dec 2026 15:16:20 +0100 Message-ID: <20261202141623.1375959-2-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202141623.1375959-1-barnabas.pocze@ideasonboard.com> References: <20261202141623.1375959-1-barnabas.pocze@ideasonboard.com> 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 If the `VIDIOC_STREAMOFF` reports `ENODEV`, meaning that the device is no longer available/connected, then proceed normally and cancel all pending buffers, and do not report an error. For example, this relevant for UVC, as `VIDIOC_STREAMOFF` will report `ENODEV` after disconnection, and returning buffers is crucial as the "uvcvideo" depends on `streamOff()` returning all pending buffers. Otherwise some requests will not be completed, leading to an assertion failure in `PipelineHandler::stop()`. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham --- src/libcamera/v4l2_videodevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 41dc5d0f65..35408fa13d 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -2031,7 +2031,7 @@ int V4L2VideoDevice::streamOff() watchdog_.stop(); ret = ioctl(VIDIOC_STREAMOFF, &bufferType_); - if (ret < 0) { + if (ret < 0 && ret != -ENODEV) { LOG(V4L2, Error) << "Failed to stop streaming: " << strerror(-ret); return ret; From patchwork Wed Dec 2 14:16:21 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 29133 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 C9B28C3355 for ; Wed, 2 Dec 2026 14:16:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 309A26AAE1; Wed, 2 Dec 2026 15:16:30 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="rAbAYNED"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 16DAF6AAD3 for ; Wed, 2 Dec 2026 15:16:27 +0100 (CET) Received: from pb-laptop.local (185.182.215.118.nat.pool.zt.hu [185.182.215.118]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2AD0182A; Wed, 2 Dec 2026 15:13:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1796220828; bh=1kc6/DpzHvrnK7QV+ttIiLteKyJQjN0S7482Weeb+ic=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rAbAYNEDyXRtKmMyuMUEhasZfAQM4Xr71elXrIAdvJEfo2JO6jEyqYCJQwewVS9Je iXVP0FwgK1XvLqeVwMaTlur5FSO+aSJ95+qe/1g3C+DxX3McoHwQtRHxNb6piifZ11 FHLtz8AqSlSQOow0xYFGflWARp363FNFFFEUKtKk= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Kieran Bingham Subject: [PATCH v2 2/4] libcamera: camera: Make `disconnected_` atomic Date: Wed, 2 Dec 2026 15:16:21 +0100 Message-ID: <20261202141623.1375959-3-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202141623.1375959-1-barnabas.pocze@ideasonboard.com> References: <20261202141623.1375959-1-barnabas.pocze@ideasonboard.com> 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 This object is read in the application thread (`isAccessAllowed()`) and written in the libcamera thread (`disconnect()`). Since it is not atomic, doing so is a data race, which is considered undefined behaviour. So make it atomic for correctness. Since it is not used for synchronization, using "relaxed" memory ordering is sufficient. Signed-off-by: Barnabás Pőcze Signed-off-by: Kieran Bingham Reviewed-by: Kieran Bingham --- include/libcamera/internal/camera.h | 2 +- src/libcamera/camera.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/libcamera/internal/camera.h b/include/libcamera/internal/camera.h index 8a2e9ed589..8af39047b4 100644 --- a/include/libcamera/internal/camera.h +++ b/include/libcamera/internal/camera.h @@ -70,7 +70,7 @@ private: std::set streams_; std::set activeStreams_; - bool disconnected_; + std::atomic disconnected_; std::atomic state_; std::unique_ptr validator_; diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 9dadc91a38..2b179745aa 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -690,7 +690,7 @@ bool Camera::Private::isRunning() const int Camera::Private::isAccessAllowed(State state, bool allowDisconnected, const char *from) const { - if (!allowDisconnected && disconnected_) + if (!allowDisconnected && disconnected_.load(std::memory_order_relaxed)) return -ENODEV; State currentState = state_.load(std::memory_order_acquire); @@ -710,7 +710,7 @@ int Camera::Private::isAccessAllowed(State low, State high, bool allowDisconnected, const char *from) const { - if (!allowDisconnected && disconnected_) + if (!allowDisconnected && disconnected_.load(std::memory_order_relaxed)) return -ENODEV; State currentState = state_.load(std::memory_order_acquire); @@ -739,7 +739,7 @@ void Camera::Private::disconnect() if (state_.load(std::memory_order_acquire) == Private::CameraRunning) state_.store(Private::CameraConfigured, std::memory_order_release); - disconnected_ = true; + disconnected_.store(true, std::memory_order_relaxed); } void Camera::Private::setState(State state) From patchwork Wed Dec 2 14:16:22 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 29134 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 D84C7C3300 for ; Wed, 2 Dec 2026 14:16:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D2F786AADB; Wed, 2 Dec 2026 15:16:31 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="pdBimIm8"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9EF366AAD7 for ; Wed, 2 Dec 2026 15:16:27 +0100 (CET) Received: from pb-laptop.local (185.182.215.118.nat.pool.zt.hu [185.182.215.118]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 70CCD3A4; Wed, 2 Dec 2026 15:13:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1796220828; bh=3Cv18XXFe//oHZqFI1jM41uh9qnYXMekQy3apxhV/L0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pdBimIm82+Hi3Qhhj/OwhPNC9FQjCIN9QNzAew4AT7/YVPD9qj/AMPhRXilhbob46 TlutGiiOChLhQtO0OLQBDg6mDUpxAWenrLxca7wbsLhVgK+8F9tACKE1vHg7sfDeA3 Sb1hQJ9iZ2Q9XNNWAwnbWJI8JHeDHrye+3kCKn50= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug , Kieran Bingham Subject: [PATCH v2 3/4] libcamera: pipeline_handler: Add an assertion to detect bad synchronization Date: Wed, 2 Dec 2026 15:16:22 +0100 Message-ID: <20261202141623.1375959-4-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202141623.1375959-1-barnabas.pocze@ideasonboard.com> References: <20261202141623.1375959-1-barnabas.pocze@ideasonboard.com> 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 From: Stefan Klug If the application does not synchronize calls to Camera::queueRequest() and Camera::stop(), bad things might happen. Add an assert to detect that. Signed-off-by: Stefan Klug Signed-off-by: Kieran Bingham Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham --- include/libcamera/internal/camera.h | 4 +++- src/libcamera/camera.cpp | 18 +++++++++++++++--- src/libcamera/pipeline_handler.cpp | 11 +++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/include/libcamera/internal/camera.h b/include/libcamera/internal/camera.h index 8af39047b4..f44b15c7da 100644 --- a/include/libcamera/internal/camera.h +++ b/include/libcamera/internal/camera.h @@ -45,6 +45,8 @@ public: const CameraControlValidator *validator() const { return validator_.get(); } + bool isRunningOrStopping() const; + private: enum State { CameraAvailable, @@ -55,7 +57,6 @@ private: }; bool isAcquired() const; - bool isRunning() const; int isAccessAllowed(State state, bool allowDisconnected = false, const char *from = __builtin_FUNCTION()) const; int isAccessAllowed(State low, State high, @@ -64,6 +65,7 @@ private: void disconnect(); void setState(State state); + State state() const { return state_.load(std::memory_order_acquire); } std::shared_ptr pipe_; std::string id_; diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 2b179745aa..f45c80cc7c 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -682,9 +682,21 @@ bool Camera::Private::isAcquired() const return state_.load(std::memory_order_acquire) != CameraAvailable; } -bool Camera::Private::isRunning() const +/** + * \brief Check if the camera running or stopping + * + * This functions checks if the camera's current state is either + * CameraRunning or CameraStopping. + * + * \return True if the camera is running or stopping + */ +bool Camera::Private::isRunningOrStopping() const { - return state_.load(std::memory_order_acquire) == CameraRunning; + State currentState = state_.load(std::memory_order_acquire); + if (currentState == CameraRunning || currentState == CameraStopping) + return true; + + return false; } int Camera::Private::isAccessAllowed(State state, bool allowDisconnected, @@ -1450,7 +1462,7 @@ int Camera::stop() * \todo Make calling stop() when not in 'Running' part of the state * machine rather than take this shortcut */ - if (!d->isRunning()) + if (d->state() != Private::CameraRunning) return 0; int ret = d->isAccessAllowed(Private::CameraRunning); diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index a803f6208c..7cddda3e1f 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -474,6 +474,17 @@ void PipelineHandler::queueRequest(Request *request) Camera::Private *data = camera->_d(); data->waitingRequests_.push(request); + /* + * If we are running into this assert, the calling application didn't + * synchronize calls to Camera::queueRequest() and Camera::stop() and a + * PipelineHandler::queueRequest() invocation was queued after + * PipelineHandler::stop(). We can't guard against that in the Camera + * class as it is lock free. Synchronization happens when the Camera + * class invokes PipelineHandler::stop() in blocking mode. As such a + * race leads to difficult to debug issues, assert here. + */ + ASSERT(data->isRunningOrStopping()); + request->_d()->prepare(300ms); } From patchwork Wed Dec 2 14:16:23 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 29135 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 6DAA6C3361 for ; Wed, 2 Dec 2026 14:16:36 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 940646AAE3; Wed, 2 Dec 2026 15:16:32 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="AZLY/qJW"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2CEB46AAD3 for ; Wed, 2 Dec 2026 15:16:28 +0100 (CET) Received: from pb-laptop.local (185.182.215.118.nat.pool.zt.hu [185.182.215.118]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 140094BB; Wed, 2 Dec 2026 15:13:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1796220829; bh=EXV8EPkli2dOQcBqZ9evFAbdM4W7VqHPoStTWZLrTC0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AZLY/qJWJAsweOrUvk+XQSVpNjHFK3tjvnpSVgpZikz/nBqCgI7EylTQahtKKLmG7 dvQgEg++atbW2w+2jSjdQd+ejUvXauCupIHPqK29bTV8CdnKlzpmluGclIDrzaMBho OA20Z7IHgFS8HwV6zv5ZDtPuM2InOPyPvYUlq7HE= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug , Kieran Bingham Subject: [PATCH v2 4/4] libcamera: pipeline_handler: Allow calling stop() on a disconnected camera Date: Wed, 2 Dec 2026 15:16:23 +0100 Message-ID: <20261202141623.1375959-5-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202141623.1375959-1-barnabas.pocze@ideasonboard.com> References: <20261202141623.1375959-1-barnabas.pocze@ideasonboard.com> 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 From: Stefan Klug Calling stop() on a disconnected camera currently has no effect. However, this is problematic as the disconnection procedure does not stop the camera either, meaning that PipelineHandler::stop() does not run for a disconnected camera. This is a problem as it prevents the pipeline handler from properly stopping streaming and for already queued requests to be returned as cancelled. So modify Camera::stop() so that it proceeds for disconnected cameras if they are otherwise in the "Running" state. And adjust the documentation accordingly. Note that the camera cannot be stopped when the disconnection is processed because concurrent queueRequest() calls by the application can run into various race conditions. So this change makes it the application's responsibility to stop a running camera, even if it was disconnected. Signed-off-by: Stefan Klug Signed-off-by: Kieran Bingham Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham --- src/libcamera/camera.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index f45c80cc7c..6f277fb872 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -743,14 +743,6 @@ int Camera::Private::isAccessAllowed(State low, State high, void Camera::Private::disconnect() { - /* - * If the camera was running when the hardware was removed force the - * state to Configured state to allow applications to free resources - * and call release() before deleting the camera. - */ - if (state_.load(std::memory_order_acquire) == Private::CameraRunning) - state_.store(Private::CameraConfigured, std::memory_order_release); - disconnected_.store(true, std::memory_order_relaxed); } @@ -935,7 +927,9 @@ const std::string &Camera::id() const * physical device disconnection. The media device is passed as a parameter. * * As soon as this signal is emitted the camera instance will refuse all new - * application API calls by returning errors immediately. + * application API calls by returning errors immediately. The only exception is + * the stop() function which must be called on the camera if it was in running + * state before, to ensure all resources are released. */ Camera::Camera(std::unique_ptr d, const std::string &id, @@ -1451,7 +1445,6 @@ int Camera::start(const ControlList *controls) * running, it is a no-op. * * \return 0 on success or a negative error code otherwise - * \retval -ENODEV The camera has been disconnected from the system * \retval -EACCES The camera is not running so can't be stopped */ int Camera::stop() @@ -1465,7 +1458,7 @@ int Camera::stop() if (d->state() != Private::CameraRunning) return 0; - int ret = d->isAccessAllowed(Private::CameraRunning); + int ret = d->isAccessAllowed(Private::CameraRunning, true); if (ret < 0) return ret;