Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(capture): generation-tag held dmabuf buffers against slot reuse (CodeRabbit #508)

Benjamin Freeman9948abb10dd7
diff --git a/electron/native/pipewire-capture/csrc/pw_shim.c b/electron/native/pipewire-capture/csrc/pw_shim.c
index 5740b532a..d0ff4ce64 100644
--- a/electron/native/pipewire-capture/csrc/pw_shim.c
+++ b/electron/native/pipewire-capture/csrc/pw_shim.c
@@ -234,8 +234,16 @@ struct osc_pw_session {
* cleared in osc_on_remove_buffer. A dmabuf frame the consumer holds for a
* GPU import (issue #507) keeps the pw_buffer pointer, but a renegotiation
* destroys the buffer set — so osc_pw_requeue_buffer must check the handle is
- * still here before touching it, or it would queue freed storage. */
+ * still here before touching it, or it would queue freed storage.
+ *
+ * Pointer equality alone is not enough: PipeWire reuses these wrapper slots,
+ * so a renegotiation can register a NEW buffer at the SAME address as one the
+ * consumer still holds. Each registration therefore carries a unique
+ * `generation`, and a retained handle is only re-queued when BOTH the pointer
+ * and its generation match — an ABA guard. `next_generation` never repeats. */
struct pw_buffer *live_buffers[OSC_MAX_LIVE_BUFFERS];
+ uint64_t live_generations[OSC_MAX_LIVE_BUFFERS];
+ uint64_t next_generation;
};
struct osc_pw_audio_api osc_audio_api;
@@ -847,18 +855,25 @@ static void osc_dmabuf_sync(int fd, int start)
}
}
-/* The live-buffer table (session->live_buffers) is only touched on the PipeWire
- * thread (add/remove_buffer) and, in osc_pw_requeue_buffer, under the thread-loop
- * lock which pauses that thread — so these need no locking of their own. */
-static void osc_track_live_buffer(struct osc_pw_session *session, struct pw_buffer *pw_buf)
+/* The live-buffer table is only touched on the PipeWire thread (add/remove_buffer)
+ * and, in osc_pw_requeue_buffer, under the thread-loop lock which pauses that
+ * thread — so these need no locking of their own. */
+
+/* Registers `pw_buf` and returns the unique generation stamped on it, which the
+ * frame carries so a later re-queue can prove it means THIS registration and not
+ * a newer buffer reusing the same slot. 0 is never a valid generation. */
+static uint64_t osc_track_live_buffer(struct osc_pw_session *session, struct pw_buffer *pw_buf)
{
size_t i;
+ uint64_t generation = ++session->next_generation;
for (i = 0; i < OSC_MAX_LIVE_BUFFERS; i++) {
if (session->live_buffers[i] == NULL) {
session->live_buffers[i] = pw_buf;
- return;
+ session->live_generations[i] = generation;
+ return generation;
}
}
+ return generation;
}
static void osc_forget_live_buffer(struct osc_pw_session *session, struct pw_buffer *pw_buf)
@@ -867,17 +882,20 @@ static void osc_forget_live_buffer(struct osc_pw_session *session, struct pw_buf
for (i = 0; i < OSC_MAX_LIVE_BUFFERS; i++) {
if (session->live_buffers[i] == pw_buf) {
session->live_buffers[i] = NULL;
+ session->live_generations[i] = 0;
return;
}
}
}
-static int osc_buffer_is_live(struct osc_pw_session *session, struct pw_buffer *pw_buf)
+/* The generation currently registered for `pw_buf`, or 0 if it is not tracked. */
+static uint64_t osc_live_buffer_generation(struct osc_pw_session *session,
+ struct pw_buffer *pw_buf)
{
size_t i;
for (i = 0; i < OSC_MAX_LIVE_BUFFERS; i++) {
if (session->live_buffers[i] == pw_buf) {
- return 1;
+ return session->live_generations[i];
}
}
return 0;
@@ -1341,8 +1359,10 @@ static int osc_inspect_buffer(struct osc_pw_session *session, struct pw_buffer *
session->dmabuf_sync_fd = -1;
if (osc_read_frame(session, buffer, &frame)) {
/* The callback needs the pw_buffer to hand back to osc_pw_requeue_buffer
- * if it takes ownership of a dmabuf frame. */
+ * if it takes ownership of a dmabuf frame, plus its generation so the
+ * re-queue can reject a stale handle after a renegotiation. */
frame.buffer_handle = pw_buf;
+ frame.buffer_generation = osc_live_buffer_generation(session, pw_buf);
if (session->callbacks.on_frame(session->callbacks.user, &frame)) {
/* Taken: leave it un-queued; the consumer will re-queue it once the
* import has copied the pixels. No SYNC bracket is open on this
@@ -1398,16 +1418,21 @@ static void osc_on_process(void *userdata)
}
/* See the header. Locks the thread loop so a foreign thread can queue safely. */
-void osc_pw_requeue_buffer(struct osc_pw_session *session, void *buffer_handle)
+void osc_pw_requeue_buffer(struct osc_pw_session *session, void *buffer_handle,
+ uint64_t buffer_generation)
{
if (session == NULL || buffer_handle == NULL || session->stream == NULL) {
return;
}
api.thread_loop_lock(session->loop);
/* Under the lock the PipeWire thread is paused, so the live-buffer table is
- * stable: only re-queue a handle a renegotiation has not destroyed. A stale
- * handle is simply dropped — PipeWire already freed that buffer. */
- if (osc_buffer_is_live(session, (struct pw_buffer *)buffer_handle)) {
+ * stable. Re-queue only when the SAME registration is still live: matching
+ * the generation as well as the pointer rejects both a destroyed buffer and a
+ * newer one PipeWire placed in the same slot after a renegotiation. A stale
+ * handle is simply dropped — PipeWire already owns or freed that buffer. */
+ if (buffer_generation != 0 &&
+ osc_live_buffer_generation(session, (struct pw_buffer *)buffer_handle) ==
+ buffer_generation) {
api.stream_queue_buffer(session->stream, (struct pw_buffer *)buffer_handle);
}
api.thread_loop_unlock(session->loop);
diff --git a/electron/native/pipewire-capture/csrc/pw_shim.h b/electron/native/pipewire-capture/csrc/pw_shim.h
index 1f013e426..23df69417 100644
--- a/electron/native/pipewire-capture/csrc/pw_shim.h
+++ b/electron/native/pipewire-capture/csrc/pw_shim.h
@@ -121,6 +121,10 @@ struct osc_pw_frame {
* Passed back to osc_pw_requeue_buffer once the import is done. Only set (and
* only meaningful) for a dmabuf frame the consumer intends to take. */
void *buffer_handle;
+ /* The registration generation of `buffer_handle`. Passed back alongside it so
+ * a re-queue can tell this buffer from a later one PipeWire put in the same
+ * slot after a renegotiation. */
+ uint64_t buffer_generation;
};
/* The negotiated video format. Reported once, from param_changed. */
@@ -249,7 +253,8 @@ struct osc_pw_session *osc_pw_start(int fd, uint32_t node_id, int want_video,
* PipeWire thread itself (that would deadlock). The consumer requeues from its
* own loop, which is a different thread. NULL session or handle is a no-op.
*/
-void osc_pw_requeue_buffer(struct osc_pw_session *session, void *buffer_handle);
+void osc_pw_requeue_buffer(struct osc_pw_session *session, void *buffer_handle,
+ uint64_t buffer_generation);
/* Stops the thread loop, joins it, and frees everything. Safe with NULL. */
void osc_pw_stop(struct osc_pw_session *session);
diff --git a/electron/native/pipewire-capture/src/shim.rs b/electron/native/pipewire-capture/src/shim.rs
index 3a4024b23..08bc20cf7 100644
--- a/electron/native/pipewire-capture/src/shim.rs
+++ b/electron/native/pipewire-capture/src/shim.rs
@@ -61,14 +61,21 @@ pub struct RawFrame {
/// `osc_pw_requeue_buffer` once the dmabuf import has copied the pixels, if
/// `on_frame` took ownership of it. Null/unused on the CPU path.
pub buffer_handle: *mut c_void,
+ /// Registration generation of `buffer_handle`, handed back with it so the
+ /// re-queue can reject a stale pointer a renegotiation reused (see the C side).
+ pub buffer_generation: u64,
}
/// A `struct pw_buffer *` we are holding out of PipeWire's queue until its dmabuf
-/// content has been imported. Send so it can travel through the mailbox; the raw
-/// pointer is only ever handed back to `osc_pw_requeue_buffer`, never dereferenced
-/// on the Rust side.
+/// content has been imported, tagged with its registration `generation` so the
+/// re-queue can tell it from a newer buffer reusing the same slot. Send so it can
+/// travel through the mailbox; the pointer is only ever handed back to
+/// `osc_pw_requeue_buffer`, never dereferenced on the Rust side.
#[derive(Debug, Clone, Copy)]
-pub struct BufferHandle(pub *mut c_void);
+pub struct BufferHandle {
+ pub ptr: *mut c_void,
+ pub generation: u64,
+}
// SAFETY: the pointer is an opaque token owned by libpipewire; Rust neither reads
// nor writes through it, only returns it to the shim's locked requeue.
unsafe impl Send for BufferHandle {}
@@ -137,7 +144,11 @@ extern "C" {
err_len: usize,
) -> *mut RawSession;
fn osc_pw_stop(session: *mut RawSession);
- fn osc_pw_requeue_buffer(session: *mut RawSession, buffer_handle: *mut c_void);
+ fn osc_pw_requeue_buffer(
+ session: *mut RawSession,
+ buffer_handle: *mut c_void,
+ buffer_generation: u64,
+ );
}
/// Where stream events go. Called on the PipeWire thread, so it must not block:
@@ -248,7 +259,7 @@ impl Drop for DmabufDesc {
// queue rather than re-queued here because re-queue must run on the main
// loop, not the PipeWire thread that supersedes a frame — see
// FrameMailbox and osc_pw_requeue_buffer.
- if !self.buffer_handle.0.is_null() {
+ if !self.buffer_handle.ptr.is_null() {
if let Ok(mut queue) = self.requeue.lock() {
queue.push(self.buffer_handle);
}
@@ -940,7 +951,7 @@ impl Session {
pub fn requeue(&self, handle: BufferHandle) {
// SAFETY: `raw` is a live session for the lifetime of `self`; the handle
// is an opaque pw_buffer token the shim validates and only re-queues.
- unsafe { osc_pw_requeue_buffer(self.raw, handle.0) };
+ unsafe { osc_pw_requeue_buffer(self.raw, handle.ptr, handle.generation) };
}
}
@@ -1027,7 +1038,10 @@ extern "C" fn on_frame(user: *mut c_void, frame: *const RawFrame) -> i32 {
drm_fourcc: frame.drm_fourcc,
modifier: frame.modifier,
planes,
- buffer_handle: BufferHandle(frame.buffer_handle),
+ buffer_handle: BufferHandle {
+ ptr: frame.buffer_handle,
+ generation: frame.buffer_generation,
+ },
requeue: mailbox.requeue_queue(),
},
frame,