From 73d9480f8a4b100712365457e06fbbd0a3a39254 Mon Sep 17 00:00:00 2001 From: cs010 Date: Sun, 22 May 2022 16:09:20 +0800 Subject: [PATCH 1/3] fix, unix socket bind should be only used in passive mode --- internal/socket/unix_socket.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/socket/unix_socket.go b/internal/socket/unix_socket.go index 2c3feaa00..7ed119cb5 100644 --- a/internal/socket/unix_socket.go +++ b/internal/socket/unix_socket.go @@ -71,11 +71,11 @@ func udsSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne } } - if err = os.NewSyscallError("bind", unix.Bind(fd, sa)); err != nil { - return - } - if passive { + if err = os.NewSyscallError("bind", unix.Bind(fd, sa)); err != nil { + return + } + // Set backlog size to the maximum. err = os.NewSyscallError("listen", unix.Listen(fd, listenerBacklogMaxSize)) } else { From 483cff91707f64a806db193485619a1826c2dc48 Mon Sep 17 00:00:00 2001 From: cs010 Date: Sun, 22 May 2022 18:18:15 +0800 Subject: [PATCH 2/3] fix, socket bind should be only used in passive mode, and don't close fd for EINPROGRESS error when connecting --- internal/socket/tcp_socket.go | 11 +++++++---- internal/socket/udp_socket.go | 8 ++++++-- internal/socket/unix_socket.go | 5 +++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/socket/tcp_socket.go b/internal/socket/tcp_socket.go index ad5ea0df3..34a4f4a97 100644 --- a/internal/socket/tcp_socket.go +++ b/internal/socket/tcp_socket.go @@ -123,6 +123,10 @@ func tcpSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne return } defer func() { + // ignore EINPROGRESS for non-blocking socket connect, should be processed by caller + if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS { + return + } if err != nil { _ = unix.Close(fd) } @@ -140,11 +144,10 @@ func tcpSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne } } - if err = os.NewSyscallError("bind", unix.Bind(fd, sa)); err != nil { - return - } - if passive { + if err = os.NewSyscallError("bind", unix.Bind(fd, sa)); err != nil { + return + } // Set backlog size to the maximum. err = os.NewSyscallError("listen", unix.Listen(fd, listenerBacklogMaxSize)) } else { diff --git a/internal/socket/udp_socket.go b/internal/socket/udp_socket.go index 5f9ac74ff..8620ebb79 100644 --- a/internal/socket/udp_socket.go +++ b/internal/socket/udp_socket.go @@ -121,6 +121,10 @@ func udpSocket(proto, addr string, connect bool, sockOpts ...Option) (fd int, ne return } defer func() { + // ignore EINPROGRESS for non-blocking socket connect, should be processed by caller + if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS { + return + } if err != nil { _ = unix.Close(fd) } @@ -143,10 +147,10 @@ func udpSocket(proto, addr string, connect bool, sockOpts ...Option) (fd int, ne } } - err = os.NewSyscallError("bind", unix.Bind(fd, sa)) - if connect { err = os.NewSyscallError("connect", unix.Connect(fd, sa)) + } else { + err = os.NewSyscallError("bind", unix.Bind(fd, sa)) } return diff --git a/internal/socket/unix_socket.go b/internal/socket/unix_socket.go index 7ed119cb5..2c84bd017 100644 --- a/internal/socket/unix_socket.go +++ b/internal/socket/unix_socket.go @@ -60,6 +60,11 @@ func udsSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne return } defer func() { + // ignore EINPROGRESS for non-blocking socket connect, should be processed by caller + // though there is less situation for EINPROGRESS when using unix socket + if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS { + return + } if err != nil { _ = unix.Close(fd) } From 4331027618f71e80c27323398e8dcf71f1060acd Mon Sep 17 00:00:00 2001 From: cs010 Date: Mon, 23 May 2022 12:31:41 +0800 Subject: [PATCH 3/3] move unix.EINPROGRESS condition to err nil block --- internal/socket/tcp_socket.go | 6 +++--- internal/socket/udp_socket.go | 6 +++--- internal/socket/unix_socket.go | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/socket/tcp_socket.go b/internal/socket/tcp_socket.go index 34a4f4a97..b29a075b8 100644 --- a/internal/socket/tcp_socket.go +++ b/internal/socket/tcp_socket.go @@ -124,10 +124,10 @@ func tcpSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne } defer func() { // ignore EINPROGRESS for non-blocking socket connect, should be processed by caller - if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS { - return - } if err != nil { + if err, ok := err.(*os.SyscallError); ok && err.Err == unix.EINPROGRESS { + return + } _ = unix.Close(fd) } }() diff --git a/internal/socket/udp_socket.go b/internal/socket/udp_socket.go index 8620ebb79..ff24c9eb3 100644 --- a/internal/socket/udp_socket.go +++ b/internal/socket/udp_socket.go @@ -122,10 +122,10 @@ func udpSocket(proto, addr string, connect bool, sockOpts ...Option) (fd int, ne } defer func() { // ignore EINPROGRESS for non-blocking socket connect, should be processed by caller - if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS { - return - } if err != nil { + if err, ok := err.(*os.SyscallError); ok && err.Err == unix.EINPROGRESS { + return + } _ = unix.Close(fd) } }() diff --git a/internal/socket/unix_socket.go b/internal/socket/unix_socket.go index 2c84bd017..3c1a31e54 100644 --- a/internal/socket/unix_socket.go +++ b/internal/socket/unix_socket.go @@ -62,10 +62,10 @@ func udsSocket(proto, addr string, passive bool, sockOpts ...Option) (fd int, ne defer func() { // ignore EINPROGRESS for non-blocking socket connect, should be processed by caller // though there is less situation for EINPROGRESS when using unix socket - if t, ok := err.(*os.SyscallError); ok && t.Err == unix.EINPROGRESS { - return - } if err != nil { + if err, ok := err.(*os.SyscallError); ok && err.Err == unix.EINPROGRESS { + return + } _ = unix.Close(fd) } }()