Repository navigation
Stop two devices flipping presence and flooding federation (ADR-0871 to ADR-0873) #612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
5e53f9e
Stop two devices flipping presence and flooding federation (ADR-0871 …
James-Chapman 9352cb5
Address review of the presence fix: expiry index, full_state, tidy
James-Chapman 7a229e1
Tell a full presence tracker apart from a window, and own its hash
James-Chapman 96c6e13
Keep the long-poll cursor at since unless only own presence moved it
James-Chapman c3be0b5
File a presence account by its online devices only
James-Chapman d8aef69
Never keep a dropped presence change, and let a busy full tracker admit
James-Chapman 5603755
Look a presence row up for change in place, and test the sliding rerun
James-Chapman 65a01c2
Log a sync held online, and keep the full-tracker test off the clock
James-Chapman f320956
Apply a sliding sync's set_presence only once its pos is accepted
James-Chapman dab1b10
Apply sliding sync presence only for a current pos too
James-Chapman a594e5b
Return the presence index's position without unwrapping it
James-Chapman a8b70a2
Never evict a presence hold for a device asking for unavailable
James-Chapman c3342aa
State the presence tracker's fail-closed contracts
James-Chapman 9f4ecd5
Say in ADR-0871 that a kept change still takes a place
James-Chapman fcaa6f4
Let a test fill the presence tracker at a small account cap
James-Chapman 3b81d9e
Describe both presence refusals, and where the full tracker is filled
James-Chapman 40163fe
Log a device_online drop only when it would have changed the stored s…
James-Chapman 7ee56ce
Warn when a full presence tracker refuses changes, and test a no-op sync
James-Chapman 0e11dc4
Name the full-tracker refusal wherever the presence 429 is described
James-Chapman bccc306
Wrap the user manual's presence paragraph
James-Chapman a6d93ce
Count a prune's re-filed accounts, and pin a refused /sync to no pres…
James-Chapman 6b871e4
Report a full presence tracker's evictions, and share the full-table …
James-Chapman 40e71f4
Never let a device asking for unavailable take an online device's place
James-Chapman 07de778
Split the tracker's bound scenario into device-cap and account-cap ones
James-Chapman 8009acb
Prove D0 keeps its place without syncing it again
James-Chapman 0944213
Merge origin/main into fix/presence-flip-flood
James-Chapman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
docs/adr/0873-a-classic-sync-whose-only-news-is-its-own-presence-keeps-waiting.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # A classic /sync whose only news is the caller's own presence keeps waiting | ||
|
|
||
| * Status: accepted | ||
| * Date: 2026-11-21 | ||
|
|
||
| Technical Story: the presence loop of ADR-0875. A classic `/sync` long-poll returned at once whenever the sync stream | ||
| moved past its `since`, even when the only new row was the caller's own presence, which `/sync` never sends to its own | ||
| user. Each presence change of an account answered every waiting sync of that account with nothing, and each client | ||
| synced again at once. | ||
|
|
||
| ## Context and Problem Statement | ||
|
|
||
| The long-poll check of `GET /sync` is "has the sync stream or the timeline moved past `since`?". Sliding sync already | ||
| waits past bumps its response would not send (ADR-0863, ADR-0869). Classic `/sync` did not, so a change of the caller's | ||
| own presence, including the one the same request's `set_presence` just made, woke it into an empty answer. | ||
|
|
||
| ## Decision Drivers | ||
|
|
||
| * ADR-0863's rule: never wake a poll with a test the response does not share, or it is answered with nothing, again. | ||
| * The check runs under `runtime.mutex` on every rerun, so it must cost a constant. | ||
|
|
||
| ## Considered Options | ||
|
|
||
| 1. When a `/sync` with a `since` token would answer and its only news is exactly one sync stream position, the | ||
| caller's own presence row (`only_own_presence_since`), answer `needs_wait` with the wait cursor advanced to the | ||
| stream's newest position. The response, when it comes, is built from the request's own `since`, as before. | ||
| 2. Judge every position after `since` (a walk of each sync surface). | ||
| 3. Send the caller their own presence, so the answer is not empty. | ||
|
|
||
| ## Decision Outcome | ||
|
|
||
| Chosen option: 1, because it is a constant-time check and covers the case that loops: one device's change waking the | ||
| account's other devices, and a request's own `set_presence` waking itself. | ||
|
|
||
| * With any other position after `since`, or several, the sync answers as before; the check never delays news. | ||
| * A sync with no `since` (an initial sync) is not affected. | ||
| * A sync with `full_state=true` never waits, this check or any other: the spec says that with it "the `timeout` | ||
| parameter will be ignored and the query will return immediately". The long-poll gate waited for it before this | ||
| decision. | ||
| * The rerun of a woken poll judges again from the same `since`: a new position that is not the caller's presence ends | ||
| the wait. | ||
|
|
||
| ### Positive Consequences | ||
|
|
||
| * A client's own presence change no longer costs it, or its other devices, an empty round trip. | ||
|
|
||
| ### Negative Consequences | ||
|
|
||
| * Two of the caller's own presence changes in a row (at least 5 s apart, ADR-0876) answer the second with nothing, as | ||
| before this decision. | ||
| * Option 3 changes what clients are sent; the spec's presence events are for other users. | ||
72 changes: 72 additions & 0 deletions
72
docs/adr/0875-a-device-syncing-online-holds-its-account-online.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # A device syncing online holds its account online against another device syncing unavailable | ||
|
|
||
| * Status: accepted | ||
| * Date: 2026-11-21 | ||
|
|
||
| Technical Story: the owner's server logged `presence.outbound.refused` for every remote server, many times a second. | ||
| Two devices of one account were syncing at once, one with `set_presence` omitted (online) and one with | ||
| `set_presence=unavailable`. Each sync set the account's presence its own way, each change woke the other device's | ||
| long-poll, and the two flipped it every 50 to 250 ms. Each flip queued an `m.presence` EDU for each of about 470 | ||
| servers, which filled the outbound dispatch queue and so refused every other federation send. | ||
|
|
||
| ## Context and Problem Statement | ||
|
|
||
| A presence row is one per account, but `set_presence` is sent per sync, and so per device. The spec says "Any activity | ||
| that bumps the last active time on any of the user's clients will cause the server to automatically set their presence | ||
| to `online`", so a client in the background asking for `unavailable` should not take the account down while another | ||
| client is in use. How should the asks of several devices become one presence? | ||
|
|
||
| ## Decision Drivers | ||
|
|
||
| * The account must not flip with each device's sync. | ||
| * A device that stops syncing must not hold the account online for ever. | ||
| * Memory stays bounded whatever the number of accounts and devices. | ||
|
|
||
| ## Considered Options | ||
|
|
||
| 1. Note each device's last ask in memory (`AccountPresenceTracker`). A device that asked for `online` holds the account | ||
| online for `device_fresh_ms` (5 minutes, the idle timeout of ADR-0492). A sync asking for `unavailable` changes | ||
| nothing while another device holds the account online, and never raises the account to online. | ||
| 2. Keep one ask per account, the latest, as before. | ||
| 3. Store the devices' asks in the database. | ||
|
|
||
| ## Decision Outcome | ||
|
|
||
| Chosen option: 1, because it matches the spec's "any of the user's clients" and ends the loop at its cause. | ||
|
|
||
| * `set_presence=offline` forgets the device's ask: it no longer holds the account online. | ||
| * The freshness time is the idle timeout, so a closed client holds the account online exactly as long as the idle | ||
| sweep would leave it online after that client's last sync; a `static_assert` in `PresenceService.cpp` keeps the | ||
| two equal. | ||
| * At most `max_devices_per_account` (64) devices per account. At the cap a new device takes the place of the least | ||
| recently seen device that asked for `unavailable`; only a device asking for `online` takes the place of an online | ||
| one (the least recently seen), and a device asking for `unavailable` with no such place is not recorded, since it | ||
| holds nothing: replacing by age alone let a 65th device asking for `unavailable` end the only online hold, which is | ||
| the flip this decision prevents. At most `max_accounts` | ||
| (65,536) accounts, pruned by the idle sweep every 30 s and when the table is full. An account with an open window | ||
| (ADR-0876) is filed under its end, any other under the time its latest online device goes stale (a device that last | ||
| asked for `unavailable` holds no place), so a prune visits only expired accounts, never the table: a flood of | ||
| overflow requests under `runtime.mutex` costs no scan. With the table still full, the account with no open window | ||
| whose devices go stale soonest is evicted to make room: it loses its devices' holds until it syncs again, never a | ||
| window. Only when every account has an open window does a sync of an account the table does not hold have its device | ||
| ignored and its change refused (ADR-0876), at most until the soonest window ends. | ||
| * The asks are in memory only: after a restart each device's next sync puts its ask back. | ||
| * The clock is the steady clock (`presence_limit_clock_ms`), which never goes back. The idle sweep measures activity on | ||
| the wall clock (`observed_at_ms`), so after a wall-clock step the hold and the sweep disagree by the step until the | ||
| device syncs again: after a backward step a background device's `unavailable` can win while the sweep still counts the | ||
| user active, and after a forward step the sweep can set the account unavailable while a hold stands. Accepted: either | ||
| is one change, limited by ADR-0876, never a loop. Judging the hold on the wall clock instead would let one clock step | ||
| lift or extend every hold at once, and the `static_assert` keeps only the two durations equal. | ||
| * An account the table does not hold takes no place for a device asking for `unavailable`: the ask alone never evicts | ||
| another account's hold. If that sync changes the stored state, the change still opens a window (ADR-0876) and may | ||
| evict a closed account to make room, as any kept change does. | ||
|
|
||
| ### Positive Consequences | ||
|
|
||
| * Two clients of one account no longer set its presence back and forth. | ||
|
|
||
| ### Negative Consequences | ||
|
|
||
| * After every online client closes, a background client asking for `unavailable` sets the account unavailable only | ||
| once the freshness time has passed, up to 5 minutes later (the idle sweep sets it unavailable at the same moment). | ||
| * Option 2 is the simpler model, and looks better in isolation; it is the one that made the loop. |
67 changes: 67 additions & 0 deletions
67
docs/adr/0876-the-first-presence-change-in-a-five-second-window-is-kept.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # The first presence change in a five-second window is kept, and every other one in it is dropped | ||
|
|
||
| * Status: accepted | ||
| * Date: 2026-11-21 | ||
|
|
||
| Technical Story: the presence loop of ADR-0875. Even with the devices combined, anything that changes an account's | ||
| presence many times a second (a buggy client, two clients fighting through `PUT /presence`) queues an EDU for every | ||
| server in the account's rooms each time. | ||
|
|
||
| ## Context and Problem Statement | ||
|
|
||
| Every change of a local user's presence is stored, wakes the syncs of everyone who shares a room with them and queues | ||
| one `m.presence` EDU per remote server in those rooms, each in its own transaction. How often may one account cause | ||
| that? | ||
|
|
||
| ## Decision Drivers | ||
|
|
||
| * One account must not be able to fill the federation dispatch queue. | ||
| * The owner's rule: the first change in a window goes through and the rest are dropped. Keeping the last change for | ||
| later ("coalescing") is the same rate limit with a different change kept, and the last change is as likely to be the | ||
| wrong one as the first. | ||
| * `PUT /presence/{userId}/status` has only 200 and 429 in the spec. | ||
|
|
||
| ## Considered Options | ||
|
|
||
| 1. A window of `change_window_ms` (5 s) per account, started by a change kept. A change asked for inside it is dropped | ||
| (a sync: nothing stored, woken or sent) or refused (`PUT`: 429 `M_LIMIT_EXCEEDED` with `retry_after_ms` and | ||
| `Retry-After`). Judged and noted under `runtime.mutex`, with the write. | ||
| 2. Coalesce: hold the last change asked for in the window and send it when the window ends. | ||
| 3. Limit only the outbound EDUs, and store every change. | ||
|
|
||
| ## Decision Outcome | ||
|
|
||
| Chosen option: 1. | ||
|
|
||
| * The window is per account, not per device, since the row and its EDUs are per account. | ||
| * A change is a change of the stored state by a sync, or any `PUT`. A sync that leaves the state as it is refreshes the | ||
| last-active time in memory as before and is not limited. | ||
| * Not limited: the idle sweep (the server's own change, at most once per idle period; a user who returns at once is | ||
| marked online at once), the profile propagation that resends an unchanged presence with a new display name or avatar | ||
| (it stores the row for local syncs and appservices and sends no `m.presence` EDU), and inbound presence from other | ||
| servers (their own server's to limit). | ||
| * An account with an open window is never removed from `AccountPresenceTracker`, or removing it would lift its limit. | ||
| A full table makes room by evicting an account with no open window (ADR-0875), so accounts that keep syncing never | ||
| shut others out. Only when every account in it has an open window is a change of an account it does not hold refused | ||
| (fails closed, as `UserCodeAttemptThrottle` does), so a flood of accounts cannot lift the limit for itself. The | ||
| refusal's `retry_after_ms` is the rest of the soonest window (at most `change_window_ms`), and its log line's `cause` | ||
| is `tracker_full`, not `window`. Such a refusal, and every eviction a full table makes (ADR-0875), counts towards | ||
| `presence.tracker.full`, a warning logged at most once a minute (`full_warning_interval_ms`) with the number of | ||
| each since the last one and no user IDs. A user who did nothing wrong is refused only here, and an evicted account | ||
| loses its devices' holds, so an operator must see both without debug logging; one warning per refusal or eviction | ||
| would let the flood that fills the table flood the log too. The report is asked for after each use of the table | ||
| (`take_saturation_report`, O(1)), so what a quiet minute leaves is reported by the next sync or `PUT`. | ||
| * A dropped change is never kept later: a parked `/sync` or sliding sync is run again after a wake-up or its timeout | ||
| (`rerun_waiting_sync`, `ClientServerDispatchOptions::sync_rerun`), and that rerun does not apply its `set_presence` | ||
| again, so a change dropped in the window is not stored once the window has ended. | ||
| * Tests move the clock with `HomeserverRuntime::test_presence_clock_advance_ms`. | ||
|
|
||
| ### Positive Consequences | ||
|
|
||
| * One account causes at most one presence broadcast every 5 seconds. | ||
|
|
||
| ### Negative Consequences | ||
|
|
||
| * A change asked for in the window is lost, not deferred: a client that set `unavailable` a second after a sync set it | ||
| `online` is refused, and its sync's next change is what counts. | ||
| * A client that does not retry a 429 on `PUT /presence` keeps whatever was set first. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.