nflacco
nflacco

Reputation: 5082

Node.js uncaught socket exception- socket is closed

Here's the problem. I get an exception when writing to a closed socket, and the exception is not caught by a try catch, presumably because it is inside a promise.

promise.then(function (val) {
  try {
    sock.write(val + tcpDelimiter);
  } catch (err) {
    logger.info('yeah right this will not work at all');
  }
})
.fail(function (error) {});

Moreover, adding process.uncaughtexception to the main app doesn't help either.

Here's the console output:

 2012-10-04T19:22:21.109Z - error: uncaughtException date=Thu Oct 04
 2012 19:22:21 GMT+0000 (UTC), pid=21508, uid=0, gid=0,
 cwd=/home/ec2-user/js-proxy, execPath=/usr/bin/nodejs,
 version=v0.6.18, argv=[/usr/local/bin/node,
 /home/ec2-user/js-proxy/app.js, --jsproxy], rss=13914112,
 heapTotal=5634752, heapUsed=4082564, loadavg=[0.0380859375,
 0.08056640625, 0.060546875], uptime=1721344.103162099, trace=[column=19, file=net.js, function=Socket._write, line=474,
 method=_write, native=false, column=15, file=net.js,
 function=Socket.write, line=466, method=write, native=false,
 column=24, file=/home/ec2-user/js-proxy/app.js, function=null,
 line=408, method=null, native=false, column=32,
 file=/home/ec2-user/js-proxy/node_modules/q/q.js, function=_fulfilled,
 line=860, method=null, native=false, column=34,
 file=/home/ec2-user/js-proxy/node_modules/q/q.js, function=null,
 line=881, method=null, native=false, column=9,
 file=/home/ec2-user/js-proxy/node_modules/q/q.js,
 function=makePromise.promiseSend, line=553, method=promiseSend,
 native=false, column=28,
 file=/home/ec2-user/js-proxy/node_modules/q/q.js, function=null,
 line=880, method=null, native=false, column=9,
 file=/home/ec2-user/js-proxy/node_modules/q/q.js,
 function=makePromise.promiseSend, line=553, method=promiseSend,
 native=false, column=35,
 file=/home/ec2-user/js-proxy/node_modules/q/q.js, function=, line=465,
 method=null, native=false, column=38, file=node.js,
 function=EventEmitter._tickCallback, line=190, method=_tickCallback,
 native=false], stack=[Error: This socket is closed.,     at
 Socket._write (net.js:474:19),     at Socket.write (net.js:466:15),   
 at /home/ec2-user/js-proxy/app.js:408:24,     at _fulfilled
 (/home/ec2-user/js-proxy/node_modules/q/q.js:860:32),     at
 /home/ec2-user/js-proxy/node_modules/q/q.js:881:34,     at
 makePromise.promiseSend
 (/home/ec2-user/js-proxy/node_modules/q/q.js:553:9),     at
 /home/ec2-user/js-proxy/node_modules/q/q.js:880:28,     at
 makePromise.promiseSend
 (/home/ec2-user/js-proxy/node_modules/q/q.js:553:9),     at
 Array.<anonymous>
 (/home/ec2-user/js-proxy/node_modules/q/q.js:465:35),     at
 EventEmitter._tickCallback (node.js:190:38)]

UPDATE: I was at a Node.js talk and learned a bit about exception handling; apparently it plain sucks and is neglected. It's being worked on.

Upvotes: 1

Views: 3770

Answers (1)

nflacco
nflacco

Reputation: 5082

The simplest fix seems to be checking if the socket is writable before writing. You could also check the destroyed property. This stuff is all set in the socket closed event.

promise.then(function (val) {
  if( sock.writable ){
    sock.write(val + conf.proxy.tcpDelimiter);
  } else {
    logger.info('this works');
  }
})
.fail(function (error) {});

Upvotes: 1

Related Questions