Skip to content

Fetch the region list during the first connect attempt - #1708

Open
lukasIO wants to merge 3 commits into
lukas/region-auth-finalfrom
lukas/region-fetch-early
Open

lukasIO wants to merge 3 commits into
lukas/region-auth-finalfrom
lukas/region-fetch-early

Conversation

@lukasIO

@lukasIO lukasIO commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

SignalClient::connect fetched the region list of a LiveKit Cloud project only after the first attempt failed. Every region failover therefore waited for that fetch, up to the 3 s fetch timeout, after the failed attempt. The JS SDK starts the fetch when the connect starts.

Change

  • The region fetch runs next to the first attempt. Both are polled in one select! loop inside connect, with no spawned task. When the attempt succeeds, the fetch is dropped. With region_failover off, there is no fetch.
  • When the attempt fails, connect lets a fetch that is still running finish. It then reads the list again through the cache. A fresh list comes back at once, and a list that expired during the attempt is fetched again: the default max-age and the join timeout are both 5 s.
  • That second read checks the cancel token first, so a cancel that lands while the failed attempt closes its transport returns Cancelled.

A connect to a project with a fresh cached list makes no request. Otherwise each connect now makes one region request, also when the first attempt succeeds.

Verified

  • cargo test -p livekit-signaling: 59 tests pass.
  • connect_fetches_regions_during_the_first_attempt: the region request goes out while the first attempt still waits on a server that never answers. Without the change it times out.
  • failover_refreshes_a_list_that_expired_during_the_attempt: the mock serves the list with max-age=0, and the failover fetches it a second time. Without the revalidation, there is one fetch.
  • cancel_during_the_attempt_cleanup_returns_cancelled (paused clock): the failed attempt's transport never finishes closing, and the test cancels during that close. Without the change, the join error comes back.
  • cancel_interrupts_an_attempt_waiting_on_the_server asserted that a cancelled connect made no region request. That is no longer true, so it now relies on the Cancelled error: a region retry would return the mock region's error instead.

🤖 Generated with Claude Code

SignalClient::connect fetched the region list only after the first attempt
failed, which added up to the 3 s fetch timeout to every failover. The fetch
now runs next to the first attempt in one select loop, with no spawned task,
and is dropped when the attempt succeeds.

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 2 potential issues.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

else {
return Err(SignalError::Cancelled);
let urls = match fetched {
Some(urls) => urls,

@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.

🟡 Redundant region request delays failover

When a region response arrives after the first dial fails with max-age=0, connect requests it again before trying a fallback. The same recheck repeats a failed refresh when fetch_region_urls returns expired cached regions. The extra request can add three seconds before failover.

Learn more

fetch_region_urls returns both newly fetched URLs and stale fallback URLs as Ok. When the first dial fails before the region request completes, connect already waits for that request under the cancellation token. Looking up the list again immediately sends another request if the server set a zero cache lifetime or if a failed refresh left the stale entry unchanged. The region HTTP timeout applies separately to each request.

Example: The cloud WebSocket fails before region discovery finishes. Discovery then returns a usable backup region with Cache-Control: max-age=0. Instead of dialing that backup, the SDK waits for a second discovery request, which can time out before the SDK uses the list it already received.

Recommended fix: Use the result directly when failover had to await the original request. For results completed during the dial, distinguish a previously fresh list from a stale fallback returned after a refresh failure, and revalidate only the previously fresh list. Preserve the cancellation check before using either result.

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 in 0814854: at the failover, the list is read again through the cache, which refetches it only when it went stale. Test: failover_refreshes_a_list_that_expired_during_the_attempt.

Comment thread livekit-signaling/src/lib.rs Resolved
lukasIO and others added 2 commits November 21, 2026 12:56
The list fetched next to the first attempt can expire before that attempt
fails: the default max-age and the join timeout are both 5 s. The failover
now reads the list again through the cache, which refetches only a stale
list and returns a fresh one at once.

That read also goes through run_until_cancelled again. Without it, a
cancel that landed while the failed attempt closed its transport returned
the join error instead of Cancelled when there was no region to try.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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-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