reconnecting-websocket icon indicating copy to clipboard operation
reconnecting-websocket copied to clipboard

Bug: `_getNextDelay` returns `0` while a socket may be reconnecting/connecting

Open titanism opened this issue 7 months ago • 0 comments

@bytemain there is a bug in the code causing socket to attempt to reconnect in 0ms which causes uncaught exception (e.g. if you're using ws package).

function _getNextDelay() {
  const {
    reconnectionDelayGrowFactor = DEFAULT.reconnectionDelayGrowFactor,
    minReconnectionDelay = DEFAULT.minReconnectionDelay,
    maxReconnectionDelay = DEFAULT.maxReconnectionDelay
  } = this._options;
  let delay = 0;
  if (this._retryCount > 0) {
    delay =
      minReconnectionDelay *
      reconnectionDelayGrowFactor ** (this._retryCount - 1);
    if (delay > maxReconnectionDelay) {
      delay = maxReconnectionDelay;
    }
-  }
+  } else {
+    // -1 `_retryCount` indicates it's reconnecting so wait at least 1s
+    delay = 1000;
+  }

  this._debug('next delay', delay);
  return delay;
}

titanism avatar Jul 03 '24 23:07 titanism