Skip to content

Commit

Permalink
lib: return directly if udp socket close before lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Feb 28, 2024
1 parent 1263bb6 commit c348000
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {

// Resolve address first
state.handle.lookup(address, (err, ip) => {
if (!state.handle)
return; // Handle has been closed in the mean time

if (err) {
state.bindState = BIND_STATE_UNBOUND;
this.emit('error', err);
Expand Down Expand Up @@ -356,9 +359,6 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
this.emit('error', ex);
});
} else {
if (!state.handle)
return; // Handle has been closed in the mean time

const err = state.handle.bind(ip, port || 0, flags);
if (err) {
const ex = new ExceptionWithHostPort(err, 'bind', ip, port);
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-dgram-bind-socket-close-before-lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const dgram = require('dgram');

const socket = dgram.createSocket({
type: 'udp4',
lookup: (...args) => {
setTimeout(() => {
args[args.length - 1](new Error('an error'));
}, 1000);
}
});

socket.on('error', common.mustNotCall());
socket.bind(12345, 'localhost');
socket.close();

0 comments on commit c348000

Please sign in to comment.