Skip to content

Failing to close a websocket connection when autoReconnect is set to true  #577

@barhun

Description

@barhun

Issue type

  • bug

Brief description

I'm authoring a service that schedules websocket connections to a diverse set of workers. When there is an intermittent connection on one worker and the websocket connection needs to be relocated to another, I can't manage to dispose the instance with failure. It doesn't help to call the function close 'cause it fails to set the variable _isClosing to true since the connection isn't open yet. Also, after the next unsuccessful attempt, it sets the variable _isReconnecting to true so that the condition for reconnection always evaluates to true and continues forever.

Steps to reproduce

  • Construct a WSv2 instance with autoReconnect set to true
  • Disable the network interface that provides the instance with internet connection
  • Call the function close on the WSv2 instance and see for yourself that it doesn't dispose the instance while continuing to register timeouts for reconnection
Additional Notes:
  • For now, I'm making use of a workaround as follows to overcome this issue:
let disposed = false
ws.dispose = async () => {
  if (!disposed) {
    ws.on('auth', ws.dispose)
    ws.on('error', ws.dispose)
    ws.on('close', () => {ws._isClosing = true; ws._isReconnecting = false})

    disposed = true
  }
  
  try {
    await ws.close()
  } catch {}
}
  • With this in place, I can call the function dispose and get rid of the instance completely

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions