Skip to content

fix(apple): Load NSImage in MenuBar asynchronously#8090

Merged
jamilbk merged 1 commit into
mainfrom
fix/nsimage-async
Feb 11, 2025
Merged

fix(apple): Load NSImage in MenuBar asynchronously#8090
jamilbk merged 1 commit into
mainfrom
fix/nsimage-async

Conversation

@jamilbk
Copy link
Copy Markdown
Member

@jamilbk jamilbk commented Feb 11, 2025

After further investigation, it appears that the NSImage initializer loads and decodes images synchronously from the disk. In the MenuBar, we are "lazy-loading" these images, but since the menu is constructed as part of app initialization, we are effectively loading these when the app boots, in FirezoneApp.

After loading, these are cached, but the initial can hang the UI thread on app launch for slow systems.

Unfortunately, NSImage does not formally conform to @Sendable. However, this may be a nuance that isn't true in most cases, such as when treating NSImage instances as read-only from only a single thread.

As such, we wrap NSImage with our own struct, and mark it @unchecked Sendable. This allows us to load the images on a background thread and assign them to their UI thread counterparts in an async manner.

See further discussion:

Related: #7771

@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
firezone ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 11, 2025 6:04am

@sentry
Copy link
Copy Markdown

sentry Bot commented Feb 11, 2025

Sentry Issue: APPLE-CLIENT-10

extension MenuBar: NSMenuDelegate {
}

extension NSImage {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Dead code

Copy link
Copy Markdown
Member

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Our tiny icons can cause a hang? That seems quite unlikely. We'll see if this fixes things though.

@sentry
Copy link
Copy Markdown

sentry Bot commented Feb 11, 2025

Sentry Issue: APPLE-CLIENT-20

@sentry
Copy link
Copy Markdown

sentry Bot commented Feb 11, 2025

Sentry Issue: APPLE-CLIENT-22

@jamilbk jamilbk added this pull request to the merge queue Feb 11, 2025
Merged via the queue into main with commit 9f88cd1 Feb 11, 2025
@jamilbk jamilbk deleted the fix/nsimage-async branch February 11, 2025 14:52
jamilbk added a commit that referenced this pull request Feb 20, 2025
github-merge-queue Bot pushed a commit that referenced this pull request Feb 20, 2025
Loading images async isn't fixing the App Hanging reports we continue to
receive, so it's something else. Rather than trying to load them
asynchronously, we revert that change.

We instead eager-load all images needed by the MenuBar at init instead
of lazy-loading them, which in rare cases could cause apparent UI hangs.
If we can't load them we log an error but continue to try an operate, as
the icons are not strictly needed for Firezone operation.


Reverts #8090
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