Skip to content

[client] Fix tray menu item leak on relayout - #6939

Open
pappz wants to merge 3 commits into
mainfrom
fix/tray-menu-item-leak
Open

[client] Fix tray menu item leak on relayout#6939
pappz wants to merge 3 commits into
mainfrom
fix/tray-menu-item-leak

Conversation

@pappz

@pappz pappz commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Describe your changes

Fix a memory leak in the tray menu: every relayout built a brand-new menu tree while the replaced one was never destroyed. Wails keeps each MenuItem in a process-global map until Destroy is called, so all items of every previous tree
(labels, click closures, bitmaps, submenus) stayed reachable forever and the GUI's memory grew with every status transition, profile refresh or language change.

The fix destroys the replaced tree right after SetMenu swaps in the new one. Since the old items can now be destroyed, the Connect/Disconnect click handlers no longer capture their own menu item for the async error path; they resolve
the current item under the menu lock instead. Additionally loadProfiles now relayouts only when the profile rows actually changed, which drops the redundant full menu rebuilds (previously 2-3 per status transition, now typically 1).

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • This change does not modify the public API, gRPC protocols, functionality behavior, CLI / service flags, or introduce a new feature — OR I have discussed it with the NetBird team beforehand (link the issue / Slack thread in the description). See CONTRIBUTING.md.

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Summary by CodeRabbit

  • Bug Fixes
    • Improved tray menu reliability for connect/disconnect actions, including safer handling while the menu is being rebuilt.
    • Prevented stale tray menu items from being referenced by current actions.
    • Reduced unnecessary tray menu relayouts by updating profiles only when user or profile list actually changes.
    • Improved tray updates when the active profile changes via background status flows.
    • Fixed concurrency issues updating the session-expires tray label to avoid conflicts during menu relayouts.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 8a7f38ad-4a42-4860-9f53-9c0f027f3c13

📥 Commits

Reviewing files that changed from the base of the PR and between 29b9734 and 8a6df99.

📒 Files selected for processing (2)
  • client/ui/tray.go
  • client/ui/tray_session.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • client/ui/tray.go

📝 Walkthrough

Walkthrough

Tray menu rebuilding now destroys replaced menus, asynchronous connect/disconnect handlers resolve current menu items safely, session labels update under the menu lock, and profile loading relayouts only when relevant data changes.

Changes

Tray menu updates

Layer / File(s) Summary
Menu replacement lifecycle
client/ui/tray.go, client/ui/tray_session.go
Menu relayout snapshots and destroys the previous menu, while session expiry labels update the current item under menuMu.
Current item click handling
client/ui/tray.go
Connect and disconnect callbacks use lock-safe current item lookup when disabling or restoring tray actions.
Conditional profile relayout
client/ui/tray_profiles.go
Profile loading compares the username and profile slice before updating cached state and triggering menu relayout.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the fix, but the issue ticket, stack, and documentation rationale sections are incomplete. Add the issue ticket number/link, fill in the Stack section, and explain why documentation is not needed.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: fixing a tray menu item leak during relayout.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/tray-menu-item-leak

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@client/ui/tray.go`:
- Around line 70-71: Update the tray item enablement flow around the visible
menuMu locking logic to keep menuMu held from locating the current item through
completion of item.SetEnabled. Remove the guidance to snapshot under the lock
and call the item afterward, ensuring relayoutMenu cannot rebuild or destroy the
item before SetEnabled returns.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c75ca01b-cf35-483c-8280-d50ab3ee4968

📥 Commits

Reviewing files that changed from the base of the PR and between 9269b56 and 29b9734.

📒 Files selected for processing (2)
  • client/ui/tray.go
  • client/ui/tray_profiles.go

Comment thread client/ui/tray.go Outdated
@github-actions

github-actions Bot commented Jul 28, 2026

Copy link
Copy Markdown

Release artifacts

Built for PR head 8a6df99 in workflow run #17192.

Artifact Link
All release artifacts Download
Linux packages Download
Windows packages Download
macOS packages Download
UI artifacts Download
UI macOS artifacts Download

GHCR images (amd64)

This comment is updated by the Release workflow. Artifact links expire according to the workflow retention policy.

lixmal
lixmal previously approved these changes Jul 28, 2026
@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot mentioned this pull request Jul 29, 2026
8 tasks
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.

2 participants