Skip to content

Keep the region list fresh while the signal session runs - #1710

Open
lukasIO wants to merge 3 commits into
lukas/region-server-reportedfrom
lukas/region-refresh
Open

lukasIO wants to merge 3 commits into
lukas/region-server-reportedfrom
lukas/region-refresh

Conversation

@lukasIO

@lukasIO lukasIO commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

While a room is connected, the JS SDK refetches the region settings each time their max-age runs out, with the latest token. The Rust region cache was filled only on demand. The first failover after a long call therefore found an expired list and fetched it first.

Change

  • signal_task drives RegionUrlProvider::keep_fresh as one branch of its select! loop. There is no spawned task, and the refresh ends with the signal task.
  • keep_fresh refetches the list of the project URL when it goes stale. That is the list a full reconnect reads. It waits at least the default TTL (5 s), which also paces retries after a failed fetch. It reads the session token before each fetch, so a refreshed token is used. It stops on a 401, and does nothing for a URL outside LiveKit Cloud.
  • It runs only while region_failover is on.
  • livekit-region adds RegionCache::fresh_for, which tells keep_fresh how long to wait.

Unlike JS, the refresh stops when the session closes. JS keeps it for 30 s after a disconnect.

Verified

  • cargo test -p livekit-signaling -p livekit-region: 65 and 7 tests pass.
  • keep_fresh_refetches_a_stale_list (paused clock): no refetch before 5 s, one refetch after. The cache ages on std::time::Instant, which a paused clock does not move, so the test starts from a stale entry.
  • keep_fresh_stops_on_a_401: the refresh returns after a 401.
  • the_signal_task_refreshes_the_region_list: the signal task refetches with the session token. It fails when the refresh is removed from the loop.
  • region_cache_reports_how_long_an_entry_stays_fresh covers fresh_for.

🤖 Generated with Claude Code

The JS SDK refetches the region settings every max-age while a room is
connected. The Rust region cache was only filled on demand, so the first
failover after a long call paid for a fetch.

The signal task now drives RegionUrlProvider::keep_fresh as one branch of
its select loop: no spawned task, and it ends with the session. It refetches
when the entry goes stale, at most every default TTL, reads the token before
each fetch so a refreshed token is used, and stops on a 401.
RegionCache::fresh_for tells it how long to wait.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

4 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

// branch below only drives it.
let region_refresh = async {
if inner.options.region_failover {
RegionUrlProvider::keep_fresh(&inner.project_url, || inner.token.lock().clone()).await;

@devin-ai-integration devin-ai-integration Bot Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Wrong cache host after failover

After a room joins a different region host, keep_fresh refreshes only the original project host. Full reconnect instead uses the joined host, so another failed dial waits for region discovery if that host has no fresh entry.

Learn more

The signaling region cache indexes its lists by hostname in fetch_region_urls. On a fallback join, SignalClient::connect passes both the selected region URL and the original project URL to SignalInner::connect. The refresh uses the original project URL, but reconnect_task reads SignalClient::url, which returns the selected region URL. try_restart_connection passes that URL into the replacement session. Its connection attempt starts discovery in parallel, but fallback still awaits discovery if the dial fails before the fetch finishes.

Example: A room joins us.project.livekit.cloud after its initial dial to project.livekit.cloud fails. During the call, refresh keeps project.livekit.cloud warm. A later full reconnect whose first dial to us.project.livekit.cloud fails waits for discovery under us.project.livekit.cloud rather than using the warmed list.

Recommended fix: Carry the project URL separately into full reconnect and use it for region discovery, while retaining the selected region URL for the initial dial and in-place resume. Add a regression test with distinct project and region hostnames and verify that full reconnect reuses the refreshed list.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed by 9ac6a7a in #1709: a full reconnect now looks up regions under the project URL, which this PR keeps fresh.

@github-actions

Copy link
Copy Markdown
Contributor

Changeset ✓

This PR includes a changeset covering all affected packages:

Package Bump
livekit patch
livekit-api patch
livekit-capture patch
livekit-ffi patch
livekit-region patch
livekit-signaling patch

This branch has not been deployed

No deployments
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.

1 participant