Skip to content

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

Open
armanmikoyan wants to merge 1 commit intonodejs:mainfrom
armanmikoyan:fix/dgram-bind-silent-error
Open

dgram: don't swallow bind errors when callback is provided#62602
armanmikoyan wants to merge 1 commit intonodejs:mainfrom
armanmikoyan:fix/dgram-bind-silent-error

Conversation

@armanmikoyan
Copy link
Copy Markdown

@armanmikoyan armanmikoyan commented Apr 5, 2026

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

When Socket.prototype.bind() is called with a callback, the internal
cleanup listener is registered on 'error', which counts as an error
handler and silently swallows bind errors (e.g. EADDRINUSE) when no
user error handler is attached.

This switches to EventEmitter.errorMonitor for the cleanup listener,
matching the pattern already used in the enqueue() function
(lib/dgram.js:571). This ensures bind errors properly propagate as
unhandled errors while still performing listener cleanup.

Setup — a socket already bound to port 5000

import dgram from 'dgram';

const receiver = dgram.createSocket('udp4');

receiver.bind({ port: 5000, address: '127.0.0.1' });

No callback, no listening event — error surfaces (correct)

import dgram from 'dgram';

const socket = dgram.createSocket('udp4');

socket.bind({ port: 5000, address: '127.0.0.1' }); 
// Throws: Error: bind EADDRINUSE 127.0.0.1:5000

Using listening event — error surfaces (correct)

import dgram from 'dgram';

const socket = dgram.createSocket('udp4');

socket.on('listening', () => {
  console.log('bound');
});

socket.bind({ port: 5000, address: '127.0.0.1' });
// Throws: Error: bind EADDRINUSE 127.0.0.1:5000

Using callback — error silently swallowed (bug)

import dgram from 'dgram';

const socket = dgram.createSocket('udp4');

socket.bind(
  { port: 5000, address: '127.0.0.1' },
  () => { console.log('bound'); },
);
// No error, no output — process exits silently

All three examples do the same thing — bind to a port already in use.
The first two properly throw, but the third silently swallows the error
just because a callback was passed to bind().

After this fix, all three cases properly surface the error.

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added dgram Issues and PRs related to the dgram subsystem / UDP. needs-ci PRs that need a full CI run. labels Apr 5, 2026
@armanmikoyan armanmikoyan force-pushed the fix/dgram-bind-silent-error branch 2 times, most recently from 60c4c3a to 6624c6f Compare April 5, 2026 17:12
Signed-off-by: armanmikoyan <arman.mikoyan1@gmail.com>
@armanmikoyan armanmikoyan force-pushed the fix/dgram-bind-silent-error branch from 6624c6f to 4b5f804 Compare April 5, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dgram Issues and PRs related to the dgram subsystem / UDP. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants