slack icon indicating copy to clipboard operation
slack copied to clipboard

Disconnect/Reconnect of RTM.ManageConnection() leaves RTM unsuable

Open weirdion opened this issue 8 years ago • 8 comments

Version: 0.4.0

If the bot looses internet connection for a few minutes, then reconnects, the RTM incoming/outgoing events no longer work.

Steps to reproduce:

  1. Start the bot.
  2. After the ConnectedEvent is reached.
  3. Message the bot, observe the log for MessageEvent gets triggered.
  4. Disable the system's internet access.
  5. Wait for bot to reach DisconnectedEvent (seems to get triggered twice, different bug?).
  6. Enable the system's internet access.
  7. The bot reconnects to the internet and ConnectedEvent is reached.
  8. Wait a minute (If tested right away, message the bot, observer the log for MessageEvent gets triggered (timeout not reached yet?))
  9. Message the bot

Expected: MessageEvent is triggered, with LatencyReport logged periodically. Actual: MessageEvent is not triggered, no LatencyReports logged. RTM.SendMessage no longer works.

Code tested with:

func (b *Bot) runSlackLoop() {
   log.Println("Slack RTM connection handler starting up")
   go b.rtm.ManageConnection()

   for event := range b.rtm.IncomingEvents {
	switch data := event.Data.(type) {
            case *slack.ConnectedEvent:
		log.Printf("[DEBUG] Connection counter: %d", event.ConnectionCount)

	case *slack.MessageEvent:
		log.Printf("[DEBUG] Message received: %v", event)
		go b.handleMessage(data)

	case *slack.LatencyReport:
		log.Printf("[DEBUG] Latency: %v\n", event.Value)

	case *slack.RTMError:
		log.Printf("[ERROR] RTMError: %s", event.Error())

	case *slack.ConnectionErrorEvent:
		log.Printf("[ERROR] ConnectionError: %s", event)

	case *slack.DisconnectedEvent:
		log.Printf("[WARN] Disconnected: (intentional? %v)", event.Intentional)

	case *slack.InvalidAuthEvent:
		log.Println("[ERROR] Invalid credentials")
	}
   }
}

weirdion avatar Nov 19 '18 02:11 weirdion

Can you post the output from sending a SIGABRT to the program while it's in this state? I'm running into a similar issue, and based on that stack trace I think I've narrowed it down to a deadlock introduced by https://github.com/nlopes/slack/pull/354. The DisconnectEvent that gets triggered twice is in fact the same bug as this one, and I think the reason becomes clear as to why as we dig in.

Specifically, these two goroutines are of interest:

goroutine 22 [chan receive, 52 minutes]:
github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.timerReset(0xc4200746e0, 0x1bf08eb000)
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/misc.go:237 +0x70
github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.(*RTM).resetDeadman(0xc4200b49a0)
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/websocket.go:113 +0x3c
github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.(*RTM).handlePong(0xc4200b49a0, 0xc42001f680, 0x37, 0x40)
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/websocket_managed_conn.go:405 +0x5a
github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.(*RTM).handleRawEvent(0xc4200b49a0, 0xc42001f680, 0x37, 0x40, 0xc4203d2040, 0x5)
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/websocket_managed_conn.go:361 +0x174
github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.(*RTM).handleEvents(0xc4200b49a0, 0xc4201b0de0)
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/websocket_managed_conn.go:230 +0x2d3
github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.(*RTM).ManageConnection(0xc4200b49a0)
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/websocket_managed_conn.go:64 +0x290

goroutine 1328 [chan send, 52 minutes]:
github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.(*RTM).receiveIncomingEvent(0xc4200b49a0, 0xc4201b0de0, 0x0)
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/websocket_managed_conn.go:339 +0x24f
github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.(*RTM).handleIncomingEvents(0xc4200b49a0, 0xc4201b0de0)
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/websocket_managed_conn.go:252 +0x4b
created by github.com/nolanlum/tanya/vendor/github.com/nlopes/slack.(*RTM).ManageConnection
        /home/travis/gopath/src/github.com/nolanlum/tanya/vendor/github.com/nlopes/slack/websocket_managed_conn.go:61 +0x275

The first goroutine is blocked on a channel receive from a time.Timer that will never return, because the event was already consumed here https://github.com/nlopes/slack/blob/v0.4.0/websocket_managed_conn.go#L208-L211, which is the cause of the second disconnect event you see. Besides the immediate issue of a deadlocked goroutine, the second problem is that the first goroutine is responsible from reading from the channel that the second goroutine is waiting to sent to (i.e. the incoming events from Slack channel). This is what I believe to be the root cause of the hang as observed in this issue.

I've not enough time to come up with a solution to the issue, but hopefully this sheds a little light on what's going on here. cc: @james-lawrence

nolanlum avatar Dec 28 '18 02:12 nolanlum

oops my bad, couple things we can do. first we should just use timer.Reset() directly. I believe this is the correct fix. I was guarding it with a t.Stop(); _<-t because I misread the documentation around Reset().

other fixes include sending a signal to the main event loop to handle the reset to prevent concurrent reads.

james-lawrence avatar Dec 30 '18 17:12 james-lawrence

@nolanlum @asadana if either of you would confirm the fix from the #456 I'll merge it.

james-lawrence avatar Dec 30 '18 17:12 james-lawrence

Sorry about the delay, was unavailable because of the holidays.

I was able to confirm that PR #456 does indeed fix the problem.

I did notice that resetDeadman() is triggered every 30 seconds, I hope there's no side-effects or added overhead by calling rtm.pingDeadman.Reset(). Otherwise looks good from my side :+1:

weirdion avatar Jan 06 '19 13:01 weirdion

I've confirmed it fixed an error was periodically showing up for us as well so sounds good.

there is overhead, because there is code... but it should be extremely minimal.

james-lawrence avatar Jan 07 '19 04:01 james-lawrence

@james-lawrence Is there anything left to do in-order to get this merged in?

weirdion avatar Jan 16 '19 22:01 weirdion

its already merged.

james-lawrence avatar Jan 16 '19 23:01 james-lawrence

Oh great, closing the issue. Thanks!

weirdion avatar Jan 16 '19 23:01 weirdion