Skip to content

udp: an error handler that sends again to a closed local port holds the event loop (Linux) #44218

Closed as not planned

Description

@robobun

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

Activity

  1. Jarred-Sumner commented on Oct 8, 2026

    @Jarred-Sumner
    Collaborator

    Use handoff tool. GitHub issues are for user-facing issues and a user did not run into this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions