Skip to content

Commit

Permalink
proxy: ensure conn is always non-nil (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Jan 11, 2023
1 parent b93030e commit dec59b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
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,
)
}

0 comments on commit dec59b4

Please sign in to comment.