Skip to content

fix(mercury): gracefully handle mercury rebalancing - #5106

Open
Shreyas281299 wants to merge 3 commits into
webex:nextfrom
Shreyas281299:mercury-4000-fix
Open

fix(mercury): gracefully handle mercury rebalancing#5106
Shreyas281299 wants to merge 3 commits into
webex:nextfrom
Shreyas281299:mercury-4000-fix

Conversation

@Shreyas281299

@Shreyas281299 Shreyas281299 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

COMPLETES #N/A

This pull request addresses

Mercury close code 4000 is not always a signal that the current SDK context should reconnect. According to Mercury connection behavior, 4000 with close reason Replaced means another connection already replaced the current registration, so this older connection should stand down. A 4000 with any other reason should be treated as an unexpected close and reconnect.

by making the following changes

  • Checks the Mercury close reason when handling close code 4000.
  • Preserves the existing no-reconnect behavior for 4000 with reason Replaced by emitting offline.replaced.
  • Reconnects the active socket for 4000 with any other reason by emitting offline.transient and calling _reconnect().
  • Adds unit coverage for 4000/Replaced, 4000 with an unknown reason, and non-active socket close handling.

Change Type

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Tooling change
  • Internal code refactor

The following scenarios were tested

  • Automated: yarn workspace @webex/internal-plugin-mercury test:unit --targets mercury.js passed with 100 passing.
  • Automated: yarn workspace @webex/internal-plugin-mercury test:style completed with no errors and one existing JSDoc warning in src/index.js.
  • Manual: Verified the Mercury rebalancing/4000 close-code behavior. Testing recording: https://app.vidcast.io/share/f7a115a2-2c8e-434f-aa0b-6f04984fd253

The GAI Coding Policy And Copyright Annotation Best Practices

  • GAI was not used (or, no additional notation is required)
  • Code was generated entirely by GAI
  • GAI was used to create a draft that was subsequently customized or modified
  • Coder created a draft manually that was non-substantively modified by GAI (e.g., refactoring was performed by GAI on manually written code)
  • Tool used for AI assistance (GitHub Copilot / Other - specify)
    • Github Copilot
    • Other - Cursor/GPT
  • This PR is related to
    • Feature
    • Defect fix
    • Tech Debt
    • Automation

I certified that

  • I have read and followed contributing guidelines
  • I discussed changes with code owners prior to submitting this pull request
  • I have not skipped any automated checks
  • All existing and new tests passed
  • I have updated the documentation accordingly

Make sure to have followed the contributing guidelines before submitting.

@Shreyas281299
Shreyas281299 requested review from a team as code owners July 20, 2026 09:00

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8e0f835eee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

this.logger.info(`${this.namespace}: socket ${sessionId} replaced; will not reconnect`);
if (isActiveSocket) this._emit(sessionId, 'offline.replaced', event);
// If not active, nothing to do
if (reason === 'replaced') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep bare 4000 closes in the replaced path

When the active socket closes with code 4000 but no reason is provided, this condition now falls into the reconnect branch, emitting offline.transient and calling _reconnect; previously every 4000 close was treated as offline.replaced, and the existing mercury-events unit coverage still exercises {code: 4000} as a replacement. In clients or browsers that do not surface the close reason, a replaced socket will now reconnect and can churn against the intended replacement connection instead of staying closed.

Useful? React with 👍 / 👎.

@aws-amplify-us-east-2

Copy link
Copy Markdown

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-5106.d3m3l2kee0btzx.amplifyapp.com

@Shreyas281299 Shreyas281299 added the validated If the pull request is validated for automation. label Jul 27, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9c0d1145da

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const {action, isActiveSocket, messageType} = options;

try {
this.webex.internal.metrics.submitClientMetrics(MERCURY_CLOSE_4000_METRIC, {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Handle the metric submission promise

When the client-metrics request fails asynchronously—such as during the same network disruption that closed Mercury—submitClientMetrics() returns a rejected promise, but this call discards it. The surrounding try/catch only handles synchronous throws, so the failure becomes an unhandled rejection and can terminate Node clients instead of merely logging the metric failure as intended; attach a rejection handler to the returned promise.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

validated If the pull request is validated for automation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant