Skip to content

Conversation

Shreyas281299
Copy link
Contributor

@Shreyas281299 Shreyas281299 commented Sep 24, 2025

COMPLETES # https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6913

This pull request addresses

  • The PR adds the support for the shutdown event from the mercury.

by making the following changes

  • When mercury sends the shutdown event, SDK connects to a new mercury connection using the same mercury url. When the new WS is connected the older one is closed.

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

  • Unit Tests
  • While on a call triggered a shutdown message on the mercury connection and ensure a new connection was setted up without any interruption
  • While in a meeting triggered a shutdown message on the mercury connection and ensure a new connection was setted up without any interruption
  • Locally linked SDK with WWC and tested a call and a meeting and triggered a shutdown event.

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 - Please Specify
  • 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 September 24, 2025 11:28
@Shreyas281299 Shreyas281299 added the validated If the pull request is validated for automation. label Sep 24, 2025
@Shreyas281299
Copy link
Contributor Author

@CodeRabbit review

Copy link

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

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

Copy link
Contributor

@rarajes2 rarajes2 left a comment

Choose a reason for hiding this comment

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

  1. Are we not adding/updating UTs ?
  2. Please add vidcast


if (this.webex.config.defaultMercuryOptions) {
this.logger.info(`${this.namespace}: setting custom options for switchover`);
options = {...options, ...this.webex.config.defaultMercuryOptions};
Copy link
Contributor

Choose a reason for hiding this comment

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

Order of spreading the options seems incorrect. The default options should come first so that the later can override it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this

}
this._shutdownSwitchoverInProgress = true;
this._shutdownSwitchoverId = `${Date.now()}`;
this.logger.info(
Copy link
Contributor

Choose a reason for hiding this comment

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

this.logger.log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this file the deafult has been logger.info for the existint code. I feel we can keep this as info only.

return pendingSocket.open(webSocketUrl, options);
})
.then(() => {
this.logger.info(
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. We were keeping info only at the beginning of the function

`${this.namespace}: [shutdown] switchover start, id=${this._shutdownSwitchoverId}`
);

const pendingSocket = new Socket();
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's rename it newSocket


Promise.all([this._prepareUrl(), this.webex.credentials.getUserToken()])
.then(([webSocketUrl, token]) => {
attemptWSUrl = webSocketUrl;
Copy link
Contributor

Choose a reason for hiding this comment

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

attemptWSUrl -> newWSUrl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

Comment on lines 96 to 100
pendingSocket.on('close', (event) => this._onclose(event, pendingSocket));
pendingSocket.on('message', (...args) => this._onmessage(...args));
pendingSocket.on('pong', (...args) => this._setTimeOffset(...args));
pendingSocket.on('sequence-mismatch', (...args) => this._emit('sequence-mismatch', ...args));
pendingSocket.on('ping-pong-latency', (...args) => this._emit('ping-pong-latency', ...args));
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like repetitive code. We can create a method which accepts the socket param and adds these listeners and call that method in other paces too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made a new method for adding the event listeners

Comment on lines 142 to 147
// Do not force-close oldSocket; server will close it with 4001.
if (oldSocket) {
this.logger.info(
`${this.namespace}: [shutdown] old socket retained; awaiting 4001 close`
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Q. Is 4001 a status code? We can improve the message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, 4001 is 'replacement of the connection due to shutdown'. Updated the message

/* eslint complexity: [0] */

try {
const isActiveSocket = !sourceSocket || (this.socket && sourceSocket === this.socket);
Copy link
Contributor

Choose a reason for hiding this comment

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

The sourceSocket passed is a new socket created (line number 300) which will never be undefined so the first condition will always return false and proceed with the next condition. Please correct if I am wrong

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we are already updating the this.socket with the new one or active one. Shouldn't we be able to call the close on it directly ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So in this condition all we need to know is that

  • If the closing socket is the current socket (this is a normal closing of mercury connection)
  • If the closing socket is an older inactive socket (this is the case of mercury shutdown)

Simplified the condition

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I got confused with the name activeSocket. I thought it means the socket is active but here it means that it is the currently being used socket.

Comment on lines 647 to 651
try {
this._emit('event:mercury_shutdown_imminent', envelope);
} catch (e) {
// ignore observer errors
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. Try-catch not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

typeof data.sequenceNumber === 'number' ||
(typeof data.sequenceNumber === 'string' && data.sequenceNumber.trim() !== '')
) {
const sequenceNumber = parseInt(data.sequenceNumber, 10);
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we getting sequenceNumber in different number system ? We are already checking the type. I think the parsing should have been done before the condition

Copy link
Contributor

@Kesari3008 Kesari3008 left a comment

Choose a reason for hiding this comment

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

Did we test calling flows when the shutdown occurs to see if we have any impact there due to connection replacement ?

@Kesari3008
Copy link
Contributor

Please update the description with details of testing done for this change and check the boxes required

Copy link
Contributor

@Kesari3008 Kesari3008 left a comment

Choose a reason for hiding this comment

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

  1. Unit test is failing mercury plugin
  2. Did we test calling flows with mercury shutdown ?

Copy link
Contributor

@mkesavan13 mkesavan13 left a comment

Choose a reason for hiding this comment

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

Did we test this by locally linking the web client once? We need one of WWC member's review as well on this one.

this._emit('offline', event);
this.webex.internal.newMetrics.callDiagnosticMetrics.setMercuryConnectedStatus(false);
let socketUrl;
if (isActiveSocket && this.socket) {
Copy link
Contributor

Choose a reason for hiding this comment

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

isActiveSocket is already ensuring that the this.socket has some value so we don't need to add both the checks.

@Shreyas281299
Copy link
Contributor Author

Did we test this by locally linking the web client once? We need one of WWC member's review as well on this one.

Yes, Ive locally linked and tested a call and a meeting in the WWC

.then(([webSocketUrl, token]) => {
newWSUrl = webSocketUrl;

let options = {
Copy link
Collaborator

@Coread Coread Oct 16, 2025

Choose a reason for hiding this comment

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

these options are a complete duplicate of the code in the open method. This could have been factored out to a private method. In fact, most of what is in this .then could have been factored out. It only really differs by the log strings


this.logger.info(`${this.namespace}: [shutdown] switchover url: ${webSocketUrl}`);

return newSocket.open(webSocketUrl, options);
Copy link
Collaborator

Choose a reason for hiding this comment

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

if the socket fails to open, I think it won't ever have the event listeners removed. You should call removeAllListeners on the socket in the catch

if (isActiveSocket) {
// Only tear down state if the currently active socket closed
if (this.socket) {
this.socket.removeAllListeners();
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: When are the listeners removed from the old socket?


const oldSocket = this.socket;
// Atomically switch active socket reference without closing the old one.
this.socket = newSocket;
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a potential race condition here. If the old mercury websocket disconnects before it reaches this line, then the plugin will still emit the offline. This code won't emit the online either so the consuming application would not realise that it has reconnected


this.logger.info(`${this.namespace}: [shutdown] switchover url: ${webSocketUrl}`);

return newSocket.open(webSocketUrl, options);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This directly connects the websocket where as the first connection uses the backoff strategy. Is this deliberate? Are we not worried that this reconnection attempt is more fragile than the normal connection?

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.

5 participants