dgram: generalized send queue to handle close · nodejs/node@a2a711a
@@ -283,20 +283,25 @@ function fixBufferList(list) {
283283function enqueue(self, toEnqueue) {
284284// If the send queue hasn't been initialized yet, do it, and install an
285285// event handler that flushes the send queue after binding is done.
286-if (!self._sendQueue) {
287-self._sendQueue = [];
288-self.once('listening', function() {
289-// Flush the send queue.
290-for (var i = 0; i < this._sendQueue.length; i++)
291-this.send.apply(self, this._sendQueue[i]);
292-this._sendQueue = undefined;
293-});
286+if (!self._queue) {
287+self._queue = [];
288+self.once('listening', clearQueue);
294289}
295-self._sendQueue.push(toEnqueue);
290+self._queue.push(toEnqueue);
296291return;
297292}
298293299294295+function clearQueue() {
296+const queue = this._queue;
297+this._queue = undefined;
298+299+// Flush the send queue.
300+for (var i = 0; i < queue.length; i++)
301+queue[i]();
302+}
303+304+300305// valid combinations
301306// send(buffer, offset, length, port, address, callback)
302307// send(buffer, offset, length, port, address)
@@ -353,7 +358,7 @@ Socket.prototype.send = function(buffer,
353358// If the socket hasn't been bound yet, push the outbound packet onto the
354359// send queue and send after binding is complete.
355360if (self._bindState != BIND_STATE_BOUND) {
356-enqueue(self, [list, port, address, callback]);
361+enqueue(self, self.send.bind(self, list, port, address, callback));
357362return;
358363}
359364@@ -407,10 +412,15 @@ function afterSend(err, sent) {
407412this.callback(err, sent);
408413}
409414410-411415Socket.prototype.close = function(callback) {
412416if (typeof callback === 'function')
413417this.on('close', callback);
418+419+if (this._queue) {
420+this._queue.push(this.close.bind(this));
421+return this;
422+}
423+414424this._healthCheck();
415425this._stopReceiving();
416426this._handle.close();