From 97c7e327f1a3f8394f7f1555668b1ca04a979f86 Mon Sep 17 00:00:00 2001 From: armanmikoyan Date: Sun, 23 Aug 2026 06:39:15 +0400 Subject: [PATCH] dgram: don't swallow bind errors when callback is provided MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armanmikoyan PR-URL: https://github.com/nodejs/node/pull/62602 Reviewed-By: Ethan Arrowood Reviewed-By: Matteo Collina Reviewed-By: Gürgün Dayıoğlu --- lib/dgram.js | 4 ++-- .../test-dgram-bind-error-callback.js | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-dgram-bind-error-callback.js diff --git a/lib/dgram.js b/lib/dgram.js index ae1691c1265..653cf6a2630 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -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); } diff --git a/test/parallel/test-dgram-bind-error-callback.js b/test/parallel/test-dgram-bind-error-callback.js new file mode 100644 index 00000000000..d5009f94822 --- /dev/null +++ b/test/parallel/test-dgram-bind-error-callback.js @@ -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()); +}));