Skip to content

Fix desktop background feedback loop causing memory growth and CPU spikes - #1928

Open
peijiankang wants to merge 1 commit into
mate-desktop:masterfrom
peijiankang:fix-desktop-background-loop
Open

peijiankang wants to merge 1 commit into
mate-desktop:masterfrom
peijiankang:fix-desktop-background-loop

Conversation

@peijiankang

Copy link
Copy Markdown

Problem

When the configured wallpaper file does not exist on disk (deleted, moved, or invalid path), caja's desktop background enters a feedback loop that causes continuous memory growth and high CPU usage.

Two issues combine to create this loop:

Root Cause

Issue 1: Duplicate signal handler accumulation

caja_connect_desktop_background_to_settings() in caja-directory-background.c is called on every begin_loading signal — which fires on initial desktop load and during periodic rescans (fm-desktop-icon-view.c sets a 4-second rescan timer).

Each call connects a new "change-event" handler to the global mate_background_preferences GSettings object via g_signal_connect(). Unlike the other handlers that use g_signal_connect_object() (auto-disconnect on object destroy), this handler targets a different object (the GSettings singleton), so it is only disconnected via weak_notify when the EelBackground is destroyed.

On a typical desktop session, the EelBackground object persists for the entire session. Between creation and destruction, every begin_loading emission adds one more handler without removing the previous ones. The handler count grows linearly with the number of rescans.

Issue 2: Write-back to GSettings triggers change-event

eel_bg_load_from_gsettings() in eel-background.c checks whether the wallpaper file exists. When it doesn't, it writes an empty string back to GSettings via g_settings_set_string().

This write triggers the "change-event" signal, which calls desktop_background_prefs_change_event_cb(), which schedules an idle callback that calls eel_bg_load_from_gsettings() again — creating a loop:

eel_bg_load_from_gsettings()
  → g_settings_set_string("")        // write empty string
    → "change-event" signal
      → desktop_background_prefs_change_event_cb()   × N handlers
        → g_idle_add(idle_cb)
          → eel_bg_load_from_gsettings()   // re-enter

With N accumulated handlers, each loop iteration is multiplied by N, causing memory growth and CPU spikes proportional to the number of rescans since session start.

Fix

Commit 1: Prevent duplicate signal handler accumulation

Add a g_object_get_data() guard using the "desktop_bg_connected" key, following the same pattern already used by caja_connect_background_to_file_metadata() in the same file. If the handler is already connected, return early.

Commit 2: Stop write-back to GSettings when wallpaper file is missing

Remove the g_settings_set_string() write-back from eel_bg_load_from_gsettings(). This eliminates the signal that triggers the feedback loop.

mate_bg_load_from_gsettings() (in mate-desktop) already handles missing wallpaper files correctly: it uses g_settings_delay() / g_settings_reset() / g_settings_revert() to read the schema default without writing to dconf, and falls back to a solid color background. Source: https://github.com/mate-desktop/mate-desktop/blob/master/libmate-desktop/mate-bg.c

Why both fixes are needed

Either fix alone is insufficient:

  • Fix 1 only: Still has 1 handler, so the loop runs once per change-event (slower but still loops)
  • Fix 2 only: Still accumulates N handlers, so any legitimate wallpaper change triggers N idle callbacks (CPU spike)

Impact on normal case

When the wallpaper file exists normally:

  • Fix 1: The skipped operations are all redundant (duplicate signal connections, duplicate weak refs, duplicate loads of the same wallpaper) — these duplicates are themselves the bug
  • Fix 2: The write-back branch is never executed (g_file_test() returns TRUE, so the condition is false) — no behavior change

…ikes

When the configured wallpaper file does not exist on disk (deleted,
moved, or invalid path), caja's desktop background enters a feedback
loop that causes continuous memory growth and high CPU usage.

Two issues combine to create this loop:

1. Duplicate signal handler accumulation (caja-directory-background.c)

   caja_connect_desktop_background_to_settings() is called on every
   begin_loading signal, which fires on initial desktop load and during
   periodic rescans (fm-desktop-icon-view.c sets a 4-second rescan
   timer). Each call connects a new "change-event" handler to the
   global mate_background_preferences GSettings object via
   g_signal_connect(). Unlike the other handlers that use
   g_signal_connect_object() (auto-disconnect on object destroy), this
   handler targets a different object (the GSettings singleton), so it
   is only disconnected via weak_notify when the EelBackground is
   destroyed. On a typical desktop session, the EelBackground object
   persists for the entire session, so the handler count grows linearly
   with the number of rescans.

2. Write-back to GSettings triggers change-event (eel-background.c)

   eel_bg_load_from_gsettings() checks whether the wallpaper file
   exists. When it doesn't, it writes an empty string back to GSettings
   via g_settings_set_string(). This write triggers the "change-event"
   signal, which calls desktop_background_prefs_change_event_cb(),
   which schedules an idle callback that calls
   eel_bg_load_from_gsettings() again, creating a loop:

     eel_bg_load_from_gsettings()
       -> g_settings_set_string("")        // write empty string
         -> "change-event" signal
           -> desktop_background_prefs_change_event_cb()   x N handlers
             -> g_idle_add(idle_cb)
               -> eel_bg_load_from_gsettings()   // re-enter

   With N accumulated handlers, each loop iteration is multiplied by N.

Fix:

1. Add a g_object_get_data() guard using "desktop_bg_connected" key in
   caja_connect_desktop_background_to_settings(), following the same
   pattern already used by caja_connect_background_to_file_metadata()
   in the same file. If the handler is already connected, return early.

2. Remove the g_settings_set_string() write-back from
   eel_bg_load_from_gsettings(). mate_bg_load_from_gsettings() (in
   mate-desktop) already handles missing wallpaper files correctly: it
   uses g_settings_delay()/g_settings_reset()/g_settings_revert() to
   read the schema default without writing to dconf, and falls back to
   a solid color background.

Either fix alone is insufficient:
- Fix 1 only: still has 1 handler, so the loop runs once per change-event
- Fix 2 only: still accumulates N handlers, causing CPU spikes on any
  legitimate wallpaper change

When the wallpaper file exists normally:
- Fix 1: the skipped operations are all redundant duplicates (the bug)
- Fix 2: the write-back branch is never executed (g_file_test returns
  TRUE), so there is no behavior change
@peijiankang
peijiankang force-pushed the fix-desktop-background-loop branch from e6566e5 to 522e7f3 Compare November 19, 2026 08:38
@peijiankang

Copy link
Copy Markdown
Author

Friendly ping for review. This PR fixes a desktop background feedback loop that causes memory growth and CPU spikes when the configured wallpaper file is missing. The fix has been verified for both the abnormal case (wallpaper missing) and the normal case (wallpaper present, no behavior change). Happy to address any feedback.

@peijiankang

Copy link
Copy Markdown
Author

@vkareh Friendly ping for review. This PR fixes a desktop background feedback loop that causes memory growth and CPU spikes when the configured wallpaper file is missing. Both root causes are addressed in a single commit. Verified for both abnormal (wallpaper missing) and normal (wallpaper present) cases.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant