UDP socket is not running
Production Risk
Low — manage UDP socket lifecycle carefully in async code.
Thrown when an operation is attempted on a UDP (dgram) socket that has not been bound or has already been closed. Common operations like send() and addMembership() require the socket to be in a running state.
- 1Calling socket.send() before socket.bind() completes
- 2Using a socket after socket.close() has been called
- 3Race condition where send() is called before the bind callback fires
Triggered when a dgram operation is attempted on a socket that is not running.
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
socket.close(); // socket is now stopped
socket.send('data', 41234, 'localhost'); // throwsexpected output
Error [ERR_SOCKET_DGRAM_NOT_RUNNING]: Not running
Fix
Only send after binding and before closing
WHEN When managing UDP socket lifecycle
const socket = dgram.createSocket('udp4');
socket.bind(() => {
socket.send(Buffer.from('hello'), 41234, 'localhost', (err) => {
if (err) console.error(err);
socket.close();
});
});Why this works
Performing sends inside the bind callback ensures the socket is running before use.
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
socket.close(); // socket is now stopped
socket.send('data', 41234, 'localhost'); // throws // this triggers ERR_SOCKET_DGRAM_NOT_RUNNINGtry {
// operation that may throw ERR_SOCKET_DGRAM_NOT_RUNNING
riskyOperation()
} catch (err) {
if (err.code === 'ERR_SOCKET_DGRAM_NOT_RUNNING') {
console.error('ERR_SOCKET_DGRAM_NOT_RUNNING:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_socket_dgram_not_running(...args) {
// validate args here
return performOperation(...args)
}✕ Send data on a dgram socket after close()
A closed socket has no OS binding and cannot send or receive data.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev