question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Room.setAndEmitConnectionState rejects a promise with Error: "disconnected from Room" that cannot be handled

See original GitHub issue

Summary

The method Room.setAndEmitConnectionState rejects a promise with Error: “disconnected from Room” that cannot be handled, at least in the following scenarios, and possibly more:

  1. Reconnection fails and max attempts are exhausted
  2. When the room is in reconnecting state and I call disconnect

https://github.com/livekit/client-sdk-js/blob/9c999845a34984a24dc4b3b27a0c09746e2ba3c8/src/room/Room.ts#L1052-L1056

Screenshot 2022-07-24 at 11 52 03 AM

Details

1. Reconnection failed and max attempts exhausted

It would be nice to be able to handle this gracefully. For now I’m using a Custom reconnect policy:

import { DefaultReconnectPolicy } from "livekit-client";

export class CustomReconnectPolicy extends DefaultReconnectPolicy {
  constructor({ handleGiveUp }) {
    this.handleGiveUp = handleGiveUp;
  }

  nextRetryDelayInMs(context) {
    const result = super.nextRetryDelayInMs(context);
    if (result !== null) {
      return result;
    } else {
      return this.handleGiveUp(context);
    }
  }
}

But that results in the following problem

2. When the room is in reconnecting state and I call disconnect

I handle reconnection failure by directing the user away from the conference page in my CustomReconnectPolicy.handleGiveUp callback. But during clean up, when I call room.disconnect().catch(console.error), Room.setAndEmitConnectionState rejects the connectFuture that results in an unhandled rejection.

https://github.com/livekit/client-sdk-js/blob/9c999845a34984a24dc4b3b27a0c09746e2ba3c8/src/room/Room.ts#L1052-L1056

Solutions

Do not reject a promise that is not returned or is inaccessible via the public API.

1. Reconnection failed and max attempts exhausted

Additionally, emit the reason for disconnect here https://github.com/livekit/client-sdk-js/blob/9c999845a34984a24dc4b3b27a0c09746e2ba3c8/src/room/RTCEngine.ts#L469

2. When the room is in reconnecting state and I call disconnect

disconnect() should only reject a promise that was returned during a call to connect() and not reject a promise that was created on reconnection. https://github.com/livekit/client-sdk-js/blob/9c999845a34984a24dc4b3b27a0c09746e2ba3c8/src/room/Room.ts#L1040-L1044

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
aravindanvecommented, Jul 29, 2022

Haha yes I considered it, but decided against doing so. I’m not entirely familiar with the livekit client-server protocol yet - didn’t want to mess anything up. I started using livekit a little over a week ago. Once I have a little more experience with livekit, I’ll try and contribute for sure!

1reaction
lukasIOcommented, Jul 29, 2022

great, thank you for checking!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling with promises - The Modern JavaScript Tutorial
Promise chains are great at error handling. When a promise rejects, the control jumps to the closest rejection handler.
Read more >
client-sdk-js/Room.ts at main · livekit/client-sdk-js - GitHub
In LiveKit, a room is the logical grouping for a list of participants. ... manage to cancel the connection attempt, reject the connect...
Read more >
What is an unhandled promise rejection? - Stack Overflow
The origin of this error lies in the fact that each and every promise is expected to handle promise rejection i.e. have a...
Read more >
Handling those unhandled promise rejections with JS async ...
One of your await ed functions fails (i.e. rejects a promise); You get the error. Another possibility is that you know you need...
Read more >
Promise.reject() - JavaScript - MDN Web Docs
The Promise.reject() method returns a Promise object that is rejected with a given reason.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found