From 2b71188dac94def052b447de072d881460c9deaf Mon Sep 17 00:00:00 2001 From: xhe Date: Wed, 11 Jan 2023 13:16:01 +0800 Subject: [PATCH 1/4] proxy: ensure conn is always non-nil Signed-off-by: xhe --- pkg/proxy/net/packetio.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pkg/proxy/net/packetio.go b/pkg/proxy/net/packetio.go index 0251a6cb..f4e015f4 100644 --- a/pkg/proxy/net/packetio.go +++ b/pkg/proxy/net/packetio.go @@ -251,10 +251,7 @@ func (p *PacketIO) Flush() error { } func (p *PacketIO) GracefulClose() error { - if p.conn != nil { - return p.conn.SetDeadline(time.Now()) - } - return nil + return p.conn.SetDeadline(time.Now()) } func (p *PacketIO) Close() error { @@ -265,11 +262,8 @@ func (p *PacketIO) Close() error { errs = append(errs, err) } */ - if p.conn != nil { - if err := p.conn.Close(); err != nil { - errs = append(errs, err) - } - p.conn = nil + if err := p.conn.Close(); err != nil && !errors.Is(err, net.ErrClosed) { + errs = append(errs, err) } return p.wrapErr(errors.Collect(ErrCloseConn, errs...)) } From 7e3d8361390c7a5287f7bc7cc06f3d4043c7f78b Mon Sep 17 00:00:00 2001 From: xhe Date: Wed, 11 Jan 2023 14:21:09 +0800 Subject: [PATCH 2/4] proxy: add close Signed-off-by: xhe --- pkg/proxy/net/packetio.go | 5 ++++- pkg/proxy/net/packetio_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/proxy/net/packetio.go b/pkg/proxy/net/packetio.go index 80d52621..393d4660 100644 --- a/pkg/proxy/net/packetio.go +++ b/pkg/proxy/net/packetio.go @@ -262,7 +262,10 @@ func (p *PacketIO) Flush() error { } func (p *PacketIO) GracefulClose() error { - return p.conn.SetDeadline(time.Now()) + if err := p.conn.SetDeadline(time.Now()); err != nil && !errors.Is(err, net.ErrClosed) { + return err + } + return nil } func (p *PacketIO) Close() error { diff --git a/pkg/proxy/net/packetio_test.go b/pkg/proxy/net/packetio_test.go index 54e78741..12fe96cf 100644 --- a/pkg/proxy/net/packetio_test.go +++ b/pkg/proxy/net/packetio_test.go @@ -184,3 +184,23 @@ func TestTLS(t *testing.T) { 500, // unable to reproduce stably, loop 500 times ) } + +func TestPacketIOClose(t *testing.T) { + testTCPConn(t, + func(t *testing.T, cli *PacketIO) { + require.NoError(t, cli.Close()) + require.NoError(t, cli.Close()) + require.NoError(t, cli.GracefulClose()) + require.NotEqual(t, cli.LocalAddr(), "") + require.NotEqual(t, cli.RemoteAddr(), "") + }, + func(t *testing.T, srv *PacketIO) { + require.NoError(t, srv.GracefulClose()) + require.NoError(t, srv.Close()) + require.NoError(t, srv.Close()) + require.NotEqual(t, srv.LocalAddr(), "") + require.NotEqual(t, srv.RemoteAddr(), "") + }, + 500, // unable to reproduce stably, loop 500 times + ) +} From f70cf902f7c710b960ff76b7327a13dffeab5b88 Mon Sep 17 00:00:00 2001 From: xhe Date: Wed, 11 Jan 2023 14:22:12 +0800 Subject: [PATCH 3/4] proxy: tweak test Signed-off-by: xhe --- pkg/proxy/net/packetio_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/proxy/net/packetio_test.go b/pkg/proxy/net/packetio_test.go index 12fe96cf..a8fbd00a 100644 --- a/pkg/proxy/net/packetio_test.go +++ b/pkg/proxy/net/packetio_test.go @@ -201,6 +201,6 @@ func TestPacketIOClose(t *testing.T) { require.NotEqual(t, srv.LocalAddr(), "") require.NotEqual(t, srv.RemoteAddr(), "") }, - 500, // unable to reproduce stably, loop 500 times + 1, // unable to reproduce stably, loop 500 times ) } From 1c97c4627f50558db33601136d114924b60ef6ce Mon Sep 17 00:00:00 2001 From: xhe Date: Wed, 11 Jan 2023 14:24:34 +0800 Subject: [PATCH 4/4] proxy: tweak test Signed-off-by: xhe --- pkg/proxy/net/packetio_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/proxy/net/packetio_test.go b/pkg/proxy/net/packetio_test.go index a8fbd00a..281853b8 100644 --- a/pkg/proxy/net/packetio_test.go +++ b/pkg/proxy/net/packetio_test.go @@ -201,6 +201,6 @@ func TestPacketIOClose(t *testing.T) { require.NotEqual(t, srv.LocalAddr(), "") require.NotEqual(t, srv.RemoteAddr(), "") }, - 1, // unable to reproduce stably, loop 500 times + 1, ) }