Skip to content

SDK: Cache the RTC foci from the well-known file. #5167

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 6 commits into from
Jun 19, 2025
Merged

Conversation

pixlwave
Copy link
Member

@pixlwave pixlwave commented Jun 4, 2025

This PR makes the following changes:

  • Refactor ServerCapabilities into ServerInfo
    • It had nothing to do with /capabilities and was confusing.
  • Include the /.well-known/matrix/client response in the ServerInfo cache.
  • Add Client::rtc_foci which uses the cached well-known.
  • Add Client::is_livekit_rtc_supported to the FFI using the cache RTC foci.

Requires ruma/ruma#2091 before merging

@pixlwave pixlwave requested a review from a team as a code owner June 4, 2025 13:29
@pixlwave pixlwave requested review from andybalaam and removed request for a team June 4, 2025 13:29
@pixlwave pixlwave changed the title Doug/live kit rtc focus SDK: Cache the RTC foci from the well-known file. Jun 4, 2025
@pixlwave pixlwave force-pushed the doug/live-kit-rtc-focus branch from 2ffc5cb to c72c656 Compare June 4, 2025 14:10
Copy link

codecov bot commented Jun 4, 2025

Codecov Report

Attention: Patch coverage is 95.92593% with 11 lines in your changes missing coverage. Please review.

Project coverage is 90.18%. Comparing base (47c8df0) to head (e69b33f).
Report is 16 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk/src/client/mod.rs 96.31% 3 Missing and 4 partials ⚠️
...tes/matrix-sdk-base/src/store/integration_tests.rs 95.23% 0 Missing and 1 partial ⚠️
crates/matrix-sdk-base/src/store/traits.rs 96.55% 0 Missing and 1 partial ⚠️
crates/matrix-sdk-sqlite/src/state_store.rs 75.00% 0 Missing and 1 partial ⚠️
...matrix-sdk/src/client/builder/homeserver_config.rs 94.73% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #5167    +/-   ##
========================================
  Coverage   90.18%   90.18%            
========================================
  Files         334      334            
  Lines      104681   104845   +164     
  Branches   104681   104845   +164     
========================================
+ Hits        94402    94551   +149     
- Misses       6226     6239    +13     
- Partials     4053     4055     +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pixlwave pixlwave force-pushed the doug/live-kit-rtc-focus branch from c72c656 to e0f18e7 Compare June 4, 2025 14:38
@poljar poljar requested review from poljar and removed request for andybalaam June 5, 2025 09:04
@bmarty
Copy link
Contributor

bmarty commented Jun 5, 2025

I have got a crash testing this branch on Android:

org.matrix.rustcomponents.sdk.InternalException: called Option::unwrap() on a None value

@pixlwave
Copy link
Member Author

pixlwave commented Jun 5, 2025

org.matrix.rustcomponents.sdk.InternalException: called Option::unwrap() on a None value

Have you got a stack trace for this?

@bmarty
Copy link
Contributor

bmarty commented Jun 5, 2025

I have not a lot. Just this:

log: thread '' panicked at 'called Option::unwrap() on a None value': /Users/bmarty/workspaces/matrix-rust-sdk/crates/matrix-sdk/src/client/mod.rs:1869

@pixlwave
Copy link
Member Author

pixlwave commented Jun 5, 2025

Hmmm, well that line is unchanged in the PR and I'm not massively sure how this works.

// SAFETY: both fields were set above, so the function will always return some.
Ok(f(&guard).unwrap())

I set the new field I just added, maybe we can get some clarity in the review 🤔

@bnjbvr
Copy link
Member

bnjbvr commented Jun 5, 2025

Hmmm, well that line is unchanged in the PR and I'm not massively sure how this works.

// SAFETY: both fields were set above, so the function will always return some.
Ok(f(&guard).unwrap())

I set the new field I just added, maybe we can get some clarity in the review 🤔

Let's break out the statement: f(&guard).unwrap():

  • &guard gives you information that's supposed to be accurate now
  • f(&guard) will call the closure parameter on it, that returns an option. In the new case you've added, it's this function:
