Skip to content

[cp-beta] [macOS] Resume app lifecycle on becomeActive to avoid frozen UI after occlusion - #190435

Merged
auto-submit[bot] merged 1 commit into
flutter:flutter-3.47-candidate.0from
cbracken:cp-macos-occlusion-bug-fix
Aug 3, 2026
Merged

auto-submit[bot] merged 1 commit into
flutter:flutter-3.47-candidate.0from
cbracken:cp-macos-occlusion-bug-fix

Conversation

@cbracken

@cbracken cbracken commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

Issue Link:

#155977

Impacted Users:

All macOS desktop end-users and developers of Flutter apps on macOS. The issue reproduces reliably and frequently on macOS desktop apps launched via Finder/LaunchServices when returning to an occluded app on the same screen (e.g. via Cmd-Tab, Mission Control, or clicking away and back).

Impact Description:

TL;DR: This issue can cause macOS desktop apps to visibly lock up.

When a Flutter macOS desktop app is occluded and then brought back to the foreground on the same screen (e.g. via Cmd-Tab or Mission Control), macOS does not reliably deliver an
NSApplicationDidChangeOcclusionState (visible=YES) notification.

Because _visible was driven solely by occlusion notifications, returning to the foreground caused applicationWillBecomeActive to read a stale _visible = NO and resend AppLifecycleState.hidden to the engine. Consequently, the framework stays in the hidden lifecycle state: animations are muted, frame scheduling is gated off, and no vsyncs are requested, leaving the UI completely frozen and unresponsive even though the window is frontmost and active.

This is a showstopper bug for shipping production macOS Flutter apps.

Changelog Description:

[flutter/155977] When returning to an occluded macOS desktop app via Cmd-Tab or Mission Control [on macOS], the UI no longer freezes due to missed occlusion notifications.

Workaround:

Yes. When the app is frozen after returning via Cmd-Tab or Mission Control, clicking the application's Dock icon will bring it back to life (triggering a WindowServer-driven occlusion state update that recovers rendering).

Risk:

What is the risk level of this cherry-pick?

  • Low
  • Medium
  • High

This patch adds an additional means of updating its _visible state from NO to YES during handleWillBecomeActive:, this then triggers frames to start pumping. This does NOT add any additional means to transition the _visible state from YES to NO.

It's simple, properly tested and doesn't introduce any additional bugs.

It's worth noting that this doesn't reasolve ALL bugs in the code; there's still a small bug in the code:

In the case where _visible is YES and the user minimises the window (which @bdero mentions fires no occlusion notification) and then cmd-tabs back, handleWillBecomeActive would result in kResumed even though the window is still down in the dock and not on-screen (because the loop find no visible window but leaves the stale _visible = YES untouched.

This is intentionally NOT fixed in this patch; which addresses only the high priority visual lock up. The impact of the remaining issue is much, much smaller.

Test Coverage:

Are you confident that your fix is well-tested by automated tests?

  • Yes
  • No

Validation Steps:

  1. Build and launch a Flutter macOS desktop app via Finder or open (LaunchServices GUI launch) on macOS.
  2. Open another application window so that the Flutter app is occluded.
  3. Switch away from the Flutter app and then return to it on the same screen multiple times using Cmd-Tab or Mission Control.
  4. Verify that upon returning to the frontmost Flutter app, the UI remains responsive, animations continue to play, and new frames render without freezing.
  5. Verify that activating the app via Cmd-Tab while all its windows are minimized does not improperly resume rendering.
  6. Run the macOS engine unit tests (FlutterEngineTest) to verify all lifecycle tests in HandleLifecycleStates pass.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

…n UI after occlusion

### Issue Link:
flutter#155977

### Impacted Users:
All macOS desktop end-users and developers of Flutter apps on macOS. The
issue reproduces reliably and frequently on macOS desktop apps launched
via Finder/LaunchServices when returning to an occluded app on the same
screen (e.g. via Cmd-Tab, Mission Control, or clicking away and back).

### Impact Description:
TL;DR: This issue can cause macOS desktop apps to visibly lock up.

When a Flutter macOS desktop app is occluded and then brought back to
the foreground on the same screen (e.g. via Cmd-Tab or Mission Control),
macOS does not reliably deliver an
`NSApplicationDidChangeOcclusionState` (visible=YES) notification.

Because `_visible` was driven solely by occlusion notifications,
returning to the foreground caused `applicationWillBecomeActive` to read
a stale `_visible = NO` and resend `AppLifecycleState.hidden` to the
engine. Consequently, the framework stays in the `hidden` lifecycle
state: animations are muted, frame scheduling is gated off, and no
vsyncs are requested, leaving the UI completely frozen and unresponsive
even though the window is frontmost and active.

This is a showstopper bug for shipping production macOS Flutter apps.

### Changelog Description:
[flutter/155977] When returning to an occluded macOS desktop app via Cmd-Tab or Mission Control [on macOS], the UI no longer freezes due to missed occlusion notifications.

### Workaround:
Yes. When the app is frozen after returning via Cmd-Tab or Mission Control, clicking the application's Dock icon will bring it back to life (triggering a WindowServer-driven occlusion state update that recovers rendering).

### Risk:
What is the risk level of this cherry-pick?

  - [x] Low
  - [ ] Medium
  - [ ] High

This patch adds an additional means of updating its `_visible` state
from `NO` to `YES` during `handleWillBecomeActive:`, this then triggers
frames to start pumping. This does NOT add any additional means to
transition the `_visible` state from `YES` to `NO`.

It's simple, properly tested and doesn't introduce any additional bugs.

It's worth noting that this doesn't reasolve ALL bugs in the code;
there's still a small bug in the code:

In the case where `_visible` is `YES` and the user minimises the window
(which @bdero mentions fires no occlusion notification) and then cmd-tabs
back, `handleWillBecomeActive` would result in `kResumed` even though the
window is still down in the dock and not on-screen (because the loop find
no visible window but leaves the stale `_visible` = `YES` untouched.

This is intentionally NOT fixed in this patch; which addresses only the
high priority visual lock up. The impact of the remaining issue is much,
much smaller.

### Test Coverage:
Are you confident that your fix is well-tested by automated tests?

  - [x] Yes
  - [ ] No

### Validation Steps:
1. Build and launch a Flutter macOS desktop app via Finder or `open` (LaunchServices GUI launch) on macOS.
2. Open another application window so that the Flutter app is occluded.
3. Switch away from the Flutter app and then return to it on the same screen multiple times using Cmd-Tab or Mission Control.
4. Verify that upon returning to the frontmost Flutter app, the UI remains responsive, animations continue to play, and new frames render without freezing.
5. Verify that activating the app via Cmd-Tab while all its windows are minimized does not improperly resume rendering.
6. Run the macOS engine unit tests (`FlutterEngineTest`) to verify all lifecycle tests in `HandleLifecycleStates` pass.
@cbracken
cbracken requested review from bdero and gaaclarke August 3, 2026 09:45
@cbracken
cbracken requested a review from a team as a code owner August 3, 2026 09:45
@cbracken cbracken added the cp: review Cherry-picks in the review queue label Aug 3, 2026
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 3, 2026
@flutter-dashboard

Copy link
Copy Markdown

This pull request was opened from and to a release candidate branch. This should only be done as part of the official Flutter release process. If you are attempting to make a regular contribution to the Flutter project, please close this PR and follow the instructions at Tree Hygiene for detailed instructions on contributing to Flutter.

Reviewers: Use caution before merging pull requests to release branches. Ensure the proper procedure has been followed.

@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-macos Building on or for macOS specifically a: desktop Running on desktop team-macos Owned by the macOS platform team labels Aug 3, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@cbracken

cbracken commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

/gemini review

I'm gonna regret this. 🙄

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates handleWillBecomeActive: in FlutterEngine.mm to determine application visibility by checking the visibility of active windows, addressing an issue where the occlusion state can latch stale. It also adds corresponding unit tests in FlutterEngineTest.mm. The review feedback suggests fully updating the _visible state to prevent it from remaining stale when all windows are minimized, and recommends correcting pointer asterisk placement in both files to align with the Objective-C style guide.

@bdero bdero left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@LongCatIsLooong LongCatIsLooong added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 3, 2026
@auto-submit
auto-submit Bot merged commit 075b1ec into flutter:flutter-3.47-candidate.0 Aug 3, 2026
10 checks passed
@cbracken
cbracken deleted the cp-macos-occlusion-bug-fix branch August 17, 2026 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD cp: review Cherry-picks in the review queue engine flutter/engine related. See also e: labels. platform-macos Building on or for macOS specifically team-macos Owned by the macOS platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants