Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Gaurd against infinite memory leak in dgram #8705

Closed
wants to merge 6 commits into from
Closed
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
54 changes: 40 additions & 14 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function Socket(type, listener) {
this._bindState = BIND_STATE_UNBOUND;
this.type = type;
this.fd = null; // compatibility hack
this.highWaterMark = 100;

Choose a reason for hiding this comment

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

I'm curious -- why 100?

Copy link
Author

Choose a reason for hiding this comment

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

Pretty arbitrary. I really don't know what a good default is.


// If true - UV_UDP_REUSEADDR flag will be set
this._reuseAddr = options && options.reuseAddr;
Expand Down Expand Up @@ -150,6 +151,11 @@ function replaceHandle(self, newHandle) {
self._handle = newHandle;
}

function cleanupAndError(self, err) {
self._sendQueue = undefined;
self.emit('error', err);
}

Socket.prototype.bind = function(port /*, address, callback*/) {
var self = this;

Expand Down Expand Up @@ -186,7 +192,7 @@ Socket.prototype.bind = function(port /*, address, callback*/) {
self._handle.lookup(address, function(err, ip) {
if (err) {
self._bindState = BIND_STATE_UNBOUND;
self.emit('error', err);
cleanupAndError(self, err);
return;
}

Expand All @@ -196,7 +202,7 @@ Socket.prototype.bind = function(port /*, address, callback*/) {
if (cluster.isWorker && !exclusive) {
cluster._getServer(self, ip, port, self.type, -1, function(err, handle) {
if (err) {
self.emit('error', errnoException(err, 'bind'));
cleanupAndError(self, errnoException(err, 'bind'));
self._bindState = BIND_STATE_UNBOUND;
return;
}
Expand All @@ -219,7 +225,7 @@ Socket.prototype.bind = function(port /*, address, callback*/) {

var err = self._handle.bind(ip, port || 0, flags);
if (err) {
self.emit('error', errnoException(err, 'bind'));
cleanupAndError(self, errnoException(err, 'bind'));
self._bindState = BIND_STATE_UNBOUND;
// Todo: close?
return;
Expand Down Expand Up @@ -247,6 +253,15 @@ Socket.prototype.sendto = function(buffer,
this.send(buffer, offset, length, port, address, callback);
};

function SocketSendQueueItem(buffer, offset, length, port, address, callback) {
this.buffer = buffer;
this.offset = offset;
this.length = length;
this.port = port;
this.address = address;
this.callback = callback;
}


Socket.prototype.send = function(buffer,
offset,
Expand Down Expand Up @@ -293,28 +308,39 @@ Socket.prototype.send = function(buffer,
if (self._bindState == BIND_STATE_UNBOUND)
self.bind(0, null);

function onListening() {
// Flush the send queue.
for (var i = 0; i < self._sendQueue.length; i++) {
var item = self._sendQueue[i];
self.send.call(self, item.buffer, item.offset, item.length,
item.port, item.address, item.callback);
}
self._sendQueue = undefined;
}

// If the socket hasn't been bound yet, push the outbound packet onto the
// send queue and send after binding is complete.
if (self._bindState != BIND_STATE_BOUND) {
// If the send queue hasn't been initialized yet, do it, and install an
// event handler that flushes the send queue after binding is done.
if (!self._sendQueue) {
self._sendQueue = [];
self.once('listening', function() {
// Flush the send queue.
for (var i = 0; i < self._sendQueue.length; i++)
self.send.apply(self, self._sendQueue[i]);
self._sendQueue = undefined;
});
self.once('listening', onListening);
}

if (self._sendQueue.length <= self.highWaterMark) {
self._sendQueue.push(new SocketSendQueueItem(
buffer, offset, length, port, address, callback));
}
self._sendQueue.push([buffer, offset, length, port, address, callback]);
return;
}

self._handle.lookup(address, function(ex, ip) {
self._handle.lookup(address, dgramSocketSendLookupCallback);

function dgramSocketSendLookupCallback(ex, ip) {
if (ex) {
if (callback) callback(ex);
self.emit('error', ex);
cleanupAndError(self, ex);
}
else if (self._handle) {
var req = { buffer: buffer, length: length }; // Keep reference alive.
Expand All @@ -336,7 +362,7 @@ Socket.prototype.send = function(buffer,
});
}
}
});
}
};


Expand Down Expand Up @@ -462,7 +488,7 @@ Socket.prototype._stopReceiving = function() {
function onMessage(nread, handle, buf, rinfo) {
var self = handle.owner;
if (nread < 0) {
return self.emit('error', errnoException(nread, 'recvmsg'));
return cleanupAndError(self, errnoException(nread, 'recvmsg'));
}
rinfo.size = buf.length; // compatibility
self.emit('message', buf, rinfo);
Expand Down