Skip to content

Fix crash when logical monitor is null#12

Merged
flexagoon merged 3 commits intoflexagoon:mainfrom
maresb:fix/handle-null-logical-monitor
Feb 20, 2026
Merged

Fix crash when logical monitor is null#12
flexagoon merged 3 commits intoflexagoon:mainfrom
maresb:fix/handle-null-logical-monitor

Conversation

@maresb
Copy link
Copy Markdown
Contributor

@maresb maresb commented Jan 20, 2026

Note

AI Assistance Disclosure: This PR was created with assistance from Claude Opus 4.5 via Cursor. The crash analysis, diagnosis, and fix were AI-assisted based on actual crash logs from my system.

Summary

Fixes #11

The get_work_area_current_monitor(), get_work_area_all_monitors(), and get_work_area_for_monitor() methods can trigger a fatal assertion in Mutter's meta_window_get_work_area_for_logical_monitor() if the window's logical monitor is null or invalid.

This can occur during:

  • Monitor hotplug (connecting/disconnecting displays)
  • Display configuration changes
  • Windows in transient states between monitors

The assertion failure causes GNOME Shell to abort, crashing the entire desktop session.

Changes

This PR wraps the work area calls in a helper function safeGetWorkArea() that catches any errors and returns null instead, preventing the crash.

Testing

I experienced this crash on my system (GNOME Shell 49.2, NixOS). Unfortunately I cannot easily test the fix without waiting for another crash to occur naturally, but the change is minimal and defensive—it simply wraps the existing calls in try/catch blocks.

Crash Log (for reference)

libmutter:ERROR:../src/core/window.c:6081:meta_window_get_work_area_for_logical_monitor: assertion failed: (logical_monitor)
#0   560760948308 i   file:///...extensions/focused-window-dbus@flexagoon.com/extension.js:51

The get_work_area_current_monitor(), get_work_area_all_monitors(), and
get_work_area_for_monitor() methods can trigger a fatal assertion in
Mutter's meta_window_get_work_area_for_logical_monitor() if the window's
logical monitor is null or invalid.

This can occur during:
- Monitor hotplug (connecting/disconnecting displays)
- Display configuration changes
- Windows in transient states between monitors

The assertion failure causes GNOME Shell to abort, crashing the entire
desktop session.

This commit wraps the work area calls in a helper function that catches
any errors and returns null instead, preventing the crash.
@maresb
Copy link
Copy Markdown
Contributor Author

maresb commented Jan 20, 2026

I successfully tested this fix by unplugging my monitor while polling the focused window, causing a crash. Then I restarted my GNOME session with my fork of the extension activated, repeated the experiment of unplugging my monitor, and the crash did not occur.

The previous try/catch approach doesn't work because Mutter's assertion
failure calls abort() at the C level, which terminates the process
immediately and cannot be caught by JavaScript exception handling.

Instead, we now check if the window has a valid monitor (index >= 0 and
within bounds) BEFORE calling the get_work_area_* methods. If the
monitor is invalid, we return null for those fields instead of crashing.
@maresb
Copy link
Copy Markdown
Contributor Author

maresb commented Feb 4, 2026

Note

AI Assistance Disclosure: This comment was created with assistance from Claude Opus 4.5 via Cursor.

Update: try/catch approach doesn't work

After deploying the try/catch fix, GNOME Shell still crashed with the same assertion failure. The crash occurred at line 19 of the patched extension (inside the safeGetWorkArea function).

Root cause: Mutter's assertion failure calls abort() at the C level, which terminates the process immediately. JavaScript try/catch cannot intercept this.

I've pushed an updated fix that checks if the window has a valid monitor before calling the work area methods, rather than trying to catch the error afterward:

function hasValidMonitor(metaWindow) {
  const windowMonitor = metaWindow.get_monitor();
  if (windowMonitor < 0) {
    return false;
  }
  const numMonitors = global.display.get_n_monitors();
  if (windowMonitor >= numMonitors) {
    return false;
  }
  return true;
}

If the monitor is invalid, the area, area_all, and area_cust fields are set to null instead of calling the methods that would trigger the crash.

@flexagoon flexagoon merged commit b10b531 into flexagoon:main Feb 20, 2026
@flexagoon
Copy link
Copy Markdown
Owner

Thanks!

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.

GNOME Shell crashes with assertion failure in meta_window_get_work_area_for_logical_monitor

2 participants