node-slack-sdk icon indicating copy to clipboard operation
node-slack-sdk copied to clipboard

RTM Disconnect in State Disconnected Throws Error

Open buildgreatthings opened this issue 6 years ago • 3 comments

Description

Describe your issue here.

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [x ] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [ x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x ] I've read and agree to the Code of Conduct.
  • [x ] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Packages:

Select all that apply:

  • [ ] @slack/web-api
  • [ ] @slack/events-api
  • [ ] @slack/interactive-messages
  • [ x] @slack/rtm-api
  • [ ] @slack/webhooks
  • [ ] I don't know

Reproducible in:

package version:

node version:

OS version(s):

Steps to reproduce:

  1. Set up RTM, and start the app
  2. Try to run rtm.close() or rtm.disconnect()

Expected result:

RTM socket should close.

Actual result:

Error

Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.

buildgreatthings avatar Jul 31 '19 16:07 buildgreatthings

Hi @awcchungster, thanks for reporting and sorry for the delay in our response.

I tried reproducing this problem but I wasn't able to. Can you provide any more information, or perhaps some sample code, to help us get to the bottom of this?

Here is what I tried:

const { RTMClient} = require('@slack/rtm-api');

const rtm = new RTMClient('xoxb-my-token');

// Start a connection and send a message
rtm.start();
rtm.sendMessage('hello', 'C123456');

// Attach a handler for when I press Ctrl+C in the terminal
process.on('SIGINT', () => {
  console.log('SIGINT received');
  rtm.disconnect().then(() => {
    console.log('disconnected');
  })
  .catch(console.error);
});

I run the program (with my bot token and a channel ID from my workspace substituted) and wait to see the message was sent (so that I know I'm connected). Then I press Ctrl+C on my keyboard to send the SIGINT signal. I see the SIGINT received line logged, and then see the disconnected line logged.

aoberoi avatar Nov 16 '19 01:11 aoberoi

Hello, I am experiencing the same issue. When trying rtm.disconnect() the entire node process crashes with the following stacktrace:

/home/sorunome/repos/soru-slack-client/node_modules/finity/lib/core/StateMachine.js:76
      throw new Error('Unhandled event \'' + event + '\' in state \'' + this.currentState + '\'.');
      ^

Error: Unhandled event 'explicit disconnect' in state 'disconnected'.
    at StateMachine.handleUnhandledEvent (/home/sorunome/repos/soru-slack-client/node_modules/finity/lib/core/StateMachine.js:76:13)
    at /home/sorunome/repos/soru-slack-client/node_modules/finity/lib/core/HierarchicalStateMachine.js:79:33
    at TaskScheduler.execute (/home/sorunome/repos/soru-slack-client/node_modules/finity/lib/core/TaskScheduler.js:32:9)
    at TaskScheduler.enqueue (/home/sorunome/repos/soru-slack-client/node_modules/finity/lib/core/TaskScheduler.js:19:12)
    at HierarchicalStateMachine.handle (/home/sorunome/repos/soru-slack-client/node_modules/finity/lib/core/HierarchicalStateMachine.js:72:24)
    at WebSocket.<anonymous> (/home/sorunome/repos/soru-slack-client/node_modules/@slack/rtm-api/dist/RTMClient.js:405:77)
    at WebSocket.onClose (/home/sorunome/repos/soru-slack-client/node_modules/ws/lib/event-target.js:124:16)
    at WebSocket.emit (events.js:315:20)
    at WebSocket.emitClose (/home/sorunome/repos/soru-slack-client/node_modules/ws/lib/websocket.js:172:10)
    at TLSSocket.socketOnClose (/home/sorunome/repos/soru-slack-client/node_modules/ws/lib/websocket.js:781:15)
    at TLSSocket.emit (events.js:327:22)
    at net.js:674:12
    at TCP.done (_tls_wrap.js:563:7)

Not even a try....catch around the await rtm.disconnect() gets it to, well, not crash. I am trying to get a minimal working example for a crash currently, but it seems to be non-trivial

Sorunome avatar Mar 30 '20 13:03 Sorunome

I figured out the issue, it appears to happen when you call rtm.disconnect() while the client is already disconnected - instead of crashing it is expected to do nothing.

const { RTMClient} = require('@slack/rtm-api');

const token = "xoxp-xxx"; // tested with a legacy xoxp token

const rtm = new RTMClient(token);

for (const evt of ["unable_to_rtm_start", "goodbye", "disconnected", "authenticated", "ready", "error", "bot_added", "bot_changed", "team_proifle_change", "team_pref_change", "user_typing", "presence_change"]) {
  rtm.on(evt, () => {
    console.log("Received " + evt);
  });
}

rtm.start();

process.on('SIGINT', () => {
  console.log('SIGINT received');
  rtm.disconnect().then(() => {
    rtm.disconnect();
    console.log('disconnected');
  })
  .catch(console.error);
});

Sorunome avatar Mar 30 '20 13:03 Sorunome

Hello after like, four years 👋

I have a fix for this teed up for the next major release, getting reviewed over here: https://github.com/slackapi/node-slack-sdk/pull/1764

filmaj avatar Mar 28 '24 19:03 filmaj

This was fixed in v7 of rtm-api. Better (four years) late than never I guess!

filmaj avatar Apr 01 '24 19:04 filmaj