Skip to content
Open
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
4 changes: 2 additions & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
const cb = arguments.length && arguments[arguments.length - 1];
if (typeof cb === 'function') {
function removeListeners() {
this.removeListener('error', removeListeners);
this.removeListener(EventEmitter.errorMonitor, removeListeners);
this.removeListener('listening', onListening);
}

Expand All @@ -287,7 +287,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
FunctionPrototypeCall(cb, this);
}

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

Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-dgram-bind-error-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common');
const assert = require('node:assert');
const dgram = require('node:dgram');

// Ensure that bind errors (e.g. EADDRINUSE) are not silently swallowed
// when socket.bind() is called with a callback but without a user
// 'error' handler.

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

socket1.bind(0, common.mustCall(() => {
const { port } = socket1.address();
const socket2 = dgram.createSocket('udp4');

process.on('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err.code, 'EADDRINUSE');
socket1.close();
socket2.close();
}));

socket2.bind({ port }, common.mustNotCall());
}));