Repository navigation
Description
What happens
On Linux, a UDP socket whose error handler sends again to a closed port on the same host keeps the event loop inside one readable event. No timer and no immediate runs. The process uses 100 % of one core.
The handler must send before the loop runs again: in the handler, in process.nextTick, or after an await. A send from setImmediate or from a timer does not do this. The socket is a Bun.udpSocket, or a node:dgram socket that is connected.
No user reported this. A review of the UDP dispatch found it.
Repro
// Linux. A connected UDP socket whose peer port is closed. The `error` handler
// sends again, as a client with a retry does.
const dgram = require("node:dgram");
const probe = dgram.createSocket("udp4");
probe.bind(0, "127.0.0.1", () => {
const port = probe.address().port;
probe.close(() => {
const socket = dgram.createSocket("udp4");
let errors = 0;
let immediates = 0;
let timerFired = false;
socket.on("error", () => {
if (++errors === 1) {
setImmediate(() => immediates++);
setTimeout(() => (timerFired = true), 1);
}
if (errors === 100000) {
console.log(`${errors} 'error' events; setImmediate ran ${immediates} times; the 1 ms timer fired: ${timerFired}`);
process.exit(1);
}
socket.send("retry");
});
socket.connect(port, "127.0.0.1", () => {
socket.send("first");
let turns = 0;
(function turn() {
if (++turns === 200) {
console.log(`200 loop turns; ${errors} 'error' events; the 1 ms timer fired: ${timerFired}`);
process.exit(0);
}
setImmediate(turn);
})();
});
});
});| Runtime | Output |
|---|---|
| Bun main a4f1429 (release build, Linux x64, kernel 7.0.0) | 100000 'error' events; setImmediate ran 0 times; the 1 ms timer fired: false |
| Node v26.3.0 | 200 loop turns; 199 'error' events; the 1 ms timer fired: true |
Without the bail-out at 100000 the Bun process does not end.
Cause
The UDP case of us_internal_dispatch_ready_poll (packages/bun-usockets/src/loop.c) drains the socket's error queue before it reads data:
while (!u->closed) {
...
if (recvmsg(us_poll_fd(p), &eh, MSG_ERRQUEUE) < 0) break;
recv_error_surfaced = 1;
if (u->on_recv_error) {
...
u->on_recv_error(u, ee ? ee : ECONNREFUSED, 1);
}
}Over loopback the ICMP error of a send is queued before send() returns. So the report of the handler's send is on the queue before the next recvmsg(MSG_ERRQUEUE), and the loop never finds the queue empty.
What does not work
A count in front of the recvmsg (for example 32 reports per event, then leave the rest for the next event) stops the loop but is wrong. The kernel charges queued reports to the receive buffer of the socket. With more than 32 reports per loop iteration the backlog fills the buffer and the kernel drops inbound datagrams that main delivers:
| 64 sends to a closed port and 16 inbound datagrams per iteration, 64 iterations | Datagrams received |
|---|---|
| main | 1024 of 1024 |
| count of 32 in front of the dequeue | 80 of 1024 |
Such a count also has to read SO_ERROR when it stops, or the recvmmsg that follows fails with the errno of a report that is still queued. That read discards an error for which the kernel had no room to queue a report.
A shape that avoids both: take every report off the queue first, with no handler call in between, then call the handler. The reports that the handlers cause then belong to the next event.
Related
- usockets(udp): bound a readable event at 32 datagrams #37103 bounds the data loop of the same case. It leaves this drain as it is, for the reasons above.
- CPU 100% on UDP error !!!! #29436 was a different problem in the same code (EPOLLERR stayed raised because nothing drained the error queue).
Activity
Jarred-Sumner commentedon Oct 8, 2026
Use handoff tool. GitHub issues are for user-facing issues and a user did not run into this issue