Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UncaughtException Client Socket #15

Closed
voipnorm opened this issue Sep 4, 2018 · 1 comment
Closed

UncaughtException Client Socket #15

voipnorm opened this issue Sep 4, 2018 · 1 comment
Assignees

Comments

@voipnorm
Copy link

voipnorm commented Sep 4, 2018

In cases where an endpoint is offline but the script is still running it generates a client socket uncaught exception. This is using SSH to long term monitor events from an endpoint. Error is below.

2018-09-04T17:28:19.330Z error: Error [ERR_UNHANDLED_ERROR]: Unhandled error. (client-socket)
at XAPI.emit (events.js:171:17)
at TSHBackend. (/Users/christno/WebstormProjects/walmartApp/node_modules/jsxapi/lib/xapi/index.js:126:13)
at TSHBackend.emit (events.js:182:13)
at DuplexPassThrough. (/Users/christno/WebstormProjects/walmartApp/node_modules/jsxapi/lib/backend/tsh.js:106:20)
at DuplexPassThrough.emit (events.js:182:13)
at Client. (/Users/christno/WebstormProjects/walmartApp/node_modules/jsxapi/lib/transport/ssh.js:106:15)
at Client.emit (events.js:182:13)
at Socket. (/Users/christno/WebstormProjects/walmartApp/node_modules/ssh2/lib/client.js:299:10)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)

@torkildr torkildr self-assigned this Sep 4, 2018
@torkildr
Copy link
Contributor

torkildr commented Sep 5, 2018

It seems this specific case was caused by delayed execution of the handler for the emitted error event.

The reason for this is due to some nuances in the javascript language, and the fact that javascript is single threaded. Our transport layer, in this case ssh2, implements all of its logic, including the connect logic, in an asynchronous manner.

This means that, in the following example, xapi.on is actually executed before the ssh client connects. We will, in other words, always assign the error handler before it is used.

const xapi = jsxapi.connect('ssh://localhost:99');
xapi.on('error', (err) => {
  console.error(`xapi error: ${err}`);
});

On the other hand, if we were to delay the execution of the error handler, we will, more than likely fail.

const xapi = jsxapi.connect('ssh://localhost:99');
Promise.resolve(() => {
  xapi.on('error', (err) => {
    console.error(`xapi error: ${err}`);
  });
});

The fact that we're forcing the consumer of the APIs to set the error handler right after the connect, is very prone for error, and I suggest that in future versions, we'll change the API slightly, so that it is possible to establish handler before triggering the connect.

An example, though not the final design, of such a solution, might be something like:

import Jsxapi from 'jsxapi';

const jsxapi = new Jsxapi();

jsxapi.on('error', err => console.log(err));
jsxapi.connect('ssh://localhost:99')

The follow up case for this issue is #16

@torkildr torkildr closed this as completed Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants