GeezerVison is showing github.com — dgram: don't swallow bind errors when callback is provided · nodejs/node@04dfefe
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/dgram.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -296,7 +296,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) { |
296 | 296 | const cb = arguments.length && arguments[arguments.length - 1]; |
297 | 297 | if (typeof cb === 'function') { |
298 | 298 | function removeListeners() { |
299 | | - this.removeListener('error', removeListeners); |
| 299 | + this.removeListener(EventEmitter.errorMonitor, removeListeners); |
300 | 300 | this.removeListener('listening', onListening); |
301 | 301 | } |
302 | 302 |
|
@@ -305,7 +305,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) { |
305 | 305 | FunctionPrototypeCall(cb, this); |
306 | 306 | } |
307 | 307 |
|
308 | | - this.on('error', removeListeners); |
| 308 | + this.on(EventEmitter.errorMonitor, removeListeners); |
309 | 309 | this.on('listening', onListening); |
310 | 310 | } |
311 | 311 |
|
|
Collapse file
Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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 | +})); |