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: wait for disconnect before callback #370

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

Conversation

nbsp
Copy link
Member

@nbsp nbsp commented Jul 13, 2024

should fix a race condition where in node-sdks await room.disconnect() doesn't wait for the room status update to change.

draft because it doesn't work reliably yet: patched node-sdks still fails approximately one in fifty times.

node-sdks diff
diff --git a/packages/livekit-rtc/src/room.ts b/packages/livekit-rtc/src/room.ts
index 143774f..f9bebeb 100644
--- a/packages/livekit-rtc/src/room.ts
+++ b/packages/livekit-rtc/src/room.ts
@@ -16,6 +16,7 @@ import type {
   ConnectResponse,
   ConnectionQuality,
   DataPacketKind,
+  DisconnectCallback,
   DisconnectResponse,
   IceServer,
   RoomInfo,
@@ -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,6 +158,10 @@ 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();
   }

@nbsp nbsp marked this pull request as ready for review July 19, 2024 21:58
@nbsp
Copy link
Member Author

nbsp commented Jul 19, 2024

fixed on the node-sdks side.
merge this before merging livekit/node-sdks#223

Comment on lines +443 to 445
let (tx, rx) = oneshot::channel();
session.close().await;
let _ = close_tx.send(());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let (tx, rx) = oneshot::channel();
session.close().await;
let _ = close_tx.send(());
session.close().await;

Comment on lines +447 to +448
let _ = self.engine_tx.send(EngineEvent::Disconnected { reason, tx });
let _ = rx.await;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let _ = self.engine_tx.send(EngineEvent::Disconnected { reason, tx });
let _ = rx.await;
let _ = self.engine_tx.send(EngineEvent::Disconnected { reason });

@theomonnom
Copy link
Member

theomonnom commented Jul 28, 2024

I think just removing close_tx and waiting for engine_tx to ends should do the work
(we can close engine_tx on EngineInner)

@nbsp
Copy link
Member Author

nbsp commented Jul 28, 2024

I think just removing close_tx and waiting for engine_tx to ends should do the work (we can close engine_tx on EngineInner)

i don't think this works, await room.disconnect() on Node doesn't unblock

@nbsp nbsp requested a review from theomonnom August 15, 2024 21:28
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.

None yet

2 participants