-
Notifications
You must be signed in to change notification settings - Fork 33
fix: livekit disconnect issue #240
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reconnection logic has potential issues with stale closures and lacks retry backoff.
Several concerns with the current implementation:
Stale closure: The async handler captures
callTokensandlivekitUrlfrom the closure. If state changes during the asyncroom.connect()call, the values used may be stale. For example, if the user ends the call while reconnecting,callTokensin the closure would still be truthy.No retry delay/backoff: Immediate reconnection without delay could cause rapid retry loops if the server is temporarily unavailable.
Token expiration: After a network issue,
callTokens.audioTokenmight have expired, causing auth failures without any token refresh mechanism.Proposed fix with stale closure guard and retry delay
useEffect(() => { const handleConnectionStateChange = async (state: ConnectionState) => { console.log("Connection state changed:", state); - if (state === ConnectionState.Disconnected && callTokens) { + // Capture current state for the async operation + const currentCallTokens = useStore.getState().callTokens; + const currentLivekitUrl = useStore.getState().livekitUrl; + + if (state === ConnectionState.Disconnected && currentCallTokens) { // Room disconnected but we still have callTokens - try to reconnect console.log("Room disconnected, attempting to reconnect..."); updateCallTokens({ isReconnecting: true }); try { - if (!livekitUrl) { + if (!currentLivekitUrl) { throw new Error("LiveKit URL not available"); } - await room.connect(livekitUrl, callTokens.audioToken); + // Add a small delay before reconnecting + await new Promise((resolve) => setTimeout(resolve, 1000)); + // Re-check that we still want to reconnect + if (!useStore.getState().callTokens) { + console.log("Call ended during reconnection delay, aborting"); + return; + } + await room.connect(currentLivekitUrl, currentCallTokens.audioToken); } catch (error) { console.error("Reconnection failed:", error); updateCallTokens({ isReconnecting: false }); } - } else if (state === ConnectionState.Connected && callTokens?.isReconnecting) { + } else if (state === ConnectionState.Connected && useStore.getState().callTokens?.isReconnecting) { // Successfully reconnected console.log("Successfully reconnected!"); updateCallTokens({ isReconnecting: false }); } }; room.on(RoomEvent.ConnectionStateChanged, handleConnectionStateChange); return () => { room.off(RoomEvent.ConnectionStateChanged, handleConnectionStateChange); }; - }, [room, callTokens, updateCallTokens, livekitUrl]); + }, [room, updateCallTokens]);🤖 Prompt for AI Agents