|
| 1 | +# macOS: system tray icon and label updates are not delivered while the menu is open |
| 2 | + |
| 3 | +A menu tray whose menu was opened through `ShowMenu()` / `OpenMenu()` stops |
| 4 | +updating. Every `SetIcon`, `SetTemplateIcon` and `SetLabel` call issued while |
| 5 | +the menu is open is delivered only after the menu is dismissed, and then all at |
| 6 | +once. |
| 7 | + |
| 8 | +Tested on macOS 27.0 (26A428), arm64, against `v3` at `master` (`4b1f3484`). |
| 9 | + |
| 10 | +## Run it |
| 11 | + |
| 12 | +``` |
| 13 | +go run . -interval 250ms -quit-after 12s -dismiss-after 6s |
| 14 | +``` |
| 15 | + |
| 16 | +The app builds against the checked out `v3` tree through the `replace` |
| 17 | +directive in `go.mod`. It needs no bundle and no permissions. |
| 18 | + |
| 19 | +It creates a menu tray, alternates a circle and a square template icon plus a |
| 20 | +`tick N` label every interval, opens its own menu after `-open-menu-after`, and |
| 21 | +dismisses it with a posted Escape after `-dismiss-after`. Use |
| 22 | +`-open-menu-after 0 -quit-after 60s` to click the tray by hand instead. |
| 23 | + |
| 24 | +Every tick also schedules two probe blocks on the main thread, one through |
| 25 | +`dispatch_async(dispatch_get_main_queue(), ...)` (what `systemtray_darwin.m` |
| 26 | +uses) and one through `CFRunLoopPerformBlock(kCFRunLoopCommonModes)` (what |
| 27 | +`mainthread_darwin.go` uses since #6026), and logs the current main run loop |
| 28 | +mode. The mode line proves a nested tracking loop was really active. |
| 29 | + |
| 30 | +`-observe-title` reads the status item button title back from the main thread. |
| 31 | +It shows the visible label going stale, but walking `[NSApp windows]` during |
| 32 | +tracking often ends tracking after a second, so it is off by default. |
| 33 | + |
| 34 | +## Result |
| 35 | + |
| 36 | +Native menu tracking is entered from a main queue drain, so the queue is not |
| 37 | +drained again until the menu closes: |
| 38 | + |
| 39 | +``` |
| 40 | +trace showMenu enqueued on the main queue |
| 41 | +trace showMenu block started draining on the main queue |
| 42 | +trace showMenu entering mouseDown: (blocks for the menu lifetime) |
| 43 | +``` |
| 44 | + |
| 45 | +While that block sits in `mouseDown:`, run loop blocks keep arriving on time |
| 46 | +and queue blocks do not arrive at all: |
| 47 | + |
| 48 | +``` |
| 49 | +T+ 3.286s mode main run loop is in NSEventTrackingRunLoopMode at tick 13 |
| 50 | +T+ 3.290s ran CFRunLoopPerformBlock(common) scheduled with tick 13, 0.004s late |
| 51 | +... |
| 52 | +T+ 8.293s ran CFRunLoopPerformBlock(common) scheduled with tick 33, 0.005s late |
| 53 | +trace menuDidClose: |
| 54 | +trace showMenu mouseDown: returned, menu dismissed |
| 55 | +trace systemTraySetIcon block ran |
| 56 | +trace systemTraySetLabel block ran |
| 57 | +T+ 8.321s ran dispatch_async(main queue) scheduled with tick 13, 5.029s late |
| 58 | +trace systemTraySetIcon block ran |
| 59 | +trace systemTraySetLabel block ran |
| 60 | +T+ 8.326s ran dispatch_async(main queue) scheduled with tick 14, 4.791s late |
| 61 | +``` |
| 62 | + |
| 63 | +Every `systemTraySetIcon` and `systemTraySetLabel` block for the tracking |
| 64 | +window runs in that burst. The visible icon and label are frozen for the whole |
| 65 | +time the menu is open, then jump to the newest value. |
| 66 | + |
| 67 | +Expected: updates issued while the menu is open reach the tray while it is |
| 68 | +open, the way main thread work dispatched from Go does since #6026. |
| 69 | + |
| 70 | +Full transcripts are in `logs/`. |
| 71 | + |
| 72 | +## Variants |
| 73 | + |
| 74 | +`trace.patch` adds the `trace` lines above to |
| 75 | +`v3/pkg/application/systemtray_darwin.m`. Apply it, rebuild, and the ordering |
| 76 | +of the click path is visible in the log. |
| 77 | + |
| 78 | +`cfrunloop-fix.patch` routes all five `dispatch_async(dispatch_get_main_queue())` |
| 79 | +sites in `systemtray_darwin.m` through `CFRunLoopPerformBlock` plus |
| 80 | +`CFRunLoopWakeUp`, like `dispatchOnMainThread`. With it applied the stall is |
| 81 | +gone: during `NSEventTrackingRunLoopMode` the tray keeps counting and the title |
| 82 | +read-back follows the tick (`logs/patched-all-sites.log`). |
| 83 | + |
| 84 | +Patching `showMenu` alone is not enough, and is worse: tracking then starts |
| 85 | +inside a run loop block callout, and blocks added afterwards do not run until |
| 86 | +that callout returns, so the Go caller blocks in `SetTemplateIcon` for the whole |
| 87 | +menu lifetime (`logs/patched-showmenu-only.log`). The all-sites patch is |
| 88 | +reported as data, not as a proposed fix. |
| 89 | + |
| 90 | +## Not answered here |
| 91 | + |
| 92 | +Whether a real mouse click starves the queue the same way. On macOS 27 the |
| 93 | +local event monitor added for #5919 can assign `statusItem.menu` directly, in |
| 94 | +which case AppKit may enter tracking from the event loop rather than from |
| 95 | +`showMenu`'s queue block. Run the traced build with `-open-menu-after 0`, click |
| 96 | +the tray by hand, hold the menu open a few seconds, and compare the trace order. |
0 commit comments