{"record":{"id":"8589f72f586568bd","repo":"valyala/fasthttp","slug":"cannot-make-non-blocked-listening-socket-w","errorCode":null,"errorMessage":"cannot make non-blocked listening socket: %w","messagePattern":"cannot make non-blocked listening socket: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tcplisten/socket.go","lineNumber":24,"sourceCode":"\t\"fmt\"\n\t\"syscall\"\n\n\t\"golang.org/x/sys/unix\"\n)\n\nfunc newSocketCloexecOld(domain, typ, proto int) (int, error) {\n\tsyscall.ForkLock.RLock()\n\tfd, err := unix.Socket(domain, typ, proto)\n\tif err == nil {\n\t\tunix.CloseOnExec(fd)\n\t}\n\tsyscall.ForkLock.RUnlock()\n\tif err != nil {\n\t\treturn -1, fmt.Errorf(\"cannot create listening socket: %w\", err)\n\t}\n\tif err = unix.SetNonblock(fd, true); err != nil {\n\t\tunix.Close(fd)\n\t\treturn -1, fmt.Errorf(\"cannot make non-blocked listening socket: %w\", err)\n\t}\n\treturn fd, nil\n}\n","sourceCodeStart":6,"sourceCodeEnd":28,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/tcplisten/socket.go#L6-L28","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check the wrapped errno in the error string to identify the failing syscall condition (EBADF/EINVAL etc.)","Raise file descriptor limits (ulimit -n) if EMFILE/ENFILE preceded the failure","Check seccomp/apparmor/sandbox policies allow fcntl(F_SETFL) on sockets","Update the golang.org/x/sys/unix dependency and Go toolchain; run on a kernel supporting SOCK_NONBLOCK so the fallback is never used"],"exampleFix":"// before\ncfg := tcplisten.Config{ReusePort: true}\nln, err := cfg.NewListener(\"tcp\", \":8080\") // fails on fallback path\n// after\nif err != nil {\n    var errno syscall.Errno\n    if errors.As(err, &errno) {\n        log.Printf(\"nonblock failed: %v, retrying after fd-limit check\", errno)\n    }\n    // raise RLIMIT_NOFILE or fix sandbox policy, then retry\n}","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"ln, err := cfg.NewListener(\"tcp\", addr)\nif err != nil {\n    if strings.Contains(err.Error(), \"cannot make non-blocked listening socket\") {\n        var errno syscall.Errno\n        if errors.As(err, &errno) {\n            log.Printf(\"nonblock setup failed (%v); check fd limits/sandbox\", errno)\n        }\n    }\n    return err\n}","preventionTips":["Run on kernels supporting SOCK_NONBLOCK|SOCK_CLOEXEC so the legacy fallback is never used","Keep RLIMIT_NOFILE comfortably above expected fd usage","Audit seccomp/apparmor profiles to permit fcntl(F_SETFL) on sockets","Keep golang.org/x/sys updated"],"tags":["network","sockets","syscall","nonblocking"],"backgroundTag":"socket-nonblock-failed","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}