Skip to content

Fix the menu bar recovery test flakes - #49

Merged
gshost1 merged 6 commits into
mainfrom
fix/ci-flakes
Nov 6, 2026
Merged

gshost1 merged 6 commits into
mainfrom
fix/ci-flakes

Conversation

@gshost1

@gshost1 gshost1 commented Nov 6, 2026

Copy link
Copy Markdown
Owner

What and why

MenubarRecoveryTests.testRecoveryWaitsForMenuToClose and its neighbours failed on CI from timing alone. They gave a 20 ms recovery a fixed 150 ms window, and on a slow runner recovery ran later: in run 42090154795 the "recovered" log line came after the failed asserts. MenubarItemController.swift is untouched, so the product is unchanged.

  • "Was replaced" checks now wait until the item really is replaced (replaced(_:from:within:), up to 30 s). A passing run returns as soon as it happens. The 30 s ceiling is there because a throttled local run stalled the main thread for over 5 s.
  • "Was not replaced" checks keep the short fixed wait. A late recovery can only make those pass.
  • Burst test: the "not replaced yet" check is skipped for a step where a gap reached the delay. If a recovery fired mid-burst, the later requests correctly schedule one more. The test waits for the item to hold still before its coalescing check, which stays strict when no gap was long. It also stops its controller on an early exit, like its neighbours.

Dropped from this branch (reverted in the history; net diff is one file):

  • The RelayHostTests fix: Let a remote agent ring the Mac to ask for the relay #40 (feat/relay-doorbell, 33596aa) fixes the same two races with its own waitConnected, and the two branches conflicted.
  • The RelayTests keepalive parking: that flake (an unlocked read of RecordingSleeper.delays) was already fixed on main by f0598da, and parking the keepalive would have removed pinger-versus-drop coverage from that test.

How tested

  • Before: the old test failed 17/30 at background priority with 8 CPU-hog processes. Plain load never failed it.
  • After (the child's runs): 30/30 under the same throttling, and the target test 30/30 normal with load.
  • After the burst fix (897e205): swift test --filter MenubarRecoveryTests 5× gave Executed 4 tests, with 0 failures each time, and 15× at background priority (taskpolicy -b) passed 15/15.
  • Code review (medium) found 9 issues:
    • Fixed: the burst test's second recovery and the missing defer.
    • Resolved by dropping the RelayHostTests / RelayTests commits: the Let a remote agent ring the Mac to ask for the relay #40 conflict, the 5 s ceiling, the prompt-count order, the parked-sleep keying and the lost pinger coverage.
    • Accepted: the "not replaced" checks can't prove absence on a stalled runner, which is inherent to a negative check. The polling helper isn't shared with RelayHostTests / BackgroundUpdateTests: a cleanup, not a fix.

Risks

Test-only. On a broken build a "was replaced" check now takes up to 30 s to fail instead of 150 ms.

What's left

  • Once this and Let a remote agent ring the Mac to ask for the relay #40 merge, the AGENTS.md known-flakes line (gitignored, edited by hand) can drop the menu bar and RelayHost entries; the RelayTests socket-drop one is already fixed by f0598da.
  • Other flakes seen in CI logs, not fixed:
    • RelayTests.testFailedKeepalivePingClosesTheSocketAndReconnects, under TSan in run 42101992705: a second ping fits in before the receive loop cancels the keepalive. Production impact is a duplicate log line, and the fix would touch Relay.swift.
    • AppUpdaterTests.testABackgroundRestartThatKeepsFailingIsLeftForTheNextCheck, in run 41973500586: 2 against 3.

testRevokeAllTurnsTheRelayOffAndAKeyScopedRevokeLeavesItOn and
testTurningTheRelayOnRefusesWhatItCanBeforeTouchID turned the relay off
right after setRelay(enabled: true) and then asserted the fake socket was
closed. The relay loop dials on a detached task, so on a slow runner
(ThreadSanitizer on CI) the cancel can land before the dial: the loop sees
the cancellation, ends without ever opening the socket, and there is
nothing to close. Under TSan with eight busy CPU hogs locally this failed
11 of 30 and 1 of 30 runs.

Both tests now wait for the connected state first, as the other host tests
in the class already do. The product behaviour is right as it is: a relay
turned off before it dials never dials.
testRecoveryWaitsForMenuToClose and its neighbours slept 150 ms after a
20 ms recovery delay and then asserted the status item had been replaced.
Recovery runs on the main actor, and on a slow CI runner it ran later than
that: run 42090154795 logged "menubar item recovered" after the failed
asserts, in a test that took 0.89 s. Locally, the old file failed 17 of 30
runs at background QoS with eight CPU hogs (wake and hidden-item tests).

The positive checks now poll until the item is replaced (30 s ceiling; a
throttled local run stalled the main thread past 5 s). Checks that nothing
was replaced keep a short fixed wait, since a late recovery can only make
those pass. The burst test only asserts "not yet replaced" for steps where
no gap between requests reached the delay, the one case in which recovery
may legitimately fire. MenubarItemController is unchanged.
testSocketDropCancelsOpenRequestsAndReconnects failed once on a9f9132 with
the backoff list read as [] although the second socket had been dialed.
The cause was a test-side data race: the pinger appended a 3600 s keepalive
sleep to RecordingSleeper every millisecond while the test read the array
without the lock. ThreadSanitizer reports exactly that race when the
a9f9132 helper is put back, and f0598da already fixed it by reading under
the lock; the test has not failed on CI since.

What was left is the busy keepalive itself: thousands of recorded entries
and lock round-trips per run, filtered out at the end. RecordingSleeper can
now park one sleep length until cancelled, so the keepalive never ticks and
the test asserts the exact backoff list, [1].
When a slow runner lets a gap in the burst reach the recovery delay, a
recovery fires mid-burst and the remaining requests correctly schedule
one more. The test took the first replacement as final and then failed
its coalescing check when the second one swapped the item. It now waits
for the item to hold still for longer than the delay in that case only;
a burst with no long gap keeps the strict check. The test also stops its
controller on any early exit, like its neighbours.
@gshost1
gshost1 merged commit a0ac1a2 into main Nov 6, 2026
8 checks passed
@gshost1 gshost1 mentioned this pull request Nov 6, 2026
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.

1 participant