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: remove listeners on bind error #30210

Closed
wants to merge 3 commits 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
23 changes: 18 additions & 5 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,21 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {

state.bindState = BIND_STATE_BINDING;

if (arguments.length && typeof arguments[arguments.length - 1] === 'function')
this.once('listening', arguments[arguments.length - 1]);
const cb = arguments.length && arguments[arguments.length - 1];
if (typeof cb === 'function') {
function removeListeners() {
this.removeListener('error', removeListeners);
this.removeListener('listening', onListening);
}

function onListening() {
removeListeners.call(this);
cb.call(this);
}

this.on('error', removeListeners);
this.on('listening', onListening);
}

if (port instanceof UDP) {
replaceHandle(this, port);
Expand Down Expand Up @@ -240,8 +253,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
}, (err) => {
// Callback to handle error.
const ex = errnoException(err, 'open');
this.emit('error', ex);
state.bindState = BIND_STATE_UNBOUND;
this.emit('error', ex);
});
return this;
}
Expand Down Expand Up @@ -309,8 +322,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
}, (err) => {
// Callback to handle error.
const ex = exceptionWithHostPort(err, 'bind', ip, port);
this.emit('error', ex);
state.bindState = BIND_STATE_UNBOUND;
this.emit('error', ex);
});
} else {
if (!state.handle)
Expand All @@ -319,8 +332,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
const err = state.handle.bind(ip, port || 0, flags);
if (err) {
const ex = exceptionWithHostPort(err, 'bind', ip, port);
this.emit('error', ex);
state.bindState = BIND_STATE_UNBOUND;
this.emit('error', ex);
// Todo: close?
return;
}
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-dgram-bind-error-repeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common');
const dgram = require('dgram');

// Regression test for https://github.com/nodejs/node/issues/30209
// No warning should be emitted when re-trying `.bind()` on UDP sockets
// repeatedly.

process.on('warning', common.mustNotCall());

const reservePortSocket = dgram.createSocket('udp4');
reservePortSocket.bind(() => {
const { port } = reservePortSocket.address();

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

let errors = 0;
newSocket.on('error', common.mustCall(() => {
if (++errors < 20) {
newSocket.bind(port, common.mustNotCall());
} else {
newSocket.close();
reservePortSocket.close();
}
}, 20));
newSocket.bind(port, common.mustNotCall());
});