server_info
                .well_known
                .as_ref()
                .and_then(|well_known| well_known.rtc_foci.clone().into())
  • if this callback function returns None, then this .unwrap() will fail.

I suppose that if the .well-known is None (or if rtc_foci can be None?), then this will return None, and cause the unwrap to fail.

@pixlwave
Copy link
Member Author

pixlwave commented Jun 5, 2025

Ohhhh, I thought this line was about re-locking access to the inner.caches 😂🤦‍♂️

I see it clear as day now. Will rename some stuff.

Thanks Benji 🙏

@pixlwave
Copy link
Member Author

pixlwave commented Jun 5, 2025

Ok, I've fixed it in 5476b5c with a double optional but then I decided that the assumption to unwrap the result of that closure is very fragile, so I added a CachedValue wrapper to make it compile safe in 80435fe.

I'm less sure about how idiomatic the latter commit is - in Swift I would use a KeyPath parameter instead of the problematic closure, but I couldn't find anything like that for Rust. Hence I kept it separate and am happy to revert it or squash it based on feedback :)

@bmarty
Copy link
Contributor

bmarty commented Jun 5, 2025

With the latest commits, it's not crashing anymore, but isLivekitRtcSupported() is returning false when testing on a matrix.org account. I believe that the file https://matrix.org/.well-known/matrix/client has the expected field that should make the method return true. Am I missing something?

@pixlwave
Copy link
Member Author

pixlwave commented Jun 6, 2025

Ah, this is presumably because the old data is still cached without a well-known. I've just seen the same thing. Does it work if you call Client.resetServerInfo() once?

@pixlwave pixlwave force-pushed the doug/live-kit-rtc-focus branch from 80435fe to d7f2550 Compare June 6, 2025 11:05
@bmarty
Copy link
Contributor

bmarty commented Jun 6, 2025

Does it work if you call Client.resetServerInfo() once?

It did not fix the issue, but I'll build the SDK again with the latest commits

@bmarty
Copy link
Contributor

bmarty commented Jun 6, 2025

Tested with d7f2550 and calling resetServerInfo() before isLivekitRtcSupported() and isLivekitRtcSupported() still returns false for a matrix.org account.

@pixlwave
Copy link
Member Author

pixlwave commented Jun 6, 2025

Huh, it's working great for me both on matrix.org and my test server which doesn't have any RTC foci 🤔

@pixlwave
Copy link
Member Author

pixlwave commented Jun 6, 2025

Do you want to try doug/rtc-focus-logs and filter your logs for [BENOIT] and report back what you see?

@bmarty
Copy link
Contributor

bmarty commented Jun 11, 2025

Hello @pixlwave , here are the logs:

2025-06-11 12:24:06.272 32271-32533 org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
2025-06-11 12:24:14.006 32271-32533 org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
2025-06-11 12:24:15.903 32271-32533 org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
---------------------------- PROCESS STARTED (4802) for package io.element.android.x.debug ----------------------------
2025-06-11 12:26:27.133  4802-4927  org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
2025-06-11 12:26:28.148  4802-5173  org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
2025-06-11 12:26:28.743  4802-5081  org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk::client: [BENOIT] Fetching client well-known from https://matrix-client.matrix.org/ | crates/matrix-sdk/src/client/mod.rs:1773

And it's working fine now, so I am not sure what happened previously.

Copy link
Contributor

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Working fine on Element X Android on a matrix.org account.

@poljar
Copy link
Contributor

poljar commented Jun 11, 2025

Can this be reviewed now? Could you point to the correct Ruma branch or release? This will get rid of the CI issue.

@bmarty
Copy link
Contributor

bmarty commented Jun 11, 2025

ruma/ruma#2091 is not available in the latest Ruma release 0.12.3, so I guess we need to wait for the next release?

@poljar
Copy link
Contributor

poljar commented Jun 11, 2025

