omarchy icon indicating copy to clipboard operation
omarchy copied to clipboard

Quickshell crash (SIGSEGV) in tray submenu: reentrant Repeater model swap in Tray.qml

Open vayners opened this issue 2 months ago • 5 comments

What happened

The Quickshell-based bar's tray popup menu (plugins/bar/widgets/Tray.qml) segfaults when clicking a tray menu row that has a submenu (hasChildren). Crash report saved under ~/.cache/quickshell/crashes/<run-id>/.

Environment

  • Omarchy: 4.0.4 (BUILD_ID from /etc/os-release)
  • Quickshell: 0.3.1 (Arch package)
  • Qt: 6.11.2
  • GPU: Intel i915 (integrated), Wayland/Hyprland session
  • Signal: SIGSEGV (11)

Root cause

Tray.qml's submenu popup renders rows through:

Repeater {
  model: root.currentChildren
  delegate: Item {
    id: menuRow
    ...
    MouseArea {
      id: rowMouse
      onClicked: {
        if (root.menuLevelSettling) return
        if (menuRow.modelData.hasChildren) {
          trayMenuFlick.contentY = 0
          root.enterSubmenu(menuRow.modelData, menuRow.rowText)   // <-- synchronous
        } else {
          menuRow.modelData.triggered()
          root.close()
        }
      }
    }
  }
}

root.currentChildren is computed from root.submenuStack:

readonly property var currentChildren: submenuDepth > 0
  ? submenuStack[submenuDepth - 1].opener.children
  : trayMenuOpener.children

and enterSubmenu() reassigns that stack synchronously, from inside the very click handler that belongs to a delegate of the Repeater this stack feeds:

function enterSubmenu(entry, title) {
  var opener = submenuOpenerComponent.createObject(root, { menu: entry })
  if (!opener) return
  var stack = submenuStack.slice()
  stack.push({ opener: opener, title: title })
  submenuStack = stack        // <-- changes root.currentChildren immediately
  settleMenuLevel()
}

Setting submenuStack immediately changes root.currentChildren, which is the Repeater's model. That drives QQuickRepeater::regenerate(), which destroys and recreates delegates — including the delegate (menuRow/rowMouse) whose onClicked handler is still executing on the call stack. The existing comment on the call site (a few lines further down, guarding a different symptom) already acknowledges this:

// Reset scroll BEFORE swapping the model: the swap destroys this delegate synchronously and ids stop resolving after.

...but only the Flickable.contentY reset was hardened against it — not the reentrant destroy itself. The result is a use-after-free while Quickshell incubates the freshly-regenerated delegate mid-teardown of the old one.

