diff --git a/socketmode/socket_mode_managed_conn.go b/socketmode/socket_mode_managed_conn.go index 428999732..b259fd624 100644 --- a/socketmode/socket_mode_managed_conn.go +++ b/socketmode/socket_mode_managed_conn.go @@ -123,9 +123,9 @@ func (smc *Client) run(ctx context.Context, connectionCount int) error { } }() - wg.Add(1) + // We don't wait on runMessageReceiver because it doesn't block on a select with the context, + // so we'd have to wait for the ReadJSON to time out, which can take a while. go func() { - defer wg.Done() defer cancel() // The receiver reads WebSocket messages, and enqueues parsed Socket Mode requests to be handled by @@ -162,7 +162,8 @@ func (smc *Client) run(ctx context.Context, connectionCount int) error { return firstErr } - // wg.Wait() finishes only after any of the above go routines finishes. + // wg.Wait() finishes only after any of the above go routines finishes and cancels the + // context, allowing the other threads to shut down gracefully. // Also, we can expect firstErr to be not nil, as goroutines can finish only on error. smc.Debugf("Reconnecting due to %v", firstErr)