Closed Bug 2084932 Opened 27 days ago Closed 20 days ago

Memory leak with animated themes and occluded windows if window is shortly resized on window creation

Categories

(Core :: Graphics, defect)

Firefox 148
defect

Tracking

()

RESOLVED FIXED
161 Branch
Tracking Status
firefox161 --- fixed

People

(Reporter: javalsai, Assigned: stransky)

References

Details

Attachments

(6 files, 1 external file, 1 obsolete file)

Attached file ffleak.tar.zst (obsolete) —

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0

Steps to reproduce

Open a fresh firefox profile and install an animated theme such as https://addons.mozilla.org/en-US/firefox/addon/stars-animated-dark/. Open several windows (e.g. 8) on another workspace and come back to the first window so that new workspace is unfocused. Then, on the first window, open and close a new window (may not happen in the first try) to trigger the memory leak.

I've only managed to reproduce this consistently with a custom bar that causes a small resize on firefox when changing the workspace window count from/to 1, that resize very strongly seems to condition the leak. All the tests were done with hyprland and quickshell as the bar that causes such resize. I will attach minimal configurations for both hyprland and quickshell (hyprland -c path/to/config.lua and qs -p path/to/config.qml) used in my testing.

Actual results

Firefox starts leaking SHR and RES memory at a rate of about 20-30 MiB/s, it rarely manifests as a single 20-30 MiB leak each time a window is opened&closed.

/proc/<pid>/smaps shows thousands of leaks of 0.9MiB mapping of /memfd:mozilla-ipc and the number of memory mappings itself only increases. The leak pauses when no firefox tabs are in an active workspace (rendering likely paused) and collects leaked memory when the occluded windows are rendered.

Ran a mozregression and this seems to manifest as far back as 2024.

I've reproduced this this on fresh arch VMs. And on builds from arch, artix and nix of firefox and artix librewolf.

I will also attach a memory report from about:memory of the leak at 2-3 GiB (from ~250MiB before leak) as the guidelines recommend of an artix librewolf build.

Expected results

No leak.

See Also: → 1828587
See Also: → 1830753
Attached file memory-report.json.gz —

Anonymized memory report.

Attachment #9666618 - Attachment is obsolete: true
Attached file hyprleak.lua —

Hyprland configuration used for testing reproducibility of the bug. (use with hyrpland -c path/to/config.lua)

Attached file qsleak.qml —

Quickshell configuration used for testing reproducibility of the bug (use with qs -p path/to/config.qml)

Component: Untriaged → Graphics
Product: Firefox → Core

Thanks for the detailed report and the configs.

This is the same leak as bug 1830753: animation frames of the theme image keep getting sent to a window whose compositor isn't producing frames, so they are never released. The fix there was to stop sending updates to windows whose compositor is paused or whose refresh driver is throttled. On Wayland we only find out a window is hidden indirectly, from frame callbacks stopping, so my guess is the resize leaves a hidden window in a state where we think it is visible but it isn't compositing.

Could you reproduce with this logging and attach the log? Start it, do the steps until the leak begins, let it leak for a few seconds, then quit.

MOZ_LOG=timestamp,sync,WidgetVSync:5,Widget:5,LayerTree:5 MOZ_LOG_FILE=/tmp/ffleak firefox

Each process writes its own file. The parent process log is /tmp/ffleak.moz_log and is the important one; the others are /tmp/ffleak.child-N.moz_log. Please attach all of them compressed together if that's easy, for example:

tar --zstd -cf ffleak-logs.tar.zst /tmp/ffleak*.moz_log

Otherwise just /tmp/ffleak.moz_log is fine.

It would also help to know whether it still leaks with MOZ_ENABLE_WAYLAND=0 (XWayland).

Flags: needinfo?(jvssxxi)
Attached file ffleak.logs.tar.zst —

MOZ_LOG=timestamp,sync,WidgetVSync:5,Widget:5,LayerTree:5 MOZ_LOG_FILE=/tmp/ffleak on artix librewolf (I can try plain firefox if desired). In this instance I experienced a permanent ~30MiB leak every time I opened a new window instead of a constant leak though.

Flags: needinfo?(jvssxxi)

MOZ_LOG=timestamp,sync,WidgetVSync:5,Widget:5,LayerTree:5 MOZ_LOG_FILE=/tmp/ffleak but the constant leak variant of the bug.

It would also help to know whether it still leaks with MOZ_ENABLE_WAYLAND=0 (XWayland).

I've tried about 3 times and I don't seem able to reproduce it with wayland disabled. It usually manifest at least a high-er memory usage every time.

Here is what claude figured out:

Thanks, the log shows what's happening, and the XWayland result fits with it. (The two log attachments are the same file, so we only have one of the two variants, but I don't think we need the other one for now.)

Here is one of the windows on the other workspace (7f6d200d8a00) from the parent log:

