{"record":{"id":"a8b32d60b9b2161e","repo":"valyala/fasthttp","slug":"cannot-enable-so-reuseaddr-w","errorCode":null,"errorMessage":"cannot enable so_reuseaddr: %w","messagePattern":"cannot enable so_reuseaddr: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tcplisten/tcplisten.go","lineNumber":95,"sourceCode":"\tln, err := net.FileListener(file)\n\tif err != nil {\n\t\tfile.Close()\n\t\treturn nil, err\n\t}\n\n\tif err = file.Close(); err != nil {\n\t\tln.Close()\n\t\treturn nil, err\n\t}\n\n\treturn ln, nil\n}\n\nfunc (cfg *Config) fdSetup(fd int, sa unix.Sockaddr, addr string) error {\n\tvar err error\n\n\tif err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1); err != nil {\n\t\treturn fmt.Errorf(\"cannot enable so_reuseaddr: %w\", err)\n\t}\n\n\t// This should disable Nagle's algorithm in all accepted sockets by default.\n\t// Users may enable it with net.TCPConn.SetNoDelay(false).\n\tif err = unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_NODELAY, 1); err != nil {\n\t\treturn fmt.Errorf(\"cannot disable nagle's algorithm: %w\", err)\n\t}\n\n\tif cfg.ReusePort {\n\t\tif err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, soReusePort, 1); err != nil {\n\t\t\treturn fmt.Errorf(\"cannot enable so_reuseport: %w\", err)\n\t\t}\n\t}\n\n\tif cfg.DeferAccept {\n\t\tif err = enableDeferAccept(fd); err != nil {\n\t\t\treturn err\n\t\t}","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/tcplisten/tcplisten.go#L77-L113","documentation":"During fdSetup, setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 1) failed. The library always enables SO_REUSEADDR on listening sockets so the port can be rebound quickly after restart, and treats failure to do so as fatal, aborting NewListener.","triggerScenarios":"Calling NewListener where setsockopt(SO_REUSEADDR) returns an error: EBADF (invalid fd from earlier steps), ENOPROTOOPT (stack without the option), EPERM (restricted sandbox), or ENOMEM.","commonSituations":"Hardened seccomp/apparmor containers blocking setsockopt; exotic network stacks or OSes missing SO_REUSEADDR; fd corruption from a failed earlier setup step.","solutions":["Read the wrapped errno: EBADF points to an earlier failure, ENOPROTOOPT to a missing option","Relax container/seccomp policies to allow setsockopt(SOL_SOCKET, SO_REUSEADDR)","Retry on a different OS/kernel that supports SO_REUSEADDR for the chosen family","If unavoidable on the target platform, patch/vendor the library to tolerate SO_REUSEADDR failure"],"exampleFix":"// before\nln, err := cfg.NewListener(\"tcp\", \":8080\") // fails in seccomp sandbox\n// after\n// Docker: add --cap-add NET_ADMIN and/or adjust seccomp to allow setsockopt\n// docker run --security-opt seccomp=unconfined ... (or a profile allowing setsockopt)","handlingStrategy":"validation","validationCode":"// preflight: ensure setsockopt on a probe socket succeeds in this sandbox\ns, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)\nif err == nil {\n    if serr := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); serr != nil {\n        syscall.Close(s)\n        log.Fatalf(\"sandbox blocks SO_REUSEADDR: %v\", serr)\n    }\n    syscall.Close(s)\n}","typeGuard":"null","tryCatchPattern":"ln, err := cfg.NewListener(\"tcp\", addr)\nif err != nil {\n    if strings.Contains(err.Error(), \"cannot enable so_reuseaddr\") {\n        var errno syscall.Errno\n        if errors.As(err, &errno) {\n            log.Printf(\"SO_REUSEADDR blocked (%v): adjust seccomp profile\", errno)\n        }\n        return err\n    }\n}","preventionTips":["Run a socket preflight check in containers before production rollout","Grant containers the capabilities needed for socket options (seccomp profile)","Avoid exotic/minimal network stacks missing SOL_SOCKET options","Log the wrapped errno to distinguish sandbox blocks from stack limitations"],"tags":["network","sockets","setsockopt","reuseaddr"],"backgroundTag":"setsockopt-failed","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}