rust-sdks icon indicating copy to clipboard operation
rust-sdks copied to clipboard

Fix reconnection logic to not exhaust retries prematurely

Open TMysliwiec opened this issue 2 months ago • 0 comments

This is a fix for: https://github.com/livekit/rust-sdks/issues/481, which was just closed again, likely due to inactivity.

Problem: The reconnection logic doesn't properly honor the reconnection interval. The interval starts counting from engine creation, not from when reconnection begins. If the engine runs for over 50 seconds (RECONNECT_ATTEMPTS × RECONNECT_INTERVAL), the reconnection loop immediately executes all attempts without waiting, causing it to fail after a couple of seconds. That is because we are using Tokio's default interval, which utilizes MissedTickBehavior::Burst. Any accumulated ticks will fire immediately. https://github.com/livekit/rust-sdks/blob/06997356c083b01da766c4c93e8c4e8354d871ce/livekit/src/rtc_engine/mod.rs#L52

Solution: By changing the interval missed tick behavior to MissedTickBehavior::Delay, we can achieve the potentially initially desired await before making another reconnection attempt. That way, the reconnection window is significantly larger, thereby increasing the likelihood of reconnection.

Reproduction steps: The problem is quite simple to reproduce. You need to run the basic_room example, wait a minute, disconnect the internet, and observe how fast the reconnection attempts are exhausted.

TMysliwiec avatar Nov 16 '25 07:11 TMysliwiec