ruma/ruma#2091 is not available in the latest Ruma release 0.12.3, so I guess we need to wait for the next release?

It's part of the stable branch, so we can depend on that ruma/ruma@d1d53e2.

@bnjbvr
Copy link
Member

bnjbvr commented Jun 11, 2025

Hello @pixlwave , here are the logs:

2025-06-11 12:24:06.272 32271-32533 org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
2025-06-11 12:24:14.006 32271-32533 org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
2025-06-11 12:24:15.903 32271-32533 org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
---------------------------- PROCESS STARTED (4802) for package io.element.android.x.debug ----------------------------
2025-06-11 12:26:27.133  4802-4927  org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
2025-06-11 12:26:28.148  4802-5173  org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk_ffi::client: [BENOIT] RTC foci: [LiveKit(LiveKitRtcFocusInfo { service_url: "https://livekit-jwt.call.matrix.org" })] | bindings/matrix-sdk-ffi/src/client.rs:1498
2025-06-11 12:26:28.743  4802-5081  org.matrix.rust.sdk     io.element.android.x.debug           E  matrix_sdk::client: [BENOIT] Fetching client well-known from https://matrix-client.matrix.org/ | crates/matrix-sdk/src/client/mod.rs:1773

And it's working fine now, so I am not sure what happened previously.

Note the server-side data is cached for some time, and must be re-fetched after some time. So if you happened to have a cached entry, with the deserialization defaulting to filling the new info as empty if it's not been found during deserialization, then the app could conclude that the info was missing, while it's just outdated.

@bmarty
Copy link
Contributor

bmarty commented Jun 12, 2025

@bnjbvr thanks, but I was forcing a refresh by invoking reset_server_info() 🤷‍♂️

@bmarty
Copy link
Contributor

bmarty commented Jun 16, 2025

I have added a commit, please let me know if it's correct (I am not 💯 sure) and if we need anything else to be able to merge this PR. Thanks!

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

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

Not yet quite right. As the CI also complains about.

Cargo.toml Outdated
@@ -78,7 +78,7 @@ ruma = { git = "https://github.com/pixlwave/Ruma", branch = "doug/sdk+patches",
"unstable-msc4278",
"unstable-msc4286",
] }
ruma-common = { git = "https://github.com/pixlwave/Ruma", branch = "doug/sdk+patches" }
ruma-common = "0.15.2"
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to depend on the same Ruma version for all the Ruma crates, otherwise Rust will complain about types not having the same version.

@pixlwave pixlwave force-pushed the doug/live-kit-rtc-focus branch from 07dfec2 to dbf2014 Compare June 19, 2025 10:01
@pixlwave
Copy link
Member Author

I've rebased on main and squashed Benoit's change into the history so it doesn't get merged with my fork in the history.

@bmarty: I think the issue was running cargo update - I reverted the lock file and let RustRover load the project and the resulting lock file changes were much smaller 🤷‍♂️

@pixlwave pixlwave requested a review from poljar June 19, 2025 10:28
@poljar
Copy link
Contributor

poljar commented Jun 19, 2025

Thanks, sorry for the delay we had a bit of trouble with our CI setup. I'm going to review this now.

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

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

Ok, I think that this is fine logic-wise. The changelog entries need some fixing though.

Comment on lines 55 to 57
- The cached `ServerCapabilities` has been renamed to `ServerInfo` and additionally contains
the well-known response alongside the existing server versions. Despite the old name, it
does not contain the server capabilities (yet?!).
Copy link
Contributor

Choose a reason for hiding this comment

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

This is now in the completely wrong place, it also lacks a link to the PR. Finally, the "yet?!", feels a bit tacky for a changelog entry.

Comment on lines 81 to 83
- `ClientServerCapabilities` has been renamed to `ClientServerInfo`. Alongside this,
`Client::reset_server_info` is now `Client::reset_server_info` and `Client::fetch_server_capabilities`
is now `Client::fetch_server_versions`, returning the server versions response directly.
Copy link
Contributor

Choose a reason for hiding this comment

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

