slack icon indicating copy to clipboard operation
slack copied to clipboard

`goodbye` handling causes connect-disconnect loop due to incorrect signalling

Open nolanlum opened this issue 7 years ago • 4 comments

As part of the cleanup in #492, I believe another race condition was introduced in the disconnect logic, particularly in the interaction between the handling of Slack's goodbye message and the teardown of the receiveIncomingEvent loop.

It seems that the only way for the read loop to be torn down is from a recv on rtm.disconnected, or an error from ReadJSON which triggers a send on killChannel. Since a goodbye triggers a send on killChannel, and the subsequent effect of killConnection is to close the socket being polled by ReadJSON, the end effect is that two false values are sent to the killChannel: the first from the goodbye handling and the second from receiveIncomingEvent. However, the second send always blocks waiting for a recv, since the normal place killChannel is polled is in the handleEvents flow, which has already exited due to handling a goodbye. What makes this bug so insidious, though, is the fact that this connect-disconnect loop does eventually end, and a normal connection can be established.

After 10 tries (each connect-disconnect loop takes about a second to complete), Slack begins ratelimiting the connection. At this point, we are saved by the recv from killChannel as part of the RTM.connect loop logic, which completely drains the killChannel (and also causes us to disregard the backoff cooldown time completely, triggering an immediate reconnect despite being ratelimited), and allows a normal connection to begin.

What you'll see in the debug log is something like this:

2019/10/12 23:35:42 websocket_managed_conn.go:369: Incoming Event: {"type": "goodbye", "source": "gateway_server"}
2019/10/12 23:35:42 websocket_managed_conn.go:211: killing connection
2019/10/12 23:35:42 websocket_managed_conn.go:173: Connecting to RTM
2019/10/12 23:35:42 rtm.go:72: Using URL: [redacted]
2019/10/12 23:35:42 websocket_managed_conn.go:189: Dialing to websocket on url [redacted]
2019/10/12 23:35:42 websocket_managed_conn.go:59: RTM connection succeeded on try 1
2019/10/12 23:35:42 websocket_managed_conn.go:211: killing connection
2019/10/12 23:35:42 websocket_managed_conn.go:173: Connecting to RTM
2019/10/12 23:35:42 rtm.go:72: Using URL: [redacted]
2019/10/12 23:35:42 websocket_managed_conn.go:189: Dialing to websocket on url [redacted]
2019/10/12 23:35:42 websocket_managed_conn.go:59: RTM connection succeeded on try 2
2019/10/12 23:35:42 websocket_managed_conn.go:211: killing connection
2019/10/12 23:35:42 websocket_managed_conn.go:173: Connecting to RTM
...
2019/10/12 23:35:44 websocket_managed_conn.go:173: Connecting to RTM
2019/10/12 23:35:44 rtm.go:68: Failed to connect to RTM: slack rate limit exceeded, retry after 58s
2019/10/12 23:35:44 websocket_managed_conn.go:177: Failed to start or connect to RTM: slack rate limit exceeded, retry after 58s
2019/10/12 23:35:44 websocket_managed_conn.go:143: reconnection 1 failed: slack rate limit exceeded, retry after 58s reconnecting in 58s
2019/10/12 23:35:44 websocket_managed_conn.go:173: Connecting to RTM
2019/10/12 23:35:44 rtm.go:68: Failed to connect to RTM: slack rate limit exceeded, retry after 58s
2019/10/12 23:35:44 websocket_managed_conn.go:177: Failed to start or connect to RTM: slack rate limit exceeded, retry after 58s
2019/10/12 23:35:44 websocket_managed_conn.go:143: reconnection 2 failed: slack rate limit exceeded, retry after 58s reconnecting in 58s
2019/10/12 23:36:42 websocket_managed_conn.go:173: Connecting to RTM
2019/10/12 23:36:42 rtm.go:72: Using URL: [redacted]
2019/10/12 23:36:42 websocket_managed_conn.go:189: Dialing to websocket on url [redacted]
2019/10/12 23:36:42 websocket_managed_conn.go:59: RTM connection succeeded on try 11
2019/10/12 23:36:42 websocket_managed_conn.go:369: Incoming Event: {"type": "hello"}

I believe the best course of action to fix this would be to add a mechanism to either preemptively tear down the receiveIncomingEvent loop at the same time as receiving a goodbye or signal that we are already in the process of tearing down the RTM connection. We can either 1) have receiveIncomingEvent tear itself down when it reads a goodbye off the socket, or 2) signal ongoing teardown by doing something like passing it a unique channel (per-connection, per-attempt) that can be closed by killConnection so we can ignore errors during teardown (or immediately return).

I can open up a PR but I'd like to hear your feedback before deciding on an approach (particularly from @james-lawrence).

Thanks!

nolanlum avatar Oct 14 '19 01:10 nolanlum

so the problem here is that it rate limits? the connection eventually reestablishes?

james-lawrence avatar Nov 02 '19 12:11 james-lawrence

looking again.... how does goodbye trigger a send on the kill channel? there are only two locations kill is written to. and goodbye isn't one of them. I think something else is going on, very well could be related to goodbye but it isn't due to goodbye writing to the kill channel.

the kill channel only receives from the Disconnect method and the receiveIncomingEvents method when it dies.

~i suspect the ErrUnexpectedEOF is the more likely culprit. it could be sent multiple times.~

james-lawrence avatar Nov 02 '19 14:11 james-lawrence

okay I'm following now. I think just removing the return from the goodbye handling will resolve this.

the issue is that goodbye kills the connection and returns leaving the single kill event from the event generator unprocessed, which is then processed by the following connection.

james-lawrence avatar Nov 02 '19 15:11 james-lawrence

I think this might impact all of the cases except the killChannel in handle events.

james-lawrence avatar Nov 02 '19 15:11 james-lawrence