Files
buzz/mobile/lib/shared/push/push_wake_runner.dart
Tom Brow 298543f555 fix(mobile): report a push wake claim it stopped waiting for (#9931)
## What

An Android push wake that runs out of time can leave a claim still
running. If that claim then wins, the message is recorded as shown but
was never returned. No later wake finds it, so the user is never told.
This makes the wake report that case, and the native handler shows its
generic notification for it.

- `resolvePushWake` tracks the message each community is claiming. After
the claim grace it sets `PushWakeResult.unreported` when a claim is
still running for a message with something to show (a message whose body
is empty would not be returned anyway). It is read in the same
synchronous step as the claimed-message snapshot, so a claim that
finishes before the snapshot is returned, and one that finishes after it
is counted.
- `PushWakeRunner` reports `unreported` when any pass does, because a
message lost in one pass stays lost.
- The channel payload carries `unreported`. The native handler posts the
named notifications, and the generic notification beside them, silently
when something already alerted. If the user already cleared the generic
card this wake posted at 2 seconds, it stays cleared. The generic is
marked as standing in for a lost message, so later wakes, which clear an
unmarked generic as before, leave it until the user clears it. A later
wake that re-posts the generic keeps the mark.
- It stays separate from `incomplete`, which means a later wake retries.
Folding the two would show the generic beside named notifications on
every retryable partial wake.
- `ShownMessageStore.claim` documents that a caller that stops waiting
for a claim must report it. A claim that later returns false still costs
one unnecessary generic notification, which is accepted.

The realistic trigger is storage that stalls after the claim marker is
created, during the watermark and prune I/O in
`DirectoryShownMessageStore.claim` (a pre-existing finding from the
review of the Android wake handler). The store's order is unchanged. The
marker has to come first for atomicity, and this signal turns the stall
into a generic notification instead of silence.

## Validation

- The push-wake journey test's stalled-storage step now asserts
`unreported`, and a stalled claim for a message with an empty body
asserts it is not set. A wake cut short in the name lookup, outside any
claim, asserts it is not set and still returns its messages. The runner
step asserts that a message lost in one pass is still reported after a
clean rerun. The channel payload test covers both values.
- Forcing the signal off fails the stalled-storage step.
- `WakeNotifierGenericTest` (instrumented) drives the generic
notification against the real notification manager on a Pixel 9a: an
unmarked card clears on a clean wake, a kept card survives clean wakes
and a later 2-second re-post, and a card the user cleared before the
result is not posted again. Each step waits for the state it expects, a
re-post check waits for a newer post, and checks that nothing changed
run after a barrier notification. Its two updates wait 700 ms first, so
Android's update rate limit can't drop them on quick reruns (12
back-to-back runs passed). Removing the cleared-card check, or dropping
the mark on re-post, fails it.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-25 06:27:18 +00:00

98 lines
3.8 KiB
Dart

import 'dart:async';
import 'push_wake_resolver.dart';
/// Runs wakes one at a time in this process. A wake that arrives while one is
/// running does not start a second run: it sets a rerun flag, and the running
/// wake looks again when it finishes, so a message that arrived after its
/// fetch is still found. Every caller waiting gets one result, with the
/// notifications of every pass.
///
/// All passes of a run share one [budget], which stays inside the native
/// watchdog, so wakes that keep arriving can't extend a run past it. A rerun
/// starts only when at least [minimumPass] of the budget is left; otherwise the
/// run returns what it has, and a message it skipped is found by the next
/// wake, which fetches from before the newest shown message.
class PushWakeRunner {
PushWakeRunner(
this._resolve, {
this.budget = const Duration(seconds: 5),
this.minimumPass = const Duration(seconds: 1),
});
/// Runs one pass within the time it is given.
final Future<PushWakeResult> Function(Duration budget) _resolve;
/// The longest a run may take, over all its passes.
final Duration budget;
/// The least time worth starting a rerun with.
final Duration minimumPass;
Future<PushWakeResult>? _running;
bool _rerun = false;
/// Runs a wake, or joins the one in progress. [remaining] is the time the
/// caller has left, when it started counting before this call (the native
/// handler's watchdog runs from the push, not from when this engine became
/// ready). A new run takes the shorter of it and [budget]; a joined call
/// changes nothing, since the run in progress already ends inside it.
Future<PushWakeResult> wake({Duration? remaining}) {
final running = _running;
if (running != null) {
_rerun = true;
return running;
}
final runBudget = remaining != null && remaining < budget
? remaining
: budget;
return _running = _loop(runBudget).whenComplete(() => _running = null);
}
Future<PushWakeResult> _loop(Duration budget) async {
final elapsed = Stopwatch()..start();
final notifications = <PushWakeNotification>[];
final failed = <String>{};
var first = true;
var interrupted = false;
var lastInterrupted = false;
var unreported = false;
do {
_rerun = false;
final PushWakeResult result;
try {
result = await _resolve(budget - elapsed.elapsed);
} on Object {
// A rerun that fails must not discard what earlier passes claimed,
// since those messages are already recorded as shown. The run is
// still incomplete: the rerun was for a message it never looked for,
// so the caller falls back to its generic notification when nothing
// else was found.
if (first) rethrow;
interrupted = true;
break;
}
first = false;
notifications.addAll(result.notifications);
// A message claimed but not returned stays lost whatever later passes
// find, so any pass reports it.
unreported = unreported || result.unreported;
// Only the last pass says what is still unread, because a community
// that failed once may have been read by the next, and a pass that ran
// out of time may have been followed by one that finished.
lastInterrupted = result.interrupted;
failed
..clear()
..addAll(result.failedCommunityIds);
} while (_rerun && budget - elapsed.elapsed >= minimumPass);
return PushWakeResult(
notifications: capPushWakeNotifications(notifications),
failedCommunityIds: failed.toList()..sort(),
// A wake that arrived during the last pass and got no rerun for lack of
// budget was never looked up, so the caller must fall back for it.
interrupted: interrupted || lastInterrupted || _rerun,
unreported: unreported,
);
}
}