Skip to content

Commit 4b5f804

Browse files
committed
dgram: don't swallow bind errors when callback is provided
Signed-off-by: armanmikoyan <arman.mikoyan1@gmail.com>
1 parent 12249cc commit 4b5f804

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/dgram.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
278278
const cb = arguments.length && arguments[arguments.length - 1];
279279
if (typeof cb === 'function') {
280280
function removeListeners() {
281-
this.removeListener('error', removeListeners);
281+
this.removeListener(EventEmitter.errorMonitor, removeListeners);
282282
this.removeListener('listening', onListening);
283283
}
284284

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

290-
this.on('error', removeListeners);
290+
this.on(EventEmitter.errorMonitor, removeListeners);
291291
this.on('listening', onListening);
292292
}
293293

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('node:assert');
4+
const dgram = require('node:dgram');
5+
6+
// Ensure that bind errors (e.g. EADDRINUSE) are not silently swallowed
7+
// when socket.bind() is called with a callback but without a user
8+
// 'error' handler.
9+
10+
const socket1 = dgram.createSocket('udp4');
11+
12+
socket1.bind(0, common.mustCall(() => {
13+
const { port } = socket1.address();
14+
const socket2 = dgram.createSocket('udp4');
15+
16+
process.on('uncaughtException', common.mustCall((err) => {
17+
assert.strictEqual(err.code, 'EADDRINUSE');
18+
socket1.close();
19+
socket2.close();
20+
}));
21+
22+
socket2.bind({ port }, common.mustNotCall());
23+
}));

0 commit comments

Comments
 (0)