fix: cleanup socker error handler on response

This commit is contained in:
TJ Holowaychuk 2013-12-19 08:53:48 -08:00
parent a58e38454f
commit 171892c669
2 changed files with 17 additions and 1 deletions

View file

@ -98,7 +98,7 @@ app.callback = function(){
return function(req, res, next){
var ctx = self.createContext(req, res);
next = next || ctx.onerror;
ctx.socket.once('error', next);
ctx.onSocketError(next);
co.call(ctx, gen)(next);
}
};

View file

@ -488,4 +488,20 @@ module.exports = {
attachment: function() {
return this.response.attachment.apply(this.response, arguments);
},
/**
* Invoke `fn` on socket errors and
* handle cleanup on response.
*
* @param {Function} fn
* @api private
*/
onSocketError: function(fn){
var sock = this.socket;
sock.once('error', fn);
this.res.on('finish', function(){
sock.removeListener('error', fn);
});
}
};