[v1] gstreamer: Disconnect request completion signal
diff mbox series

Message ID 20261120200637.802757-1-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • [v1] gstreamer: Disconnect request completion signal
Related show

Commit Message

Barnabás Pőcze Nov. 20, 2026, 8:06 p.m. UTC
The request completion signal must be disconnected when the camera is
disassociated from the gstreamer state. Otherwise, if the same `Camera` is
opened and used again, the signal handler might be called on the stale
(potentially already freed) state, which will either be a use-after-free,
or a potential assertion failure when the front of the (stale)
`queuedRequests_` is accessed.

This is because the signals of a `Camera` are shared between all users,
and they are not automatically disconnected when `release()`d, etc.
So great care must be taken not to leave "dangling" receivers on signals.

So disconnect the signal in `gst_libcamera_src_close()`, the opposite
operation of `gst_libcamera_src_open()`, where the signal is connected.

Note that this issue depends on the same `Camera` object being used, so
it requires the `CameraManager` to stay alive between the close and reopen
steps.

Closes: https://gitlab.freedesktop.org/camera/libcamera/-/work_items/377
Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 src/gstreamer/gstlibcamerasrc.cpp | 2 ++
 1 file changed, 2 insertions(+)

Comments

Laurent Pinchart Nov. 20, 2026, 9:55 p.m. UTC | #1
On Fri, Nov 20, 2026 at 09:06:37PM +0100, Barnabás Pőcze wrote:
> The request completion signal must be disconnected when the camera is
> disassociated from the gstreamer state. Otherwise, if the same `Camera` is
> opened and used again, the signal handler might be called on the stale
> (potentially already freed) state, which will either be a use-after-free,
> or a potential assertion failure when the front of the (stale)
> `queuedRequests_` is accessed.
> 
> This is because the signals of a `Camera` are shared between all users,
> and they are not automatically disconnected when `release()`d, etc.
> So great care must be taken not to leave "dangling" receivers on signals.
> 
> So disconnect the signal in `gst_libcamera_src_close()`, the opposite
> operation of `gst_libcamera_src_open()`, where the signal is connected.
> 
> Note that this issue depends on the same `Camera` object being used, so
> it requires the `CameraManager` to stay alive between the close and reopen
> steps.

I assume you mean that it's triggering the issue that requires the
camera manager to stay alive, not the fix.

> Closes: https://gitlab.freedesktop.org/camera/libcamera/-/work_items/377
> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
> ---
>  src/gstreamer/gstlibcamerasrc.cpp | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp
> index 9541aa3f69..cf61939f1e 100644
> --- a/src/gstreamer/gstlibcamerasrc.cpp
> +++ b/src/gstreamer/gstlibcamerasrc.cpp
> @@ -981,6 +981,8 @@ gst_libcamera_src_close(GstLibcameraSrc *self)
>  				    ("libcamera::Camera.release() failed: %s", g_strerror(-ret)));
>  	}
>  
> +	state->cam_->requestCompleted.disconnect(state);
> +

It would be nice to handle that automatically. Maybe Camera::acquire()
should return an object that exposes the runtime part of the Camera
class API. Then the signal would be disconnected automatically when that
object is destroyed.

In the meantime,

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

>  	state->cam_.reset();
>  	state->cm_.reset();
>  }

Patch
diff mbox series

diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp
index 9541aa3f69..cf61939f1e 100644
--- a/src/gstreamer/gstlibcamerasrc.cpp
+++ b/src/gstreamer/gstlibcamerasrc.cpp
@@ -981,6 +981,8 @@  gst_libcamera_src_close(GstLibcameraSrc *self)
 				    ("libcamera::Camera.release() failed: %s", g_strerror(-ret)));
 	}
 
+	state->cam_->requestCompleted.disconnect(state);
+
 	state->cam_.reset();
 	state->cm_.reset();
 }