[v2,3/4] libcamera: pipeline_handler: Add an assertion to detect bad synchronization
diff mbox series

Message ID 20261202141623.1375959-4-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • libcamera: atomic-disconnects
Related show

Commit Message

Barnabás Pőcze Dec. 2, 2026, 2:16 p.m. UTC
From: Stefan Klug <stefan.klug@ideasonboard.com>

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 <stefan.klug@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 include/libcamera/internal/camera.h |  4 +++-
 src/libcamera/camera.cpp            | 18 +++++++++++++++---
 src/libcamera/pipeline_handler.cpp  | 11 +++++++++++
 3 files changed, 29 insertions(+), 4 deletions(-)

Patch
diff mbox series

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<PipelineHandler> 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);
 }