{"record":{"id":"d7ed3d581956a668","repo":"valyala/fasthttp","slug":"cannot-create-listening-socket-w","errorCode":null,"errorMessage":"cannot create listening socket: %w","messagePattern":"cannot create listening socket: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tcplisten/socket.go","lineNumber":20,"sourceCode":"\npackage tcplisten\n\nimport (\n\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":2,"sourceCodeEnd":28,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/tcplisten/socket.go#L2-L28","documentation":"Returned by newSocketCloexecOld (the fallback path of newSocketCloexec) when the raw unix.Socket(domain, typ, proto) syscall fails to create a listening socket fd. This is the low-level socket(2) call failing, before any bind/listen happens. The wrapped errno tells which resource or protocol constraint was hit.","triggerScenarios":"EMFILE/ENFILE (process or system file descriptor table full), ENOMEM/ENOBUFS (kernel memory shortage), EPROTONOSUPPORT/EAFNOSUPPORT (unsupported domain/protocol — e.g. requesting a socket family the kernel doesn't support), or EPERM under restrictive seccomp policies.","commonSituations":"Applications with thousands of open fds hitting RLIMIT_NOFILE; hosts with fs.file-max exhausted; containers with seccomp profiles blocking socket() for the requested domain; IPv6-disabled kernels when tcp6 sockets are requested.","solutions":["Raise fd limits: ulimit -n / systemd LimitNOFILE, and sysctl fs.file-max if system-wide exhaustion.","Fix fd leaks in the app (check with lsof / /proc/<pid>/fd count).","Adjust seccomp/container profiles to allow socket() for the needed address family, or enable IPv6 if tcp6 is required.","Check dmesg/kernel logs for ENOMEM/ENOBUFS and increase kernel memory or reduce load."],"exampleFix":"// before\n# default ulimit -n 1024 under heavy load\n// after\n# systemd unit\n[Service]\nLimitNOFILE=65535","handlingStrategy":"try-catch","validationCode":"// Pre-flight: try creating a socket of the same family\nfd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK, 0)\nif err != nil { log.Fatalf(\"cannot create sockets (limits/seccomp?): %v\", err) }\nsyscall.Close(fd)","typeGuard":null,"tryCatchPattern":"ln, err := s.Listen(addr)\nif err != nil {\n    if strings.Contains(err.Error(), \"cannot create listening socket\") {\n        if errors.Is(err, syscall.EMFILE) || errors.Is(err, syscall.ENFILE) {\n            log.Fatalf(\"fd limit hit, raise ulimit -n: %v\", err)\n        }\n        log.Fatalf(\"socket() blocked or unsupported: %v\", err)\n    }\n}","preventionTips":["Set RLIMIT_NOFILE and fs.file-max generously.","Fix fd leaks and monitor /proc/<pid>/fd counts.","Ensure seccomp/apparmor profiles permit socket() for needed families.","Enable IPv6 kernel support if tcp6 sockets are used."],"tags":["go","network","syscall","socket"],"backgroundTag":"socket-creation-failed","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}