leaveSubmenu() (called from the submenu header's back-button MouseArea, outside the affected Repeater) has the same synchronous-reassignment pattern, lower risk since the clicking MouseArea isn't itself inside the regenerated Repeater, but same bug class.

Stacktrace (top frames, full report attached)

Signal: Segmentation fault (11)
#0   ... libc.so.6
#1   ... libQt6QmlModels.so.6
#2   ... libQt6QmlModels.so.6
#3   ... libQt6Qml.so.6
#4   QV4::QObjectWrapper::getQmlProperty(...)
#5   QV4::QQmlContextWrapper::getPropertyAndBase(...)
#6   QV4::ExecutionContext::getProperty(QV4::String*)
#7   QV4::Runtime::TypeofName::call(...)
...
#10  QV4::Module::evaluate()
#12  QQmlEnginePrivate::createInternalContext(...)
#14  QQmlObjectCreator::createInstance(...)
#16  QQmlIncubatorPrivate::incubate(...)
#17  QQmlEnginePrivate::incubate(...)
#19  QQuickRepeater::regenerate()
#20  QQuickRepeater::setModel(QVariant const&)
...
#106 QQuickMouseArea::setPressed(...)
#107 QQuickMouseArea::mouseReleaseEvent(QMouseEvent*)

i.e. mouse release on the row's MouseArea → onClicked JS → submenuStack write → Repeater::setModel → regenerate() → incubating the next delegate → crash evaluating a typeof expression in that delegate's freshly-created QML context.

Suggested fix

Defer the submenuStack write with Qt.callLater(...) so the model swap (and the resulting Repeater.regenerate()) runs after the triggering click's C++/QML call stack has fully unwound, instead of mid-event. This idiom is already used elsewhere in this same codebase for the same class of problem (e.g. shell.qml:1461, shell.qml:1484, Ui/Panel.qml:29, plugins/agents/Panel.qml:433), so it matches existing style.

Patch (tested locally against 4.0.4, no more crashes after repeated rapid submenu enter/leave clicks):

--- a/shell/plugins/bar/widgets/Tray.qml
+++ b/shell/plugins/bar/widgets/Tray.qml
@@ function enterSubmenu(entry, title) {
   function enterSubmenu(entry, title) {
     var opener = submenuOpenerComponent.createObject(root, { menu: entry })
     if (!opener) return
     var stack = submenuStack.slice()
     stack.push({ opener: opener, title: title })
-    submenuStack = stack
+    // Defer the model swap: this runs synchronously from a row's own
+    // MouseArea.onClicked, and reassigning submenuStack destroys that row's
+    // delegate (incl. the MouseArea still mid-event) via Repeater.regenerate,
+    // which segfaults incubating the next delegate mid-teardown. Qt.callLater
+    // runs the swap after the click's call stack has fully unwound instead.
+    Qt.callLater(function() { root.submenuStack = stack })
     settleMenuLevel()
   }

   function leaveSubmenu() {
     if (submenuStack.length === 0) return
     var stack = submenuStack.slice()
     var top = stack.pop()
-    submenuStack = stack
-    top.opener.destroy()
+    // Same reentrancy hazard as enterSubmenu (see comment there): defer the
+    // model swap past the triggering click's call stack.
+    Qt.callLater(function() {
+      root.submenuStack = stack
+      top.opener.destroy()
+    })
     settleMenuLevel()
   }

The comment at shell/plugins/bar/widgets/Tray.qml around the row MouseArea.onClicked mentions the synchronous rebuild is intentional so "the next row lands under a cursor that hasn't moved" — Qt.callLater only defers to the next event-loop tick (microseconds), so cursor position is unaffected while the reentrant destroy is avoided.

Steps to reproduce

  1. Have a tray application whose menu contains at least one submenu entry (anything with nested QMenus exposed via org.kde.StatusNotifierItem/QsMenu, e.g. NetworkManager applet, some Electron apps).
  2. Open the tray icon's menu.
  3. Click the entry that has a submenu (chevron ›).
  4. Occasionally (more reliably when clicking quickly / repeatedly, or navigating back and forth), Quickshell crashes with SIGSEGV.

Happy to open a PR with the patch above if useful — already running it locally without recurrence.

vayners avatar Sep 22 '26 19:09 vayners

Tested and verified this fix in a live Omarchy test VM under Hyprland with a multi-level DBusMenu applet (tested up to 3 levels deep).

Verification Results:

  • Submenu drill-down & back navigation: Navigates cleanly into nested menus and returns via the back header without any delegate context corruption.
  • Rapid click stress test: Ran rapid enter/leave click cycles (< 100ms intervals); Quickshell remained completely stable with zero SIGSEGV or V4 engine warnings.
  • Checks & tests: qmllint is clean on Tray.qml, and existing shell unit tests (test/shell.d/tray-test.sh and test/shell.d/tray-menu-test.sh) continue to pass.
  • Conflict check: Checked against open PR #12877 (which modifies tray item activation and tooltip suppression) — there are no overlapping hunks or conflicts.

One minor defensive consideration for the PR: in enterSubmenu, guarding against the menu having closed before the tick runs:

Qt.callLater(function() {
  if (!root.trayMenuOpen) {
    opener.destroy()
    return
  }
  root.submenuStack = stack
})

sanjyay avatar Sep 22 '26 20:09 sanjyay

Thanks for the thorough verification, @sanjyay — nested submenu drill-down, back navigation, and the click stress test. Posted your defensive guard as a suggestion on PR #12970: https://github.com/omacom/omarchy/pull/12970#discussion_r4078562388

vayners avatar Sep 23 '26 02:09 vayners

Thanks for the report. One thing stands out in the backtrace: frames #7 to #10 (QV4::Runtime::TypeofName::call inside QV4::Module::evaluate(), during QQuickRepeater::regenerate() incubation) match the signature in #12677. That crash comes from a typeof module check evaluated in a fresh JS module context. Tray.qml imports TrayModel.js, and on quattro that file still ends with the if (typeof module !== "undefined") block. #12892 removes those blocks from every shell module.

So two fixes may apply here: #12970 moves the submenu stack swap out of the click handler, and #12892 removes the module-scope typeof trigger. If you can still reproduce the crash, testing each PR separately and reporting back on it would show whether one of them is enough.

jandrusk avatar Oct 08 '26 13:10 jandrusk

Thanks @jandrusk for the pointer to #12677 and the two candidate PRs.

Status on my side: no new crash report since 2026-09-29 (only one entry in ~/.cache/quickshell/crashes/), but I have not deliberately re-tested the submenu click path since, so I can't confirm it's gone. I'm short on time right now and haven't run either PR against a live session.

What I did check: a dry-run git apply --check of #12970 against the packaged shell on Omarchy 4.0.4 applies cleanly. #12892 does not apply to 4.0.4 (it targets quattro; conflicts in e.g. ImagePickerModel.js, MenuModel.js).

I'll report back if I get to a proper A/B test of the two PRs; otherwise feel free to treat the verification by @sanjyay on #12970 as the stronger signal.

vayners avatar Oct 10 '26 15:10 vayners

Checked against quattro at defbf90e8. The pattern you describe is still there: the submenu row's onClicked (shell/plugins/bar/widgets/Tray.qml:749-754) calls enterSubmenu(), which reassigns submenuStack synchronously (Tray.qml:100). That changes currentChildren, the model of the Repeater that owns the clicked row (Tray.qml:646), so the delegate is torn down and the new ones are incubated while its handler is still running. leaveSubmenu() (Tray.qml:108) swaps the same way. Nothing on quattro has changed this since 4.0.4.

The backtrace fits both explanations raised here. The crashing frame is typeof evaluated in Module::evaluate() while a new row is being created, which is TrayModel.js (imported at Tray.qml:9; the frames show it being evaluated as the new row's context is created) running its top-level if (typeof module !== "undefined") block (TrayModel.js:41). Looking up module walks the QML context chain, which is where the synchronous swap would put a half-destroyed object. That makes it the same signature as #12677, which crashed with no click involved. The mechanism is explained from source but not reproduced: it needs a tray app with nested DBusMenu submenus and is intermittent. I did not run it on a worker.

Two open pull requests address it from either side:

  • #12970 defers both stack swaps with Qt.callLater and drops stale callbacks when the menu closes. It already carries Fixes #12938, and it is being reviewed as its own item.
  • #12892 removes the typeof module blocks from every shell JS module, TrayModel.js included. It currently has merge conflicts with quattro.

Either one may be enough on its own. An A/B test of the two, as suggested above, is the most useful thing a reporter can add.

This was reviewed by Claude Opus 5.5 alone. The second opinion (Codex Medium) answered a ping but turned down the review because the day's review budget was spent, so no second model checked this. The issue now waits on the maintainer's decision about #12970 and #12892.

omarchybot avatar Oct 12 '26 11:10 omarchybot