Skip to content
Merged
Show file tree
Hide file tree
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 Nov 21, 2026
9352cb5
Address review of the presence fix: expiry index, full_state, tidy
James-Chapman Nov 21, 2026
7a229e1
Tell a full presence tracker apart from a window, and own its hash
James-Chapman Nov 21, 2026
96c6e13
Keep the long-poll cursor at since unless only own presence moved it
James-Chapman Nov 21, 2026
c3be0b5
File a presence account by its online devices only
James-Chapman Nov 21, 2026
d8aef69
Never keep a dropped presence change, and let a busy full tracker admit
James-Chapman Nov 21, 2026
5603755
Look a presence row up for change in place, and test the sliding rerun
James-Chapman Nov 21, 2026
65a01c2
Log a sync held online, and keep the full-tracker test off the clock
James-Chapman Nov 21, 2026
f320956
Apply a sliding sync's set_presence only once its pos is accepted
James-Chapman Nov 21, 2026
dab1b10
Apply sliding sync presence only for a current pos too
James-Chapman Nov 21, 2026
a594e5b
Return the presence index's position without unwrapping it
James-Chapman Nov 21, 2026
a8b70a2
Never evict a presence hold for a device asking for unavailable
James-Chapman Nov 21, 2026
c3342aa
State the presence tracker's fail-closed contracts
James-Chapman Nov 21, 2026
9f4ecd5
Say in ADR-0871 that a kept change still takes a place
James-Chapman Nov 21, 2026
fcaa6f4
Let a test fill the presence tracker at a small account cap
James-Chapman Nov 21, 2026
3b81d9e
Describe both presence refusals, and where the full tracker is filled
James-Chapman Nov 21, 2026
40163fe
Log a device_online drop only when it would have changed the stored s…
James-Chapman Nov 21, 2026
7ee56ce
Warn when a full presence tracker refuses changes, and test a no-op sync
James-Chapman Nov 21, 2026
0e11dc4
Name the full-tracker refusal wherever the presence 429 is described
James-Chapman Nov 21, 2026
bccc306
Wrap the user manual's presence paragraph
James-Chapman Nov 21, 2026
a6d93ce
Count a prune's re-filed accounts, and pin a refused /sync to no pres…
James-Chapman Nov 21, 2026
6b871e4
Report a full presence tracker's evictions, and share the full-table …
James-Chapman Nov 21, 2026
40e71f4
Never let a device asking for unavailable take an online device's place
James-Chapman Nov 21, 2026
07de778
Split the tracker's bound scenario into device-cap and account-cap ones
James-Chapman Nov 21, 2026
8009acb
Prove D0 keeps its place without syncing it again
James-Chapman Nov 21, 2026
0944213
Merge origin/main into fix/presence-flip-flood
James-Chapman Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ Changes before 1.0.0 are in [CHANGELOG-pre-release.md](CHANGELOG-pre-release.md)
so the worker stops serving the removed event (ADR-0862).
* A sliding sync `pos` is bound to the user and device it was issued to; any other is `M_UNKNOWN_POS`, so one user can
no longer continue another's connection (ADR-0866).
* An account's presence changes at most once every 5 s (a later sync's change is dropped, `PUT /presence` is 429), so a
flip loop no longer fills the federation queue; a prune visits only expired accounts (ADR-0875, ADR-0876).
* A full presence tracker's refused changes and evicted accounts are logged at warning level as
`presence.tracker.full`, at most once a minute with how many of each, so an operator sees it saturate (ADR-0876).

## Bug fixes

Expand Down Expand Up @@ -181,6 +185,14 @@ Changes before 1.0.0 are in [CHANGELOG-pre-release.md](CHANGELOG-pre-release.md)
* The Fedora 44 and 45 RPMs build again: GCC 16 refused `std::ranges::find` with a bare string literal under C++26,
and `reject-unsafe.sh` now rejects that form in code and tests (ADR-0870).
* The AlmaLinux and openSUSE CI suites install diffutils, which the integration shard check needs for `cmp` and `diff`.
* Two clients of one account, one syncing online and one `unavailable`, no longer flip its presence on every sync: an
online device holds the account online for 5 minutes (ADR-0875).
* A classic `/sync` whose only news is the caller's own presence keeps waiting instead of answering nothing (ADR-0873).
* A classic `/sync` with `full_state=true` is answered at once, whatever its `timeout`, as the spec says; it waited
like any other long poll (ADR-0873).
* A sliding sync refused with `M_UNKNOWN_POS` no longer changes the caller's presence: `set_presence` is applied
only once the `pos` is accepted (ADR-0876).
* `docs/observability-audit.md` named the wrong logger for `presence.sync.set`; it is `client_server`.
* The example configurations list every module a structured logger call names (twelve were missing, `token` is
gone), and a tooling test fails when that list and the modules the sources name differ (ADR-0872).
* The TURN log calls mark GCC's cleanup of their braced field lists excluded from coverage, a compiler path no test
Expand Down
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.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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 docs/adr/0875-a-device-syncing-online-holds-its-account-online.md
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.
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.
3 changes: 3 additions & 0 deletions docs/adr/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,7 @@
* [0870 - A ranges algorithm takes a typed value, never a bare string literal](0870-a-ranges-algorithm-takes-a-typed-value-never-a-bare-string-literal.md)
* [0871 - TURN credential requests log under their own `turn` module, and never log a credential](0871-turn-credential-requests-log-under-their-own-turn-module.md)
* [0872 - The example configs list every log module, and a tooling test reads the modules from the sources](0872-the-example-configs-list-every-log-module-and-a-tooling-test-reads-the-modules-from-the-sources.md)
* [0873 - A classic /sync whose only news is the caller's own presence keeps waiting](0873-a-classic-sync-whose-only-news-is-its-own-presence-keeps-waiting.md)
* [0874 - A braced log-field list may mark GCC's cleanup branches excluded from coverage](0874-a-braced-log-field-list-may-mark-gcc-s-cleanup-branches-excluded-from-coverage.md)
* [0875 - A device syncing online holds its account online against another device syncing unavailable](0875-a-device-syncing-online-holds-its-account-online.md)
* [0876 - The first presence change in a five-second window is kept, and every other one in it is dropped](0876-the-first-presence-change-in-a-five-second-window-is-kept.md)
4 changes: 3 additions & 1 deletion docs/database-persistence.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,9 @@ value. Test fixtures that do not exercise persistence select it through
(256) rows with a thread root as `thread_id` in one room; a new one is committed with the
delete of that user's own oldest (ADR-0737). The device-list change, stored filter and presence tables are read through
standing indexes by observer, user and user (`for_each_observer_device_list_change`, `for_each_user_filter`,
`find_presence_state`; ADR-0659). `store_receipts` commits an inbound `m.receipt`
`find_presence_state`; ADR-0659). A presence row is changed in place only through `find_mutable_presence_state`, which
answers from the same position lookup, and the change keeps the row's `user_id`, the key of `presence_user_index`.
`store_receipts` commits an inbound `m.receipt`
EDU's accepted rows in one transaction and, past `max_inbound_receipt_entries`, deletes the oldest
remote rows in it, taken from the ordered `receipt_remote_by_stream` index (ADR-0652).
- The v1 schema's media tables carry the upload names and pending uploads.
Expand Down
2 changes: 1 addition & 1 deletion docs/matrix-v1.19-client-server-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ comparison (ADR-0631); any other `from` is `400 M_INVALID_PARAM`.
| `GET` | `/_matrix/client/v3/presence/{userId}/status` | `getPresence` | access token | - | 200, 403, 404 |
| `PUT` | `/_matrix/client/v3/presence/{userId}/status` | `setPresence` | access token | required application/json | 200, 429 |

`GET` answers the caller's own presence, or another user's when they share a joined room (otherwise 403 `M_FORBIDDEN`, also for a user who does not exist); a local user with no presence is `offline`, a remote one is 404. `GET /sync` takes `set_presence` (`online` when omitted, `unavailable`, `offline`; another value is 400 `M_INVALID_PARAM`) and carries `presence.events` for users sharing a joined room. `presence.enabled=false` makes `GET` 404, `PUT` a 200 that stores nothing, and `/sync` presence-free. See `docs/user-manual.md` "Presence".
`GET` answers the caller's own presence, or another user's when they share a joined room (otherwise 403 `M_FORBIDDEN`, also for a user who does not exist); a local user with no presence is `offline`, a remote one is 404. `GET /sync` takes `set_presence` (`online` when omitted, `unavailable`, `offline`; another value is 400 `M_INVALID_PARAM`) and carries `presence.events` for users sharing a joined room. A device that synced `online` in the last 5 minutes holds the account online against another syncing `unavailable` (ADR-0875). An account's presence changes at most once in 5 s: a later `/sync` change in the window is dropped and a `PUT` is 429 `M_LIMIT_EXCEEDED` with `retry_after_ms` (ADR-0876). The same 429 (error `too many accounts are changing presence`) and dropped `/sync` change also come, for an account that has not changed at all, when every account in the server's presence tracker has a window open; `retry_after_ms` is then the soonest window's end, and the server logs `presence.tracker.full`. A `/sync` long-poll whose only news is the caller's own presence keeps waiting (ADR-0873). `presence.enabled=false` makes `GET` 404, `PUT` a 200 that stores nothing, and `/sync` presence-free. See `docs/user-manual.md` "Presence".

## Push notifications

Expand Down
Loading
Loading