Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgram: generalized send queue to handle close #7066

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
32 changes: 21 additions & 11 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,25 @@ function fixBufferList(list) {
function enqueue(self, toEnqueue) {
// 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 < this._sendQueue.length; i++)
this.send.apply(self, this._sendQueue[i]);
this._sendQueue = undefined;
});
if (!self._queue) {
self._queue = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

Nothing new in this PR, but this looks like a deopt.

Copy link
Member Author

Choose a reason for hiding this comment

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

why?
Plus, this is used during startup only, so I see little harm in deopting there.

Copy link
Contributor

Choose a reason for hiding this comment

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

The Socket class doesn't have a _queue property in its constructor. I didn't mean a deopt on this function, but on the hidden class for Socket.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I wouldn't worry here, as it will get optimized on the long run, and
it does not add a huge delay during "boot" of a Socket.
Il giorno mar 31 mag 2016 alle 12:00 Ron Korving [email protected]
ha scritto:

In lib/dgram.js
#7066 (comment):

@@ -283,20 +283,25 @@ function fixBufferList(list) {
function enqueue(self, toEnqueue) {
// 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 < this._sendQueue.length; i++)
    
  •    this.send.apply(self, this._sendQueue[i]);
    
  •  this._sendQueue = undefined;
    
  • });
  • if (!self._queue) {
  • self._queue = [];

The Socket class doesn't have a _queue property in its constructor. I
didn't mean a deopt on this function, but on the hidden class for Socket.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/nodejs/node/pull/7066/files/0e094acf5bed6a847c655fc69c33067e29227f60#r65153703,
or mute the thread
https://github.com/notifications/unsubscribe/AADL4wacLUEUMxSKPGvIT5WbfZubJhNZks5qHAaigaJpZM4Ip7BT
.

self.once('listening', clearQueue);
}
self._sendQueue.push(toEnqueue);
self._queue.push(toEnqueue);
return;
}


function clearQueue() {
const queue = this._queue;
this._queue = undefined;

// Flush the send queue.
for (var i = 0; i < queue.length; i++)
queue[i]();
}


// valid combinations
// send(buffer, offset, length, port, address, callback)
// send(buffer, offset, length, port, address)
Expand Down Expand Up @@ -353,7 +358,7 @@ Socket.prototype.send = function(buffer,
// 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) {
enqueue(self, [list, port, address, callback]);
enqueue(self, self.send.bind(self, list, port, address, callback));
return;
}

Expand Down Expand Up @@ -407,10 +412,15 @@ function afterSend(err, sent) {
this.callback(err, sent);
}


Socket.prototype.close = function(callback) {
if (typeof callback === 'function')
this.on('close', callback);

if (this._queue) {
this._queue.push(this.close.bind(this));
return this;
}

this._healthCheck();
this._stopReceiving();
this._handle.close();
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-dgram-close-in-listening.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
// Ensure that if a dgram socket is closed before the sendQueue is drained
// wil not crash
Copy link
Member

Choose a reason for hiding this comment

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

typo: will


const common = require('../common');
const dgram = require('dgram');

const buf = Buffer.alloc(1024, 42);

const socket = dgram.createSocket('udp4');

socket.on('listening', function() {
socket.close();
});

// adds a listener to 'listening' to send the data when
// the socket is available
socket.send(buf, 0, buf.length, common.PORT, 'localhost');