Mystery TCP error in node TLS client

415 views Asked by At

I have several node tcp clients and a tls client that connect to external servers:

    var socket = net.connect(args.connect.ip_port, args.connect.host);
    socket.setTimeout(this.timeout);
    socket.on('connect', this.socketOnConnect.bind(this));
    socket.on('error', this.socketOnError.bind(this));
    socket.on('timeout', this.socketOnTimeout.bind(this));

or

    this.clearStream = tls.connect(
        args.connect.ip_port,
        args.connect.url,
        {},
        this.onSecureConnect.bind(this)
    );
    this.clearStream.on('error', this.clearStreamOnError.bind(this));
    this.clearStream.on('end', this.clearStreamOnEnd.bind(this));

Both of these servers intermittently emit an unhandled Error:

(err): events.js:72
 (err):         throw er; // Unhandled 'error' event
 (err):               ^
 (err): Error: read ECONNRESET
 (err):     at errnoException (net.js:904:11)
 (err):     at TCP.onread (net.js:558:19)
 (err): events.js:72
 (err):         throw er; // Unhandled 'error' event
 (err):               ^
 (err): Error: read ECONNRESET
 (err):     at errnoException (net.js:904:11)
 (err):     at TCP.onread (net.js:558:19)

I reckoned that the on('error handler should deal with these error but evidently not. What have I missed?

I guess I could use a domain to wrap the connect calls but I'd rather understand what is happening before I do that.

1

There are 1 answers

0
RoyHB On

I haven't discovered why the errors are sneaking past the on('error handlers but I've gone ahead and wrapped the suspected functions in domains with a traceback in the domain error handler, which has at least made the issue manageable.

sample code below for anyone else who may be interested.

    var self = this;
    var domain1 = domain.create();

    domain1.on('error', function (err) {
        log.error(util.format('Domain error in tls client: %s.  Exiting.', err.message));
        log.error(traceback.replace(/(?:\r\n|\r|\n)/g, '<br />'));
        self.cleanExit();
    });

    domain1.run(function () {
        self.clearStream = tls.connect(
            args.connect.ip_port,
            args.connect.url,
            {},
            self.onSecureConnect.bind(self)
        );
    });