wl_shm: keep a buffer valid after its pool is destroyed
CI / test (push) Failing after 18s

Qt Quick apps (kwi) render through Mesa's software EGL path, which creates a pool,
creates one buffer and destroys the pool at once. We closed the fd and dropped the
pool, so every snapshot returned None and the window stayed blank. The pool now lives
until its last buffer is destroyed (re-keyed so recycled client ids can't collide).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 00:24:33 -08:00
1 parent df1f37ee78
commit 82cac8a915
2 files changed
+75 -4

No files matched your search

+20
View File
@@ -223,6 +223,7 @@ fn commit_shm_frame(
surf_id : UInt,
xdg_surf_id : UInt,
win_id : UInt64,
destroy_pool? : Bool = false,
) -> Unit raise {
match c.configure_serial {
Some(ser) => {
@@ -277,6 +278,11 @@ fn commit_shm_frame(
w_buf.i32(stride)
w_buf.u32(0U)
c.send(w_buf.finish_msg(pool_id, 0U))
if destroy_pool {
// Mesa's software EGL path (Qt Quick apps like kwi) destroys the pool right away;
// the buffer must stay valid.
c.send(@wayland.WireWriter::new().finish_msg(pool_id, 1U))
}
// wl_surface.attach(buf, 0, 0) + damage_buffer + commit
let w_att = @wayland.WireWriter::new()
@@ -356,3 +362,17 @@ test "e2e pure-moonbit wayland compositor with lib/wm, ssd decorations, and shm
@sys.close_fd(c2.fd)
comp.close()
}
///|
test "a buffer stays valid after its wl_shm_pool is destroyed" {
let socket_path = "/tmp/wayland-moon-pooldestroy-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(
comp, socket_path, "Kwi", "org.kde.kwi",
)
commit_shm_frame(comp, c, shm, surf, xdg, 1UL, destroy_pool=true)
assert_true(comp.windows.get(1UL).unwrap().surface is Some(_))
}
+55 -4
View File
@@ -50,11 +50,14 @@ enum ObjKind {
struct ShmPoolState {
fd : Int
mut size : Int
/// The client destroyed the wl_shm_pool but buffers made from it are still alive
/// (the spec keeps their mapping valid); the fd is closed with the last buffer.
mut destroyed : Bool
}
///|
struct ShmBufferState {
pool_id : UInt
mut pool_id : UInt
offset : Int
width : Int
height : Int
@@ -169,6 +172,28 @@ fn ClientConn::take_fd(self : ClientConn) -> Int {
self.pending_fds.remove(0)
}
///| Whether any live buffer was created from this pool.
fn ClientConn::pool_in_use(self : ClientConn, pool_id : UInt) -> Bool {
for _, b in self.shm_buffers {
if b.pool_id == pool_id {
return true
}
}
false
}
///| Close a destroyed pool once its last buffer is gone.
fn ClientConn::release_dead_pool(self : ClientConn, pool_id : UInt) -> Unit {
match self.shm_pools.get(pool_id) {
Some(p) =>
if p.destroyed && !self.pool_in_use(pool_id) {
@sys.close_fd(p.fd)
self.shm_pools.remove(pool_id)
}
None => ()
}
}
///|
fn ClientConn::close_all(self : ClientConn) -> Unit {
for _, pool in self.shm_pools {
@@ -562,7 +587,7 @@ fn WaylandServer::dispatch_msg(
let fd = c.take_fd()
c.objects.set(pool_id, ObjShmPool(pool_id))
if fd >= 0 {
c.shm_pools.set(pool_id, { fd, size })
c.shm_pools.set(pool_id, { fd, size, destroyed: false })
}
}
ObjShmPool(pool_id) =>
@@ -586,10 +611,28 @@ fn WaylandServer::dispatch_msg(
} else if opcode == 1U {
// destroy
match c.shm_pools.get(pool_id) {
Some(p) => @sys.close_fd(p.fd)
Some(p) =>
if c.pool_in_use(pool_id) {
// Client ids are reused after delete_id, so move the pool (and its buffers)
// to a private key a new pool can't collide with.
p.destroyed = true
let mut key = 0x80000000U
while c.shm_pools.contains(key) {
key = key + 1U
}
c.shm_pools.remove(pool_id)
c.shm_pools.set(key, p)
for _, b in c.shm_buffers {
if b.pool_id == pool_id {
b.pool_id = key
}
}
} else {
@sys.close_fd(p.fd)
c.shm_pools.remove(pool_id)
}
None => ()
}
c.shm_pools.remove(pool_id)
c.objects.remove(pool_id)
send_delete_id(c, pool_id)
} else if opcode == 2U {
@@ -602,9 +645,17 @@ fn WaylandServer::dispatch_msg(
}
ObjBuffer(buf_id) =>
if opcode == 0U {
let pool_id = match c.shm_buffers.get(buf_id) {
Some(b) => Some(b.pool_id)
None => None
}
c.shm_buffers.remove(buf_id)
c.objects.remove(buf_id)
send_delete_id(c, buf_id)
match pool_id {
Some(pid) => c.release_dead_pool(pid)
None => ()
}
}
ObjXdgWmBase =>
if opcode == 0U {