Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/colony/modules/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,29 +468,38 @@ TCPSocket.prototype.__close = function (tryToClose) {
process.removeListener('tcp-close', this._closehandler);

var retries = 0;
function closeSocket(){
function closeSocket(cb){
if (self.socket === null) return;
var ret = tm.tcp_close(self.socket);
if (ret < 0 && ret != -tm.ENOTCONN) { // -57 is inactive, socket has already been closed
if (retries > 3) {
// tried 3 times and couldn't close, error out
self.emit('close');
self.emit('error', new Error('ENOENT Cannot close socket ' + self.socket + ' Got: err'+ret));
cb && cb();
} else {
retries++;
// try again
setTimeout(closeSocket, 100);
setTimeout(function(){
closeSocket(cb);
}, 100);
}

} else {
self.socket = null;
self.emit('close');
cb && cb();
}
}

if (tryToClose !== false) {
closeSocket();
closeSocket(function(){
self.removeAllListeners();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to wrapped in a callback. Since no other code can reach closeSocket, just close over calls to this.removeAllListeners(); and bind to this (which eliminates the junk self alias binding as well).

});
} else {
self.removeAllListeners();
}

}

TCPSocket.prototype.destroy = TCPSocket.prototype.close = function () {
Expand All @@ -511,7 +520,6 @@ TCPSocket.prototype.destroy = TCPSocket.prototype.close = function () {
self.__close();
}
}
self.removeAllListeners();
});
};

Expand Down