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

session: fix the memory tracker leak in prepare binary protocol (#44618) #47703

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
28 changes: 24 additions & 4 deletions server/conn_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,21 +407,41 @@ func TestCursorDetachMemTracker(t *testing.T) {
// testkit also uses `PREPARE` related calls to run statement with arguments.
// format the SQL to avoid the interference from testkit.
tk.MustExec(fmt.Sprintf("set tidb_mem_quota_query=%d", maxConsumed/2))
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 1)
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 0)
// This query should exceed the memory limitation during `openExecutor`
require.Error(t, c.Dispatch(ctx, append(
appendUint32([]byte{mysql.ComStmtExecute}, uint32(stmt.ID())),
mysql.CursorTypeReadOnly, 0x1, 0x0, 0x0, 0x0,
)))
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 1)
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 0)

// The next query should succeed
tk.MustExec(fmt.Sprintf("set tidb_mem_quota_query=%d", maxConsumed+1))
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 1)
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 0)
// This query should succeed
require.NoError(t, c.Dispatch(ctx, append(
appendUint32([]byte{mysql.ComStmtExecute}, uint32(stmt.ID())),
mysql.CursorTypeReadOnly, 0x1, 0x0, 0x0, 0x0,
)))
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 1)
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 0)
}

func TestMemoryTrackForPrepareBinaryProtocol(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
srv := CreateMockServer(t, store)
srv.SetDomain(dom)
defer srv.Close()

c := CreateMockConn(t, srv).(*mockConn)

tk := testkit.NewTestKitWithSession(t, store, c.Context().Session)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id_2 int)")
for i := 0; i <= 10; i++ {
stmt, _, _, err := c.Context().Prepare("select count(id_2) from t")
require.NoError(t, err)
require.NoError(t, stmt.Close())
}
require.Len(t, tk.Session().GetSessionVars().MemTracker.GetChildrenForTest(), 0)
}
5 changes: 5 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,11 @@ func (s *session) CacheGeneralStmt(sql string) (interface{}, error) {

// PrepareStmt is used for executing prepare statement in binary protocol
func (s *session) PrepareStmt(sql string) (stmtID uint32, paramCount int, fields []*ast.ResultField, err error) {
defer func() {
if s.sessionVars.StmtCtx != nil {
s.sessionVars.StmtCtx.DetachMemDiskTracker()
}
}()
if s.sessionVars.TxnCtx.InfoSchema == nil {
// We don't need to create a transaction for prepare statement, just get information schema will do.
s.sessionVars.TxnCtx.InfoSchema = domain.GetDomain(s).InfoSchema()
Expand Down