{"record":{"id":"67c7460673ca3159","repo":"valyala/fasthttp","slug":"cannot-disable-nagle-s-algorithm-w","errorCode":null,"errorMessage":"cannot disable nagle's algorithm: %w","messagePattern":"cannot disable nagle's algorithm: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tcplisten/tcplisten.go","lineNumber":101,"sourceCode":"\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}\n\t}\n\n\tif cfg.FastOpen {\n\t\tif err = enableFastOpen(fd); err != nil {\n\t\t\treturn err\n\t\t}","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/tcplisten/tcplisten.go#L83-L119","documentation":"fdSetup set TCP_NODELAY on the listening socket (inherited by accepted connections to disable Nagle's algorithm) and setsockopt returned an error. The library treats this as fatal because low-latency behavior is part of its contract; users wanting Nagle can re-enable it later via TCPConn.SetNoDelay(false).","triggerScenarios":"Calling NewListener with network \"tcp4\"/\"tcp6\" (IPPROTO_TCP level) where setsockopt(TCP_NODELAY) fails: ENOPROTOOPT (stack lacking the option), EBADF, EPERM in sandboxes.","commonSituations":"Non-TCP socket families accidentally routed through fdSetup; minimal/exotic TCP stacks (embedded, some BSD variants, older z/OS) lacking TCP_NODELAY; restricted container policies.","solutions":["Check the wrapped errno; ENOPROTOOPT means the platform lacks TCP_NODELAY","Ensure the network is \"tcp\"/\"tcp4\"/\"tcp6\" so the IPPROTO_TCP option is valid","Relax sandbox/seccomp restrictions on setsockopt","Vendor/patch the library to skip TCP_NODELAY if your platform does not support it"],"exampleFix":"// before\nln, err := cfg.NewListener(\"tcp\", \":8080\")\n// after (callers re-enabling Nagle per-connection)\nln, err := cfg.NewListener(\"tcp\", \":8080\")\n// per conn:\n// conn.(*net.TCPConn).SetNoDelay(false)","handlingStrategy":"try-catch","validationCode":"// verify TCP_NODELAY support before enabling the listener\ns, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)\nif err == nil {\n    if serr := syscall.SetsockoptInt(s, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1); serr != nil {\n        log.Printf(\"TCP_NODELAY unsupported here: %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 disable nagle\") {\n        var errno syscall.Errno\n        if errors.As(err, &errno) && errno == syscall.ENOPROTOOPT {\n            log.Printf(\"TCP_NODELAY unsupported on this stack; proceeding without\")\n            // vendor a patched fdSetup that tolerates ENOPROTOOPT\n        }\n        return err\n    }\n}","preventionTips":["Only pass \"tcp\"/\"tcp4\"/\"tcp6\" networks to NewListener","Verify platform support for TCP_NODELAY in CI on the target OS","Re-enable Nagle per-connection with TCPConn.SetNoDelay(false) instead of patching the library","Include setsockopt permissions in sandbox/seccomp profiles"],"tags":["network","tcp","nagle","setsockopt"],"backgroundTag":"setsockopt-failed","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}