Skip to content

Commit

Permalink
client: silently ignore server message sent after channel have been c…
Browse files Browse the repository at this point in the history
…losed by client
  • Loading branch information
erossignon committed Jun 17, 2023
1 parent 5c3f84e commit 5b6228e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Expand Up @@ -6,6 +6,7 @@
// tslint:disable:no-console
import { createPublicKey, randomBytes } from "crypto";
import { EventEmitter } from "events";
import { types } from "util";
import chalk from "chalk";
import * as async from "async";

Expand Down Expand Up @@ -77,7 +78,6 @@ import {
traceClientResponseMessage,
_dump_client_transaction_statistics
} from "../utils";
import { types } from "util";
// import * as backoff from "backoff";
// tslint:disable-next-line: no-var-requires
const backoff = require("backoff");
Expand Down Expand Up @@ -563,7 +563,7 @@ export class ClientSecureChannelLayer extends EventEmitter {
str += "\n is opened ................ : " + this.isOpened();
str += "\n is valid ................. : " + this.isValid();
str += "\n channelId ................ : " + this.channelId;
str += "\n transportParameters: ..... : " ;
str += "\n transportParameters: ..... : ";
str += "\n maxMessageSize (to send) : " + (this._transport?.parameters?.maxMessageSize || "<not set>");
str += "\n maxChunkCount (to send) : " + (this._transport?.parameters?.maxChunkCount || "<not set>");
str += "\n receiveBufferSize(server): " + (this._transport?.parameters?.receiveBufferSize || "<not set>");
Expand Down Expand Up @@ -790,15 +790,14 @@ export class ClientSecureChannelLayer extends EventEmitter {
.join(" ")
);
}

for (const key of Object.keys(this._requests)) {
// kill timer id
const transaction = this._requests[key];
if (transaction.callback) {
transaction.callback(new Error("Transaction has been canceled because client channel is being closed"));
}
}
setImmediate(callback);
callback();
}

/**
Expand Down Expand Up @@ -828,15 +827,22 @@ export class ClientSecureChannelLayer extends EventEmitter {
this._cancel_security_token_watchdog();

doDebug && debugLog("Sending CloseSecureChannelRequest to server");
const request = new CloseSecureChannelRequest({});
const request = new CloseSecureChannelRequest({
requestHeader: {
timeoutHint: 1000 // 1 second !
}
});

this.__in_normal_close_operation = true;

if (!this._transport || this._transport.isDisconnecting()) {
this.dispose();
return callback(new Error("Transport disconnected"));
}
this._performMessageTransaction("CLO", request, () => {
this._performMessageTransaction("CLO", request, (err) => {
if (err) {
warningLog("CLO transaction terminated with error: ", err.message);
}
this.dispose();
callback();
});
Expand All @@ -862,8 +868,8 @@ export class ClientSecureChannelLayer extends EventEmitter {
private _on_message_received(response: Response, msgType: string, requestId: number) {
// assert(msgType !== "ERR");

/* istanbul ignore next */
if (response.responseHeader.requestHandle !== requestId) {
/* istanbul ignore next */
warningLog(response.toString());
errorLog(
chalk.red.bgWhite.bold("xxxxx <<<<<< _on_message_received ERROR"),
Expand All @@ -885,6 +891,12 @@ export class ClientSecureChannelLayer extends EventEmitter {

/* istanbul ignore next */
if (!requestData) {
if (this.__in_normal_close_operation) {
// may be some responses that are received from the server
// after the communication is closed. We can just ignore them
// ( this happens with Dotnet C# stack for instance)
return;
}
errorLog(
chalk.cyan.bold("xxxxx <<<<<< _on_message_received for unknown or timeout request "),
requestId,
Expand Down Expand Up @@ -1061,7 +1073,7 @@ export class ClientSecureChannelLayer extends EventEmitter {
const percent = 75 / 100.0;
let timeout = this.tokenRenewalInterval || lifeTime * percent;
timeout = Math.min(timeout, (lifeTime * 75) / 100);
timeout = Math.max(timeout, 50); // at least one half second !
timeout = Math.max(timeout, 50);

if (doDebug) {
debugLog(
Expand Down Expand Up @@ -1247,7 +1259,6 @@ export class ClientSecureChannelLayer extends EventEmitter {
}

private _connect(transport: ClientTCP_transport, endpointUrl: string, _i_callback: ErrorCallback) {

const on_connect = (err?: Error | null) => {
doDebug && debugLog("Connection => err", err ? err.message : "null");
// force Backoff to fail if err is not ECONNRESET or ECONNREFUSED
Expand Down
4 changes: 2 additions & 2 deletions packages/node-opcua-secure-channel/source/message_builder.ts
Expand Up @@ -386,7 +386,7 @@ export class MessageBuilder extends MessageBuilderBase {
warningLog(chalk.red("MessageBuilder : ERROR DETECTED IN 'message' event handler"));
if (types.isNativeError(err)) {
warningLog(err.message);
warningLog(err.stack);
// warningLog(err.stack);
}
}
} else {
Expand All @@ -400,7 +400,7 @@ export class MessageBuilder extends MessageBuilderBase {
("" + this.totalBodySize).padEnd(8),
objMessage.constructor.name
);
console.log(objMessage.toString());
warningLog(objMessage.toString());

// we don't report an error here, we just ignore the message
return false; // this._report_error(message);
Expand Down
Expand Up @@ -1010,7 +1010,7 @@ export class ServerSecureChannelLayer extends EventEmitter {
}

if (callback) {
setImmediate(callback);
callback();
}

/* istanbul ignore next */
Expand Down Expand Up @@ -1403,7 +1403,7 @@ export class ServerSecureChannelLayer extends EventEmitter {
requestId
};

if (msgType === "CLO" && request.schema.name === "CloseSecureChannelRequest") {
if (msgType === "CLO" /* && request.schema.name === "CloseSecureChannelRequest" */) {
this.close();
} else if (msgType === "OPN" && request.schema.name === "OpenSecureChannelRequest") {
// intercept client request to renew security Token
Expand Down

0 comments on commit 5b6228e

Please sign in to comment.