nakama-js icon indicating copy to clipboard operation
nakama-js copied to clipboard

only close adapter if it is open

Open thegoldenmule opened this issue 1 year ago • 1 comments
trafficstars

We are seeing errors in production where this code in socket.ts:

this.adapter.onError = (evt) => {
        reject(evt);
        this.adapter.close();
};

Calls this:

close() {
    this._socket.close();
    this._socket = void 0;
}

But this._socket is undefined and throws an error. This throws a TypeError inside of a Promise constructor, which is then unhandled because the promise is already rejected. This can be demonstrated very succinctly with this bit of code:

const foo = () => new Promise((res, rej) => {
  rej('Error A');

  throw new Error('Error B');
});

const run = async () => {
  try {
    await foo();
  } catch(error) {
    console.log('Caught an error: ' + error);
  }
};

run();

This will output: Caught an error: Error A, whereas Error B cannot be handled.

To fix this, mirroring all other calls to this.adapter.close(), we simply need to wrap with a check that the adapter is indeed open.

thegoldenmule avatar Jun 26 '24 15:06 thegoldenmule

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jun 26 '24 15:06 CLAassistant

Pinging @lugehorsam for visibility.

thegoldenmule avatar Jul 02 '24 18:07 thegoldenmule