From 6311f1c30a9c579dc354d84809099d28f074b3de Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Mon, 28 Jan 2013 22:19:02 +0100 Subject: [PATCH] dgram: avoid EventEmitter leak warning When a datagram socket hasn't been bound yet, node will defer `send()` operations until binding has completed. Before this patch a `listening` listener would be installed every time `send` was called. This triggered an EventEmitter leak warning when more than 10 packets were sent in a tight loop. Therefore switch to using a single `listening` listener, and use an array to enqueue outbound packets. --- lib/dgram.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 242c197515..222de73eb3 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -243,10 +243,21 @@ Socket.prototype.send = function(buffer, if (self._bindState == BIND_STATE_UNBOUND) self.bind(0, null); + // 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) { - self.once('listening', function() { - self.send(buffer, offset, length, port, address, callback); - }); + // 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._sendQueue.push([buffer, offset, length, port, address, callback]); return; }