Skip to content

Fix #1495 SocketModeClient still fails to automatically reconnect when apps.connections.open API returns an error code#1496

Merged
seratch merged 1 commit intoslackapi:mainfrom
seratch:issue-1495-reconnection
Jun 14, 2022
Merged

Fix #1495 SocketModeClient still fails to automatically reconnect when apps.connections.open API returns an error code#1496
seratch merged 1 commit intoslackapi:mainfrom
seratch:issue-1495-reconnection

Conversation

@seratch
Copy link
Contributor

@seratch seratch commented Jun 13, 2022

Summary

This pull request resolves #1495 .

To test the behavior, you can modify retrieveWSSURL method as below plus run cd examples && ./link.sh && npm start to see how it works:

  private reconnecting: boolean = false;

  private async retrieveWSSURL(): Promise<AppsConnectionsOpenResponse> {
    if (this.reconnecting && this.numOfConsecutiveReconnectionFailures % 2 === 0) {
      const e: WebAPIPlatformError = {
        name: 'XXX',
        message: 'An API error occurred: XXX',
        code: APICallErrorCode.PlatformError,
        data: {
          ok: false,
          error: 'internal_error',
        },
      };
      throw e;
    }
    this.reconnecting = true;
    try {
      this.logger.debug('Going to retrieve a new WSS URL ...');
      return await this.webClient.apps.connections.open();
    } catch (error) {
      this.logger.error(`Failed to retrieve a new WSS URL for reconnection (error: ${error})`);
      throw error;
    }
  }

The direct cause of the reconnection failure was that there is no logic to handle connecting:reconnecting in the current code. When you use a sub state machine in this state machine library, it has a state hierarchy and your code needs to have the comprehensive set of handlers for all the state sets.

To prevent this type of coding error happening again in the future (actually, this pattern errors existed much more before 1.3.0 release), I've added enums for the sub state machines - ConnectingState and ConnectedState. With these, passing a state to a sub machine is type-safe. Passing the top-level State enum to a sub state machine does not compile anymore.

Requirements (place an x in each [ ])

…nect when apps.connections.open API returns an error code
@seratch seratch added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented pkg:socket-mode applies to `@slack/socket-mode` labels Jun 13, 2022
@seratch seratch added this to the socket-mode@1.3.1 milestone Jun 13, 2022
@seratch seratch requested a review from filmaj June 13, 2022 11:21
@seratch seratch self-assigned this Jun 13, 2022
Copy link
Member

@mwbrooks mwbrooks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for continuing to harden the Socket Mode package!

Is this a new, recent error or one that has existed for a while but we've only become aware of?

[ERROR] Failed to retrieve a new WSS URL for reconnection (error: Error: An API error occurred: internal_error)

@seratch
Copy link
Contributor Author

seratch commented Jun 13, 2022

Is this a new, recent error or one that has existed for a while but we've only become aware of?

@mwbrooks This is not a new but a rare pattern. Except the ones listed in the "unrecoverable" error code list, the rest should be retried but the state machine is not working in the way at least with the current code.

Copy link
Contributor

@filmaj filmaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I like the split between groups of states 👍 :shipit:

@seratch seratch merged commit 276ba33 into slackapi:main Jun 14, 2022
@seratch seratch deleted the issue-1495-reconnection branch June 14, 2022 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented pkg:socket-mode applies to `@slack/socket-mode`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SocketModeClient still fails to automatically reconnect when apps.connections.open API returns an error code

3 participants