Repository navigation
Commit 7880b86
authored
fix: stop the macOS app crashing in the background (#4)
## Problem
XPath 1.0.0 (54) crashed on macOS 26.7.1 while it was in the background:
`EXC_BAD_ACCESS` at `0x0` on `io.flutter.raster`, in
`impeller::Canvas::SetupRenderPass()` (canvas.cc:1615), called from
`EmbedderExternalViewEmbedder::SubmitFlutterView`.
## Root cause: a Flutter engine bug
This is
[flutter/flutter#185394](flutter/flutter#185394),
"[Impeller] Crash on macOS when app is foregrounded" (open, P2,
team-macos). The macOS back-buffer cache (`FlutterBackBufferCache`) can
hand back a surface of the wrong size when the window's frame size
changes and then changes back (A → B → A). Examples are a display
hot-plug or reconfiguration, a Space or full-screen change, a window
drag, or bringing the app to the front. Impeller wraps the mismatched
`MTLTexture`, the texture invalidates itself, and the render target
silently drops it. `SetupRenderPass` then dereferences the null texture.
- Fix:
[flutter/flutter#192522](flutter/flutter#192522)
is still **open** (not merged). It is not in 3.47.7 (the latest stable,
per its CHANGELOG) or in the 3.50 beta.
- Also reported: #193095 (same stack) and #192829 (Cmd-Tab on macOS 27,
closed as a duplicate).
**Reproduced on our pinned SDK (3.47.6, release build).** This used a
minimal scratch app, not XPath. A Swift timer shrinks the window by 1 pt
and restores it every 2.5 s, which is the driver from #192522. The
Flutter side redraws a text once a second, the way `XpClock` does:
| Renderer | UI | Result |
|---|---|---|
| Impeller (default) | 1 s ticking text | **SIGSEGV after 5 s, at flip
#1.** The stack matches the field crash frame for frame (canvas.cc:1615
/ 372, dl_dispatcher.cc:1318, embedder_external_view_embedder.cc:376 /
598). |
| Impeller | still UI | survived 60 s (17 flips) |
| Skia (`FLTEnableImpeller=false`) | 1 s ticking text | **survived 90 s
(36 flips)**, with no validation errors and the log line `Using the Skia
rendering backend (Metal).` |
## Changes
1. **`macos/Runner/Info.plist`: `FLTEnableImpeller = false`** (with a
comment linking the issues). I checked the 3.47.6 engine source to
confirm macOS still reads this key: `FlutterDartProject.enableImpeller`
reads it, and `FlutterEngine runWithEntrypoint` then skips
`--enable-impeller=true` and uses Skia (Metal). XPath uses no
Impeller-only features (no fragment shaders, Flutter GPU or backdrop
filters). Goldens and widget tests are unaffected because `flutter test`
does not read `Info.plist`.
2. **`XpClock` follows the app lifecycle** using an
`AppLifecycleListener`. It ticks only while the app is `resumed`. When
the app is `inactive` (visible but not in front), `hidden` (occluded or
minimised) or `paused`, the timer stops. On resume, every live duration
is rebuilt at once against the current time. The new
`XpClock.isLive(context)` tells widgets whether the app is in front.
3. **`XpSpinner`** stands still while `!XpClock.isLive`, as it already
does under Reduce Motion.
Change 1 is the fix. Changes 2 and 3 mean XPath draws no frames of its
own while it is not in front (it used to draw one a second for the
clock, and 60 fps per spinner). That lowers the crash exposure and saves
CPU and battery. On its own it does **not** fix the engine bug: a
display change still causes a frame, and upstream reports idle apps
crashing too.
## Tests
`apps/xp_desktop/test/widgets/xp_clock_test.dart` drives the real
`flutter/lifecycle` platform channel, the same messages the macOS
embedder sends:
- `XpClock ticks every second while the app is in front`
- `XpClock stops while inactive|hidden|paused and catches up on resume`
- `XpClock a still clock stays still in front and away`
- `XpClock starts still when the app is already away`
- `XpClock without a clock above, the system time is live`
- `XpClock spinners stand still while the app is away`
## Trade-offs and follow-ups
- Skia is Flutter's legacy renderer on macOS. The Flutter docs warn that
the Impeller opt-out will be removed in a future release, and with Skia
there is no wide-gamut colour. Once a Flutter stable ships #192522,
remove the key (the Info.plist comment says so).
- While XPath is visible but not in front, elapsed times stay frozen
until it comes back to the front, and then catch up at once.1 parent cb6e796 commit 7880b86
4 files changed
Lines changed: 223 additions & 10 deletions
File tree
- apps/xp_desktop
- lib/widgets
- macos/Runner
- test/widgets
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
11 | 18 | | |
12 | 19 | | |
13 | 20 | | |
| |||
31 | 38 | | |
32 | 39 | | |
33 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
34 | 46 | | |
35 | 47 | | |
36 | 48 | | |
37 | 49 | | |
38 | 50 | | |
| 51 | + | |
39 | 52 | | |
40 | 53 | | |
| 54 | + | |
41 | 55 | | |
42 | 56 | | |
43 | 57 | | |
44 | 58 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
51 | 87 | | |
52 | 88 | | |
53 | 89 | | |
54 | 90 | | |
| 91 | + | |
55 | 92 | | |
56 | 93 | | |
57 | 94 | | |
58 | 95 | | |
59 | 96 | | |
60 | | - | |
61 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
62 | 101 | | |
63 | 102 | | |
64 | 103 | | |
| |||
71 | 110 | | |
72 | 111 | | |
73 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | | - | |
| 38 | + | |
| 39 | + | |
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
23 | 32 | | |
24 | 33 | | |
25 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
0 commit comments