Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: ensure conn is always non-nil #184

Merged
merged 5 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions pkg/proxy/net/packetio.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ func (p *PacketIO) Flush() error {
}

func (p *PacketIO) GracefulClose() error {
if p.conn != nil {
return p.conn.SetDeadline(time.Now())
if err := p.conn.SetDeadline(time.Now()); err != nil && !errors.Is(err, net.ErrClosed) {
return err
}
return nil
}
Expand All @@ -276,11 +276,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...))
}
20 changes: 20 additions & 0 deletions pkg/proxy/net/packetio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "")
},
1,
)
}