13:01:15.666080 WaylandVsyncSource::HiddenWindowCallback() we're hidden, time since last VSync 1205 ms
13:01:15.666124 nsWindow::NotifyOcclusionState() mIsFullyOccluded 1
13:01:17.503659 nsWindow::OnContainerSizeAllocate [0,0] -> [775 x 835] scaled [1.20] [930 x 1002]
13:01:17.510681 nsWindow::OnExposeEvent GdkWindow [7f6d21b5a2c0] XID [0x0]
13:01:17.510692 nsWindow::NotifyOcclusionState() mIsFullyOccluded 0
After that there is no vsync activity at all for this window until the log ends 20 seconds later: no frame callbacks and no HiddenWindowCallback. Four other windows on that workspace do exactly the same thing at 13:01:17.5, which is just after a window was closed at 13:01:17.45, when the bar resizes everything.

What I think is going on:

  1. On Wayland we have no direct occlusion notification. WaylandVsyncSource::HiddenWindowCallback marks a window occluded after it has gone about a second without a frame callback. That works here at 13:01:15.
  2. When the occluded window is resized, GTK sends it an expose event, and nsWindow::OnExposeEvent calls NotifyOcclusionState(OcclusionState::VISIBLE). Through AppWindow::OcclusionStateChanged and CanonicalBrowsingContext::RecomputeAppWindowVisibility that makes the window active again and resumes its compositor. (The log has no compositor-side lines, so this step is from reading the code, not the log.)
  3. The window is still on a hidden workspace, so it never gets another frame callback. The hidden-window timer is only started from the frame callback handler (WaylandVsyncSource::SetHiddenWindowVSync), and after it fires it stops, since widget.wayland.vsync.keep-firing-at-idle is false by default. So nothing ever marks the window occluded again.
  4. The window now looks visible: its compositor isn't paused and its refresh driver isn't throttled. That means SharedSurfacesAnimation::SetCurrentFrame keeps sending it the theme's animation frames, because the checks added in bug 1830753 only skip paused or throttled windows. But its compositor only gets vsync from frame callbacks, so it never composites and never releases those frames. That is the leak, and it gets freed once the window is shown and renders again.

On X11 the expose event follows real visibility, and we get proper visibility events there, which is presumably why XWayland doesn't reproduce.

Possible fixes, in widget/gtk: don't treat an expose event as a visibility signal on Wayland, or start the hidden-window timer on every transition to visible, so a window that is not actually visible gets marked occluded again after a second. The second option also covers anything else that might mark a hidden window visible.

Flags: needinfo?(stransky)

(In reply to Timothy Nikkel (:tnikkel) from comment #8)

  1. On Wayland we have no direct occlusion notification. WaylandVsyncSource::HiddenWindowCallback marks a window occluded after it has gone about a second without a frame callback. That works here at 13:01:15.
  2. When the occluded window is resized, GTK sends it an expose event, and nsWindow::OnExposeEvent calls NotifyOcclusionState(OcclusionState::VISIBLE). Through AppWindow::OcclusionStateChanged and CanonicalBrowsingContext::RecomputeAppWindowVisibility that makes the window active again and resumes its compositor. (The log has no compositor-side lines, so this step is from reading the code, not the log.)

Yes, that's correct. When we get 'expose' event it means the window is visible from OS perspective.

  1. The window is still on a hidden workspace, so it never gets another frame callback. The hidden-window timer is only started from the frame callback handler (WaylandVsyncSource::SetHiddenWindowVSync), and after it fires it stops, since widget.wayland.vsync.keep-firing-at-idle is false by default. So nothing ever marks the window occluded again.

Yes, that's possible - let's check that.

  1. The window now looks visible: its compositor isn't paused and its refresh driver isn't throttled. That means SharedSurfacesAnimation::SetCurrentFrame keeps sending it the theme's animation frames, because the checks added in bug 1830753 only skip paused or throttled windows. But its compositor only gets vsync from frame callbacks, so it never composites and never releases those frames. That is the leak, and it gets freed once the window is shown and renders again.
Attached file about-support.json —

about:setup raw data

(In reply to [:javalsai] from comment #10)

Created attachment 9667041 [details]
about-support.json

about:setup raw data

Hmm, are you sure it's not librewolf problem itself?

(In reply to Elle Solomina (she/her) from comment #11)

Hmm, are you sure it's not librewolf problem itself?

100%, as indicated, I've reproduced this in all kinds of firefox builds, even on other machines and a fresh arch VM (with normal firefox). I can provide the about:support of another machine if desired, it's just that the laptop where I usually reply from has librewolf, so I am attaching that data to track the same instance of the problem.

I probably can even get around to sharing an image of a suspended VM after triggering the leak if anyone wants to see the problem in action.

The bug report makes sense to me, will look at it after recent phabricator outage.

Severity: -- → S3
Flags: needinfo?(stransky)

When nsWindow receives expose event we make it visible but VSync source may stay disabled.

Assignee: nobody → stransky
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Pushed by stransky@redhat.com: https://github.com/mozilla-firefox/firefox/commit/5ff58768d972 https://hg.mozilla.org/integration/autoland/rev/4495e6065ff5 [Wayland] Explicitly watch for hidden VSync source when nsWindow gets expose event r=emilio,gtk-reviewers
Status: ASSIGNED → RESOLVED
Closed: 20 days ago
Resolution: --- → FIXED
Target Milestone: --- → 161 Branch
Duplicate of this bug: 1880467
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: