libcamera: camera_manager: Non-fatal duplicate id detection
diff mbox series

Message ID 20261026165640.3755874-1-kieran.bingham@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: camera_manager: Non-fatal duplicate id detection
Related show

Commit Message

Kieran Bingham Oct. 26, 2026, 4:56 p.m. UTC
There are some hardware configurations which currently generate a
duplicate camera ID when the camera is on a shared but multiplexed bus.

At present this is somewhat difficult to adapt to ensure a consistent
and unique id.

While this case is undesired, it presently causes libcamera to hit a
Fatal asssertion on real world hardware which then causes pipewire to
close.

This behaviour is even less desirable, and we can easily continue to
operate libcamera without a potentially duplicate camera.

Reduce the Fatal log level to an Error allowing the code to continue
without constructing this duplicate camera.

Closes: https://gitlab.freedesktop.org/camera/libcamera/-/work_items/303
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 src/libcamera/camera_manager.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Barnabás Pőcze Oct. 26, 2026, 5:02 p.m. UTC | #1
2026. 10. 26. 17:56 keltezéssel, Kieran Bingham írta:
> There are some hardware configurations which currently generate a
> duplicate camera ID when the camera is on a shared but multiplexed bus.
> 
> At present this is somewhat difficult to adapt to ensure a consistent
> and unique id.
> 
> While this case is undesired, it presently causes libcamera to hit a
> Fatal asssertion on real world hardware which then causes pipewire to
> close.
> 
> This behaviour is even less desirable, and we can easily continue to
> operate libcamera without a potentially duplicate camera.
> 
> Reduce the Fatal log level to an Error allowing the code to continue
> without constructing this duplicate camera.
> 
> Closes: https://gitlab.freedesktop.org/camera/libcamera/-/work_items/303

Could this be "downgraded" to "Link" or similar? I would like to keep the
issue open.

Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>



> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> ---
>   src/libcamera/camera_manager.cpp | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
> index 0dd4e0c590a1..9c9073053710 100644
> --- a/src/libcamera/camera_manager.cpp
> +++ b/src/libcamera/camera_manager.cpp
> @@ -210,7 +210,7 @@ void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
>   
>   		for (const std::shared_ptr<Camera> &c : cameras_) {
>   			if (c->id() == camera->id()) {
> -				LOG(Camera, Fatal)
> +				LOG(Camera, Error)
>   					<< "Trying to register a camera with a duplicated ID '"
>   					<< camera->id() << "'";
>   				return;
Kieran Bingham Oct. 26, 2026, 7:30 p.m. UTC | #2
Quoting Barnabás Pőcze (2026-10-26 17:02:46)
> 2026. 10. 26. 17:56 keltezéssel, Kieran Bingham írta:
> > There are some hardware configurations which currently generate a
> > duplicate camera ID when the camera is on a shared but multiplexed bus.
> > 
> > At present this is somewhat difficult to adapt to ensure a consistent
> > and unique id.
> > 
> > While this case is undesired, it presently causes libcamera to hit a
> > Fatal asssertion on real world hardware which then causes pipewire to
> > close.
> > 
> > This behaviour is even less desirable, and we can easily continue to
> > operate libcamera without a potentially duplicate camera.
> > 
> > Reduce the Fatal log level to an Error allowing the code to continue
> > without constructing this duplicate camera.
> > 
> > Closes: https://gitlab.freedesktop.org/camera/libcamera/-/work_items/303
> 
> Could this be "downgraded" to "Link" or similar? I would like to keep the
> issue open.

Yes, that's ok for me. Will change when merging unless there are other
concerns.

> 
> Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
> 
> > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> > ---
> >   src/libcamera/camera_manager.cpp | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
> > index 0dd4e0c590a1..9c9073053710 100644
> > --- a/src/libcamera/camera_manager.cpp
> > +++ b/src/libcamera/camera_manager.cpp
> > @@ -210,7 +210,7 @@ void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
> >   
> >               for (const std::shared_ptr<Camera> &c : cameras_) {
> >                       if (c->id() == camera->id()) {
> > -                             LOG(Camera, Fatal)
> > +                             LOG(Camera, Error)
> >                                       << "Trying to register a camera with a duplicated ID '"
> >                                       << camera->id() << "'";
> >                               return;
>
Laurent Pinchart Oct. 27, 2026, 11:08 a.m. UTC | #3
On Mon, Oct 26, 2026 at 07:30:46PM +0000, Kieran Bingham wrote:
> Quoting Barnabás Pőcze (2026-10-26 17:02:46)
> > 2026. 10. 26. 17:56 keltezéssel, Kieran Bingham írta:
> > > There are some hardware configurations which currently generate a
> > > duplicate camera ID when the camera is on a shared but multiplexed bus.
> > > 
> > > At present this is somewhat difficult to adapt to ensure a consistent
> > > and unique id.
> > > 
> > > While this case is undesired, it presently causes libcamera to hit a
> > > Fatal asssertion on real world hardware which then causes pipewire to
> > > close.
> > > 
> > > This behaviour is even less desirable, and we can easily continue to
> > > operate libcamera without a potentially duplicate camera.
> > > 
> > > Reduce the Fatal log level to an Error allowing the code to continue
> > > without constructing this duplicate camera.
> > > 
> > > Closes: https://gitlab.freedesktop.org/camera/libcamera/-/work_items/303
> > 
> > Could this be "downgraded" to "Link" or similar? I would like to keep the
> > issue open.
> 
> Yes, that's ok for me. Will change when merging unless there are other
> concerns.
> 
> > Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
> > 
> > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> > > ---
> > >   src/libcamera/camera_manager.cpp | 2 +-
> > >   1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
> > > index 0dd4e0c590a1..9c9073053710 100644
> > > --- a/src/libcamera/camera_manager.cpp
> > > +++ b/src/libcamera/camera_manager.cpp
> > > @@ -210,7 +210,7 @@ void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
> > >   
> > >               for (const std::shared_ptr<Camera> &c : cameras_) {
> > >                       if (c->id() == camera->id()) {
> > > -                             LOG(Camera, Fatal)
> > > +                             LOG(Camera, Error)
> > >                                       << "Trying to register a camera with a duplicated ID '"
> > >                                       << camera->id() << "'";
> > >                               return;

This silent failure (as in not returning an error to the caller, it's of
course not silent in the log) means that pipeline handlers won't be
informed of the issue. I've investigated a bit to see if that could
cause a problem.

CameraManager::Private::addCamera() is called by
PipelineHandler::registerCamera():

void PipelineHandler::registerCamera(std::shared_ptr<Camera> camera)
{
	cameras_.push_back(camera);

...

	manager_->_d()->addCamera(std::move(camera));
}

cameras_ there is a std::vector<std::weak_ptr<Camera>>, and the camera
argument to the function a shared pointer with a single reference as
all pipeline handlers (currently) call it with

	registerCamera(std::move(camera))

When CameraManager::Private::addCamera() fails due to a duplicate ID,
the camera shared pointer gets destroyed, and the weak pointer is
dangling.

cameras_ is used in PipelineHandler::disconnect() (called from
PipelineHandler::mediaDeviceDisconnected()) to loop over cameras and
call CameraManager::Private::removeCamera():

	std::vector<std::weak_ptr<Camera>> cameras{ std::move(cameras_) };

	for (const std::weak_ptr<Camera> &ptr : cameras) {
		std::shared_ptr<Camera> camera = ptr.lock();
		if (!camera)
			continue;

		camera->disconnect();
		manager_->_d()->removeCamera(camera);
	}

ptr.lock() will return a null shared pointer here, so things should be
fine.

There are currently no other user of cameras_ in PipelineHandler, so
core code should be fine.

On the pipeline handler side, nothing should break immediately when the
Camera instance gets deleted in PipelineHandler::registerCamera() as the
caller doesn't hold a valid pointer to the camera (given that they all
use std::move).

Deletion of the camera will drop the shared reference to the
PipelineHandler stored in Camera::Private::pipe_. That's fine during the
match() function, but once match() returns, the
CameraManager::Private::pipelineFactoryMatch() function will delete the
PipelineHandler instance. We will therefore see the pipeline handler
beind deleted even though the match() function succeeds. I think that's
fine too, but I haven't analyzed the code to prove it.

Conclusion: I think this patch is OK, but it's possibly a bit fragile.
I'd like a unit test that triggers the issue, to catch problems in CI.

Patch
diff mbox series

diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
index 0dd4e0c590a1..9c9073053710 100644
--- a/src/libcamera/camera_manager.cpp
+++ b/src/libcamera/camera_manager.cpp
@@ -210,7 +210,7 @@  void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera)
 
 		for (const std::shared_ptr<Camera> &c : cameras_) {
 			if (c->id() == camera->id()) {
-				LOG(Camera, Fatal)
+				LOG(Camera, Error)
 					<< "Trying to register a camera with a duplicated ID '"
 					<< camera->id() << "'";
 				return;