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

livekit-rtc: set room state to disconnected on dc

Open nbsp opened this issue 1 year ago • 7 comments

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

nbsp avatar Jun 28 '24 11:06 nbsp

⚠️ No Changeset found

Latest commit: 5dabd533ed7a5b1f252787193874a6e0afc5c8e6

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

changeset-bot[bot] avatar Jun 28 '24 11:06 changeset-bot[bot]

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

nbsp avatar Jun 28 '24 12:06 nbsp

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

nbsp avatar Jun 28 '24 13:06 nbsp

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?

theomonnom avatar Jun 28 '24 15:06 theomonnom

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 avatar Jul 01 '24 10:07 nbsp

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

nbsp avatar Jul 01 '24 12:07 nbsp

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 avatar Jul 19 '24 21:07 nbsp

can we close this? Is there still an issue around room state when disconnecting?

lukasIO avatar Feb 10 '25 10:02 lukasIO