Skip to content
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

livekit-rtc: set room state to disconnected on dc #223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nbsp
Copy link
Member

@nbsp nbsp commented Jun 28, 2024

while trying to reproduce another bug i found this: room.disconnect() does not set the room state to disconnected.

Copy link

changeset-bot bot commented Jun 28, 2024

⚠️ No Changeset found

Latest commit: 5dabd53

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@nbsp nbsp requested a review from theomonnom June 28, 2024 11:07
Copy link
Member

@theomonnom theomonnom left a comment

Choose a reason for hiding this comment

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

I believe the issue is different here. The FfiServer is sending ConnectionStateChanged before it successfully closes the room.

You may not notice this because it's not waiting for the DisconnectCallback, unlike what the python-sdks is doing.

@nbsp
Copy link
Member Author

nbsp commented Jun 28, 2024

aha, i see. i'll change it so that event gets properly processed before the await resolves

@nbsp
Copy link
Member Author

nbsp commented Jun 28, 2024

diff --git a/packages/livekit-rtc/src/room.ts b/packages/livekit-rtc/src/room.ts
index 95f19ff..f9bebeb 100644
--- a/packages/livekit-rtc/src/room.ts
+++ b/packages/livekit-rtc/src/room.ts
@@ -148,7 +149,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
       return;
     }

-    FfiClient.instance.request<DisconnectResponse>({
+    const res = FfiClient.instance.request<DisconnectResponse>({
       message: {
         case: 'disconnect',
         value: {
@@ -157,9 +158,12 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
       },
     });

+    await FfiClient.instance.waitFor<DisconnectCallback>((ev: FfiEvent) => {
+      return ev.message.case == 'disconnect' && ev.message.value.asyncId == res.asyncId;
+    });
+
     FfiClient.instance.removeAllListeners();
     this.removeAllListeners();
-    this.connectionState = ConnectionState.CONN_DISCONNECTED;
   }

   private onFfiEvent = (ffiEvent: FfiEvent) => {

this doesn't work, for some reason. neither RoomEvent.Disconnect or RoomEvent.ConnectionStateChanged fire here, and this goes on without changing anything

@theomonnom
Copy link
Member

theomonnom commented Jun 28, 2024

this doesn't work, for some reason. neither RoomEvent.Disconnect or RoomEvent.ConnectionStateChanged fire here, and this goes on without changing anything

Mmh seems like an issue on the Rust side then?

@nbsp
Copy link
Member Author

nbsp commented Jul 1, 2024

diff --git a/packages/livekit-rtc/src/room.ts b/packages/livekit-rtc/src/room.ts
index 95f19ff..f9bebeb 100644
--- a/packages/livekit-rtc/src/room.ts
+++ b/packages/livekit-rtc/src/room.ts
@@ -148,7 +149,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
       return;
     }

-    FfiClient.instance.request<DisconnectResponse>({
+    const res = FfiClient.instance.request<DisconnectResponse>({
       message: {
         case: 'disconnect',
         value: {
@@ -157,9 +158,12 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
       },
     });

+    await FfiClient.instance.waitFor<DisconnectCallback>((ev: FfiEvent) => {
+      return ev.message.case == 'disconnect' && ev.message.value.asyncId == res.asyncId;
+    });
+
     FfiClient.instance.removeAllListeners();
     this.removeAllListeners();
-    this.connectionState = ConnectionState.CONN_DISCONNECTED;
   }

   private onFfiEvent = (ffiEvent: FfiEvent) => {

this doesn't work, for some reason. neither RoomEvent.Disconnect or RoomEvent.ConnectionStateChanged fire here, and this goes on without changing anything

looked into it a bit more, the DisconnectCallback does fire, but nothing else gets run because of it.
adding a manual RoomEvent.Disconnect hook to the callback might work, but i don't know if that's a hack, because iirc it should do it by itself?

@nbsp
Copy link
Member Author

nbsp commented Jul 1, 2024

sometimes it works? there's a race condition somewhere in here.

@nbsp
Copy link
Member Author

nbsp commented Jul 19, 2024

this is working now: the room state is set to disconnected manually, regardless of whether the ConnectionStateChange event fires on time before end-of-stream. merge livekit/rust-sdks#370 first, then i'll update this PR to use updated main with the necessary changes.

@nbsp nbsp requested a review from theomonnom July 19, 2024 21:59
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.

2 participants