From 44115f38387709dec86a9ea67b2def46420b6e93 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Sat, 6 Jun 2026 17:30:35 +0800 Subject: [PATCH] bug: file descriptor leak on z/OS s390x when FcntlInt fails (#2255) (#2285) --- tcplisten/socket_zos_s390x.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tcplisten/socket_zos_s390x.go b/tcplisten/socket_zos_s390x.go index 998baa9..8d13ae0 100644 --- a/tcplisten/socket_zos_s390x.go +++ b/tcplisten/socket_zos_s390x.go @@ -10,10 +10,18 @@ import ( func newSocketCloexec(domain, typ, proto int) (int, error) { fd, err := unix.Socket(domain, typ, proto) - _, err = unix.FcntlInt(uintptr(fd), unix.F_SETFD, unix.FD_CLOEXEC) - _, err = unix.FcntlInt(uintptr(fd), unix.F_SETFL, unix.O_NONBLOCK) - if err == nil { - return fd, nil + if err != nil { + return -1, fmt.Errorf("cannot create listening socket: %w", err) } - return -1, fmt.Errorf("cannot create listening unblocked socket: %s", err) + _, err = unix.FcntlInt(uintptr(fd), unix.F_SETFD, unix.FD_CLOEXEC) + if err != nil { + unix.Close(fd) //nolint:errcheck + return -1, fmt.Errorf("cannot mark listening socket close-on-exec: %w", err) + } + _, err = unix.FcntlInt(uintptr(fd), unix.F_SETFL, unix.O_NONBLOCK) + if err != nil { + unix.Close(fd) //nolint:errcheck + return -1, fmt.Errorf("cannot mark listening socket nonblocking: %w", err) + } + return fd, nil }