** Description changed:

  [ Impact ]
  
- In mutter 50.1 every Wayland cursor surface a client creates is leaked, 
together with its
- `MetaCursorWayland` sprite. Each leaked surface also stays connected to the 
cursor
- renderer's `cursor-painted` signal, so every cursor repaint on every output 
walks all of
- them on the compositor thread. The cost grows with client churn: browsers, 
Electron apps,
- Playwright/automation, anything that sets a texture cursor. The desktop 
slowly degrades to
- roughly half rate on all monitors, including a stuttering pointer, until the 
user logs out.
+ In mutter 50.1 every Wayland cursor surface a client creates is leaked,
+ together with its MetaCursorWayland sprite, because of a reference cycle:
+ MetaWaylandSurface -> MetaWaylandCursorSurface role -> MetaCursorWayland
+ -> strong reference back to the surface (introduced by 95d44e93, "wayland:
+ Keep ref on surface at MetaWaylandCursor").
  
- Measured on an affected 26.04.1 desktop (3× 4K, 2 days 8 hours of uptime): 
20,388
- handlers on `cursor-painted`, all the same callback (`on_cursor_painted`); 
~380,000
- `g_closure_invoke` calls per second; gnome-shell main thread at 42 % vs ~15 % 
when healthy;
- non-primary CRTCs at 50–66 ms per frame. Restarting the client apps freed 
none of it.
+ Each leaked surface stays connected to the cursor renderer's
+ "cursor-painted" signal, so every cursor repaint walks all of them. The
+ cost grows with client churn (browsers, Electron apps, automation, any
+ client that sets a texture cursor): GNOME Shell gets progressively slower,
+ down to roughly half frame rate with a stuttering pointer, until the user
+ logs out. On an affected desktop after 2 days 8 hours of uptime there were
+ 20,388 handlers on "cursor-painted" and the gnome-shell main thread was at
+ 42 % CPU instead of ~15 %. Confirmed on NVIDIA and AMD.
  
- Independently confirmed by a second user on an AMD iGPU laptop (Ubuntu 
26.04), so it is not
- NVIDIA-specific; logging out clears it. 50.1-0ubuntu2.5 (resolute-proposed, 
2026-10-08) does not
- include the fix.
- 
- Cause: 95d44e93 ("wayland: Keep ref on surface at MetaWaylandCursor") created 
a reference
- cycle, MetaWaylandSurface → role (MetaWaylandCursorSurface) → 
MetaCursorWayland → strong
- ref back to the surface. Upstream fixed it on gnome-50 with 918da17b 
("cursor/wayland:
- Don't hold a ref on the surface", MR !5097), first released in 50.3. 
50.1-0ubuntu2.4 does
- not include it.
+ The fix is upstream MR !5097, released in 50.3, backported in full:
+  * 918da17b "cursor/wayland: Don't hold a ref on the surface": the cursor
+    keeps only a weak pointer to its surface, which breaks the cycle.
+  * c3fdb88a "wayland-surface-state: Always hold a reference to the
+    buffer": surface state now always owns a reference to its buffer.
+    Before, a surface being freed could drop a buffer reference it did
+    not hold, crashing in meta_wayland_buffer_destroy_handler(). Cursor
+    surfaces are now freed too, so this fix is needed alongside the leak
+    fix.
+  * 7452db01 "wayland-cursor-surface: Chain up the constructed vfunc":
+    no functional change.
  
  [ Test Plan ]
  
- A headless reproducer (nothing is shown on the real desktop) is at
- https://github.com/nickallevato/mutter-cursor-leak-fix (`test/`). It runs
- `mutter --headless` on a private D-Bus session, moves a virtual pointer with 
the
- RemoteDesktop API, and starts N short-lived GTK4 clients that each set a 
texture cursor
- and exit. An LD_PRELOAD shim counts live `MetaWaylandCursorSurface` /
- `MetaCursorWayland` objects with weak refs.
+ Leak:
  
- Results with 30 clients:
- - 50.1-0ubuntu2.2: 30 of 30 cursor surfaces still alive after all clients 
exited
- - 50.1-0ubuntu2.4: 30 of 30 still alive
- - 50.1-0ubuntu2.4 + 918da17b: 0 of 30 (never more than 1 alive at a time)
+ 1. Use the headless reproducer at
+    https://github.com/nickallevato/mutter-cursor-leak-fix (test/). It runs
+    mutter --headless on a private D-Bus session, moves a virtual pointer
+    through the RemoteDesktop API and starts 30 short-lived GTK4 clients
+    that each set a texture cursor and exit. An LD_PRELOAD shim counts live
+    MetaWaylandCursorSurface and MetaCursorWayland objects.
+    - 50.1-0ubuntu2.6: 30 of 30 cursor surfaces still alive at the end.
+    - 50.1-0ubuntu2.7: 0 of 30, and never more than 1 alive at a time; no
+      crash and no new warnings or criticals in the output.
+ 2. On a real session, use texture-cursor apps (e.g. a browser) heavily
+    for a day. gnome-shell CPU stays at baseline and the pointer stays
+    smooth.
  
- Manual check on a real session: use texture-cursor apps (e.g. a browser) 
heavily for a
- day. With the fix, gnome-shell CPU stays at baseline and the cursor stays 
smooth; without
- it, both degrade.
+ Regression checks (buffer handling is used by every Wayland surface), in
+ a GNOME Wayland session with the proposed package:
+ 
+ 3. Use GTK4, GTK3, Firefox, Chromium and an Electron app normally; open
+    and close many windows, menus and tooltips; play a video in a window
+    and fullscreen; run a fullscreen game or video that uses direct
+    scanout.
+ 4. Kill rendering clients abruptly (kill -9 during video playback, and
+    while the pointer shows the client's cursor).
+ 5. Expected: no gnome-shell crash, no frozen or stalled client windows,
+    gnome-shell memory stays stable, and the journal shows no new
+    gnome-shell warnings, in particular no "meta_wayland_buffer_finalize:
+    runtime check failed".
  
  [ Where problems could occur ]
  
- The patch turns the sprite's surface reference into a weak pointer. Code that 
used the
- surface through the sprite after the surface was destroyed now sees NULL. 
Upstream guards
- the two places that do this (`prepare_at` and `get_buffer`). A missed caller 
would show up
- as a gnome-shell crash on cursor changes while a client is exiting. The 
reproducer covers
- exactly that case, a client exiting while its cursor is displayed, 30 times 
per run, with
- no crash or critical. The change is 4 hunks in one file, is upstream in 
50.3–50.5, and
- applies cleanly on top of 50.1-0ubuntu2.4.
+  * The cursor now refers to its surface through a weak pointer. Upstream
+    guards both uses (prepare_at and get_buffer). A missed use would show
+    up as a gnome-shell crash when a cursor updates while its client
+    exits.
+  * Surface state now always owns a reference to its buffer. This applies
+    to every Wayland surface, not just cursors. If a reference is kept
+    too long, buffers are released late: memory grows, or clients stall
+    waiting for wl_buffer.release. If one is dropped too early, gnome-shell
+    crashes with a use-after-free in the buffer or surface code. The new
+    check in meta_wayland_buffer_finalize() logs a warning when a buffer
+    is freed while its client resource still exists.
+ 
+ All three commits have been in gnome-50 since 50.3 with no upstream
+ follow-up fixes, and apply unchanged to 50.1.
  
  [ Other Info ]
  
- Investigated, backported and tested with the help of an AI coding agent 
(Claude Code);
- findings and numbers were checked on the affected machine. Workaround until 
fixed: log out
- and back in.
+ Upstream MR: https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/5097
+ Patches:
+   
debian/patches/ubuntu/wayland-cursor-surface-Chain-up-the-constructed-vfunc.patch
+   debian/patches/ubuntu/cursor-wayland-Don-t-hold-ref-on-surface.patch
+   
debian/patches/ubuntu/wayland-surface-state-Always-hold-a-reference-to-the-buffer.patch
+ Workaround until fixed: log out and back in.

-- 
You received this bug notification because you are a member of Ubuntu
Desktop Bugs, which is subscribed to mutter in Ubuntu.
https://bugs.launchpad.net/bugs/2171024

Title:
   Leaked Wayland cursor surfaces make GNOME Shell progressively slower
  over uptime (fixed upstream in 50.3 by 918da17b)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/mutter/+bug/2171024/+subscriptions


-- 
desktop-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/desktop-bugs

Reply via email to