valyala/fasthttp · error

cannot make non-blocked listening socket: %w

Error message

cannot make non-blocked listening socket: %w

What it means

newSocketCloexecOld successfully created a listening socket file descriptor via unix.Socket, but the follow-up unix.SetNonblock(fd, true) call failed. The library closes the fd and returns this error wrapping the underlying errno. It only runs on systems where the SOCK_NONBLOCK/SOCK_CLOEXEC combined socket() flags are unsupported, so it indicates a platform-specific failure in making the just-created socket non-blocking.

Source

Thrown at tcplisten/socket.go:24

	"fmt"
	"syscall"

	"golang.org/x/sys/unix"
)

func newSocketCloexecOld(domain, typ, proto int) (int, error) {
	syscall.ForkLock.RLock()
	fd, err := unix.Socket(domain, typ, proto)
	if err == nil {
		unix.CloseOnExec(fd)
	}
	syscall.ForkLock.RUnlock()
	if err != nil {
		return -1, fmt.Errorf("cannot create listening socket: %w", err)
	}
	if err = unix.SetNonblock(fd, true); err != nil {
		unix.Close(fd)
		return -1, fmt.Errorf("cannot make non-blocked listening socket: %w", err)
	}
	return fd, nil
}

View on GitHub (pinned to c96f600972)

Solutions

  1. Check the wrapped errno in the error string to identify the failing syscall condition (EBADF/EINVAL etc.)
  2. Raise file descriptor limits (ulimit -n) if EMFILE/ENFILE preceded the failure
  3. Check seccomp/apparmor/sandbox policies allow fcntl(F_SETFL) on sockets
  4. Update the golang.org/x/sys/unix dependency and Go toolchain; run on a kernel supporting SOCK_NONBLOCK so the fallback is never used

Example fix

// before
cfg := tcplisten.Config{ReusePort: true}
ln, err := cfg.NewListener("tcp", ":8080") // fails on fallback path
// after
if err != nil {
    var errno syscall.Errno
    if errors.As(err, &errno) {
        log.Printf("nonblock failed: %v, retrying after fd-limit check", errno)
    }
    // raise RLIMIT_NOFILE or fix sandbox policy, then retry
}
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

ln, err := cfg.NewListener("tcp", addr)
if err != nil {
    if strings.Contains(err.Error(), "cannot make non-blocked listening socket") {
        var errno syscall.Errno
        if errors.As(err, &errno) {
            log.Printf("nonblock setup failed (%v); check fd limits/sandbox", errno)
        }
    }
    return err
}

Prevention

When it happens

Trigger: Calling NewListener (e.g. cfg.NewListener("tcp", ":8080")) on a system where newSocketCloexec falls back to newSocketCloexecOld (EPROTONOSUPPORT/EINVAL from the modern socket call), and unix.SetNonblock on the resulting fd returns an error (e.g. EBADF, EINVAL).

Common situations: Running on older or unusual kernels/build tags where the fast path is unavailable; resource exhaustion (fd limits) corrupting the fd table; heavily restricted seccomp/container profiles that block fcntl operations.

Related errors


AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31). Data as JSON: /api/errors/8589f72f586568bd. Report an issue: GitHub.