Skip to content

Commit

Permalink
Merge #83335
Browse files Browse the repository at this point in the history
83335: sql/pgwire: fix a leak of accounted for memory r=rafiss a=joshimhoff

`@rafiss,` can I get a suggestion on where to add an automated test?

Fixes #83034.

**sql/pgwire: fix a leak of accounted for memory**

This commit fixes a bug where in case client-provided session params failed
to parse, CRDB leaked accounted for memory.

Release note (bug fix): In case client-provided session params fail to parse,
CRDB no longer leaks accounted for memory.

Co-authored-by: Josh Imhoff <[email protected]>
  • Loading branch information
craig[bot] and joshimhoff committed Jul 15, 2022
2 parents e9ee218 + 3f554c5 commit 902f937
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pkg/sql/pgwire/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1937,3 +1937,33 @@ func TestPGWireRejectsNewConnIfTooManyConns(t *testing.T) {
requireConnectionCount(t, 0)
})
}

func TestConnCloseReleasesReservedMem(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
ctx := context.Background()
defer s.Stopper().Stop(ctx)

before := s.PGServer().(*Server).connMonitor.AllocBytes()

pgURL, cleanupFunc := sqlutils.PGUrl(
t, s.ServingSQLAddr(), "testConnClose" /* prefix */, url.User(username.RootUser),
)
values := pgURL.Query()
values.Add("options", "c sadsad=") // invalid client-provided session param
pgURL.RawQuery = values.Encode()

defer cleanupFunc()
db, err := gosql.Open("postgres", pgURL.String())
require.NoError(t, err)
defer db.Close()

err = db.Ping()
require.Error(t, err)
require.Regexp(t, "pq: option .* is invalid", err.Error())

// Check that no accounted-for memory is leaked, after the connection attempt fails.
after := s.PGServer().(*Server).connMonitor.AllocBytes()
require.Equal(t, before, after)
}
1 change: 1 addition & 0 deletions pkg/sql/pgwire/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ func (s *Server) ServeConn(ctx context.Context, conn net.Conn, socketType Socket
var sArgs sql.SessionArgs
if sArgs, err = parseClientProvidedSessionParameters(ctx, &s.execCfg.Settings.SV, &buf,
conn.RemoteAddr(), s.trustClientProvidedRemoteAddr.Get()); err != nil {
reserved.Close(ctx)
return s.sendErr(ctx, conn, err)
}

Expand Down

0 comments on commit 902f937

Please sign in to comment.