dgram: don't swallow bind errors when callback is provided

Signed-off-by: armanmikoyan <arman.mikoyan1@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/62602
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
This commit is contained in:
armanmikoyan authored and GitHub committed 2026-08-23 02:39:15 +00:00
1 parent da7b94e0ae
commit 97c7e327f1
2 files changed
+25 -2

No files matched your search

+2 -2
View File
@@ -295,7 +295,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);
}
@@ -304,7 +304,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
FunctionPrototypeCall(cb, this);
}
this.on('error', removeListeners);
this.on(EventEmitter.errorMonitor, removeListeners);
this.on('listening', onListening);
}
@@ -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());
}));