Skip to content

Commit 6624c6f

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 6624c6f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('node:assert');
4+
const dgram = require('node:dgram');
5+
6+
// Regression test: when socket.bind() is called with a callback but without
7+
// a user 'error' handler, bind errors (e.g. EADDRINUSE) must not be silently
8+
// swallowed. Before the fix, the internal cleanup listener was registered on
9+
// 'error', which counted as an error handler and prevented the error from
10+
// being thrown as an uncaught exception.
11+
12+
const socket1 = dgram.createSocket('udp4');
13+
14+
socket1.bind(0, common.mustCall(() => {
15+
const { port } = socket1.address();
16+
const socket2 = dgram.createSocket('udp4');
17+
18+
process.on('uncaughtException', common.mustCall((err) => {
19+
assert.strictEqual(err.code, 'EADDRINUSE');
20+
socket1.close();
21+
socket2.close();
22+
}));
23+
24+
socket2.bind({ port }, common.mustNotCall());
25+
}));

0 commit comments

Comments
 (0)