Skip to content

fix(ancs): process replies as they arrive, fetch the newest first - #339

Merged
zackb merged 1 commit into
zackb:mainfrom
fernando2393:pr/ancs-blip-resilience
Nov 23, 2026
Merged

zackb merged 1 commit into
zackb:mainfrom
fernando2393:pr/ancs-blip-resilience

Conversation

@fernando2393

@fernando2393 fernando2393 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem (iPhone, iOS 26, BlueZ 5.85)

When a notification session starts, iOS replays all of Notification Center (about 170 entries here) and the client fetches each one:

  1. The fetch ran at the supervisor's tick. Replies sat in the inbox until the next one-second tick, which also sent the next request. Measured with timestamps on the request write and on reply arrival: the phone answers in a median of 180 ms, then the daemon waited 1000 ms.
  2. A replay is bigger than the queue. The replay arrives in one burst, so once 32 requests are queued the remaining pre-existing entries are skipped. They arrive oldest first, so the newest are the ones skipped.

Change

  • Drain on arrival. When a Data Source reply, a Notification Source event or a desktop action reaches the ANCS inbox, the D-Bus thread wakes the supervisor. Until the next full tick, the supervisor then runs only the client's drain (AncsClient::pump), still on the tick thread. The wait between a reply and the next request went from 1000 ms to 0: 33 fetches took 6 s instead of 33 s.
  • Newest first. Additions that arrive together are fetched by descending UID (fetch_order), so the queued 32 are the newest.
  • BLUETOOTH.md: two troubleshooting rows for a flapping LE link (a combo Wi-Fi/Bluetooth card with no antenna; stale per-device connection parameters).

Testing

  • 3 new unit tests for fetch_order; the whole suite passes (650).
  • By hand with an iPhone: replays as above; reminders and messages pop up within seconds, locked and unlocked.
  • Not unit-tested: the supervisor wake (it needs BlueZ); happy to add a seam if you want one.

🤖 Generated with Claude Code

@zackb zackb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for digging into this, and for the antenna and connection parameter stuff. That must have taken you some real time and will save someone else (probably me :)) theirs.

Before going further, I want to make sure we agree on what's actually slow.

  1. Where does the "about a second each" come from? In your logs, is the phone taking ~1s to answer each GetNotificationAttributes, or is the daemon waiting? My suspicion is the AncsClient::tick only running once per second. Replies get buffered and only drained on the next tick, and that's also when the next request goes out. btmon trace would show for sure. If the phone responds in a ms or few, the replay is slow because of our loop, and draining on arrival would turn ~170s into a few seconds. Most of the reordering wouldn't be needed then.
  2. Did you see the drop at client.cpp:471? Once 32 requests are queued, further pre-existing events are dropped without being remembered. The chron ordering means the newest entries never get fetched. Is that the "a session that ended early never reached it" you saw, or were sessions really ending mid replay?
  3. "Some old ones come back unflagged": do you have an example (app, Date attribute, flags)? I've had reports of this from others but have never been able to repro it myself. I'd like to see what you hit before changing live behaviour.
  4. The front-of-queu eviction: when the queue is full, submit(..., front = true) drops queue_.back(). That entry could be a user's libnotify reply/dismiss action or an app-name lookup, not only backlog. The dropped UID also stays in in_progress and the registry's seen_, so it won't be fetched again this session. Was that intended?
  5. "UIDs only grow" is only true in a single session, UIDs restart on each connection (see AI notes in BLUETOOTH.md).

Also, the build.log and build-cfg.log files should not be in source control.

If 1 turns out to be the bottleneck, I think the "drain on arrival" change is the way.
We could then see what's left of 2-5 with the replay running at full speed, and keep the fetch_order.

The ANCS client only ran on the supervisor's one-second tick, so each reply
from the phone waited for the next tick before it was processed and the next
request went out. Measured on an iPhone: the phone answers a
GetNotificationAttributes in about 180 ms, then the daemon waited 1000 ms. A
replay of Notification Center (about 170 entries here) took minutes.

The D-Bus thread now wakes the supervisor when something lands in the ANCS
inbox (a Data Source reply, a Notification Source event or a desktop action),
and the supervisor runs only the ANCS drain until the next full tick. The
daemon's wait between a reply and the next request went from 1000 ms to 0.

A replay also brings more entries than the client queues, and the ones beyond
the limit are never fetched. Within a session UIDs grow, so additions that
arrive together are now fetched newest first (fetch_order, with unit tests).

Also adds two BLUETOOTH.md troubleshooting rows for causes of a flapping LE
link that are easy to miss: a Wi-Fi/Bluetooth combo card with no antenna, and
stale per-device connection parameters.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fernando2393
fernando2393 force-pushed the pr/ancs-blip-resilience branch from dfe5282 to b21fb26 Compare November 23, 2026 09:28
@fernando2393 fernando2393 changed the title fix(ancs): keep notifications flowing across LE link blips fix(ancs): process replies as they arrive, fetch the newest first Nov 23, 2026
@fernando2393

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review, you were right on the main point. I've reworked the PR around it and force-pushed.

  1. Tick. Confirmed by measurement: timestamps on the control point write and on Data Source arrival show the phone answering in a median of 180 ms (p90 225 ms), then 1000 ms of daemon wait before the next request. With drain on arrival, that wait is 0. At ~180 ms per entry a full 170-entry replay is ~30 s rather than a few seconds, but it's about 6× faster.
  2. Drop at :471. Yes, that's what I was seeing. The whole replay arrives in one burst (all ~170 Notification Source events within a millisecond), so even at full speed the queue fills and the rest are skipped. I can't show that sessions really ended mid-replay, so I dropped that claim. Kept fetch_order so the 32 queued are the newest.
  3. Unflagged old entries. From last night's log: across 14 full replays (167–169 entries each), the same 36 entries arrived without PreExisting every time. Flags were mostly 0x10 or 0x30, categories 0, 8, 9, 10 and 5. I didn't log app ID or Date then. Since this morning I do, and so far I have one: com.burbn.instagram, flags 0x10, category 0, Date 20261123T094814, replayed 31 minutes after delivery. The phone's Notification Center has since been cleared, so I'll post more as they appear. I took the behaviour change out of this PR until we agree on it.
  4. Eviction. Not intended, and you're right that it could drop a user's action. Removed.
  5. UIDs. Right, only within a session. Fixed.

Sorry about the build logs. They're gone.

@zackb

zackb commented Nov 23, 2026

Copy link
Copy Markdown
Owner

"the same 36 entries arrived without PreExisting every time" this is huge. and explains a lot of issues users are having with replay!

@zackb
zackb merged commit bcf7e9b into zackb:main Nov 23, 2026
7 checks passed
@fernando2393
fernando2393 deleted the pr/ancs-blip-resilience branch November 23, 2026 21:52
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.

2 participants