Repository navigation
Fix desktop background feedback loop causing memory growth and CPU spikes - #1928
Open
peijiankang wants to merge 1 commit into
Open
peijiankang wants to merge 1 commit into
peijiankang wants to merge 1 commit into
Conversation
…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
force-pushed
the
fix-desktop-background-loop
branch
from
November 19, 2026 08:38
e6566e5 to
522e7f3
Compare
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. |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()incaja-directory-background.cis called on everybegin_loadingsignal — which fires on initial desktop load and during periodic rescans (fm-desktop-icon-view.csets a 4-second rescan timer).Each call connects a new
"change-event"handler to the globalmate_background_preferencesGSettings object viag_signal_connect(). Unlike the other handlers that useg_signal_connect_object()(auto-disconnect on object destroy), this handler targets a different object (the GSettings singleton), so it is only disconnected viaweak_notifywhen theEelBackgroundis destroyed.On a typical desktop session, the
EelBackgroundobject persists for the entire session. Between creation and destruction, everybegin_loadingemission 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()ineel-background.cchecks whether the wallpaper file exists. When it doesn't, it writes an empty string back to GSettings viag_settings_set_string().This write triggers the
"change-event"signal, which callsdesktop_background_prefs_change_event_cb(), which schedules an idle callback that callseel_bg_load_from_gsettings()again — creating a loop: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 bycaja_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 fromeel_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 usesg_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.cWhy both fixes are needed
Either fix alone is insufficient:
Impact on normal case
When the wallpaper file exists normally:
g_file_test()returnsTRUE, so the condition isfalse) — no behavior change