This one's in the wrong place as well, please add new entries to the top, so this would have been a merge conflict if it was at the top.

PR link is also missing.

@@ -22,6 +22,7 @@ Breaking changes:
- `RoomInfo` replaces its field `is_tombstoned: bool` with `tombstone: Option<RoomTombstoneInfo>`,
containing the data needed to implement the room migration UI, a message and the replacement room id.
([#5027](https://github.com/matrix-org/matrix-rust-sdk/pull/5027))
- `Client::reset_server_capabilities` has been renamed to `Client::reset_server_info`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong place, PR link please.

@pixlwave
Copy link
Member Author

Changelogs updated (TIL you put them in reverse chronological order).

@pixlwave pixlwave requested a review from poljar June 19, 2025 12:02
Comment on lines +13 to +29
- `Client::reset_server_capabilities` has been renamed to `Client::reset_server_info`.
([#5167](https://github.com/matrix-org/matrix-rust-sdk/pull/5167))
Copy link
Contributor

Choose a reason for hiding this comment

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

Well that's still in the wrong place, this PR is not part of the 0.12.0 release from two weeks ago.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ohhhh sorry I see what you're saying, I conflated the ordering comment with the "wrong place" comments… 🙈

Comment on lines 40 to 43
- The cached `ServerCapabilities` has been renamed to `ServerInfo` and
additionally contains the well-known response alongside the existing server versions.
Despite the old name, it does not contain the server capabilities.
([#5167](https://github.com/matrix-org/matrix-rust-sdk/pull/5167))
Copy link
Contributor

Choose a reason for hiding this comment

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

This one is under the 0.11.0 release, which is happened two months ago.

Comment on lines 68 to 71
- `ClientServerCapabilities` has been renamed to `ClientServerInfo`. Alongside this,
`Client::reset_server_info` is now `Client::reset_server_info` and `Client::fetch_server_capabilities`
is now `Client::fetch_server_versions`, returning the server versions response directly.
([#5167](https://github.com/matrix-org/matrix-rust-sdk/pull/5167))
Copy link
Contributor

Choose a reason for hiding this comment

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

This one is under the 0.12.0 release.

@pixlwave pixlwave force-pushed the doug/live-kit-rtc-focus branch from a93a542 to 0df12a7 Compare June 19, 2025 15:23
@pixlwave pixlwave force-pushed the doug/live-kit-rtc-focus branch from 0df12a7 to e69b33f Compare June 19, 2025 15:24
@pixlwave
Copy link
Member Author

Alright, moved 👍

Out of interest, are there any plans to adopt Towncrier for these changelogs? It would be an awful lot easier for contributors (especially as we use them elsewhere) 🙂

@pixlwave pixlwave requested a review from poljar June 19, 2025 15:26
@poljar
Copy link
Contributor

poljar commented Jun 19, 2025

Out of interest, are there any plans to adopt Towncrier for these changelogs? It would be an awful lot easier for contributors (especially as we use them elsewhere) 🙂

No, we had plans to use git-cliff but ultimately it was too complicated of a setup.

I think towncrier would reintroduce some of the downsides git-cliff had.

@poljar poljar merged commit 06b3871 into main Jun 19, 2025
45 checks passed
@poljar poljar deleted the doug/live-kit-rtc-focus branch June 19, 2025 15:40
@pixlwave
Copy link
Member Author

pixlwave commented Jun 19, 2025

No, we had plans to use git-cliff but ultimately it was too complicated of a setup.

I think towncrier would reintroduce some of the downsides git-cliff had.

What were the downsides of git-cliff? Towncrier doesn't use commits, just changelog entries as a text file so it mimicks what we're doing right now updating the CHANGELOG.md, but eradicates conflicts, accidentally rebasing entries into the wrong version and semi-automates the PR link generation (from the file number). It would be a very nice improvement to the workflow :)

We could quite easily set up a changelog.d directory per crate and then tweak the existing matrix-org style guide for contributors to match the multi-crate structure.

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.

4 participants