diff --git a/pkg/apiserver/profiling/service.go b/pkg/apiserver/profiling/service.go index a34246f1bf..4fa56e1178 100644 --- a/pkg/apiserver/profiling/service.go +++ b/pkg/apiserver/profiling/service.go @@ -16,7 +16,6 @@ import ( "github.com/pingcap/tidb-dashboard/pkg/apiserver/model" "github.com/pingcap/tidb-dashboard/pkg/config" "github.com/pingcap/tidb-dashboard/pkg/dbstore" - "github.com/pingcap/tidb-dashboard/pkg/httpc" "github.com/pingcap/tidb-dashboard/pkg/pd" ) @@ -50,7 +49,6 @@ type ServiceParams struct { ConfigManager *config.DynamicConfigManager LocalStore *dbstore.DB - HTTPClient *httpc.Client EtcdClient *clientv3.Client PDClient *pd.Client } diff --git a/pkg/apiserver/statement/queries.go b/pkg/apiserver/statement/queries.go index a63637cbef..d92c67b51f 100644 --- a/pkg/apiserver/statement/queries.go +++ b/pkg/apiserver/statement/queries.go @@ -3,6 +3,7 @@ package statement import ( + "context" "fmt" "regexp" "strings" @@ -89,8 +90,9 @@ func (s *Service) queryStatements( ) } } - - err = query.Find(&result).Error + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) + defer cancel() + err = query.WithContext(ctx).Find(&result).Error return } diff --git a/pkg/httpc/client.go b/pkg/httpc/client.go index 5da43891dd..cae0c70184 100644 --- a/pkg/httpc/client.go +++ b/pkg/httpc/client.go @@ -88,7 +88,8 @@ func (c *Client) SendRequest( if err != nil { return nil, err } - return res.Body() + defer res.Response.Body.Close() + return io.ReadAll(res.Response.Body) } func (c *Client) Send( diff --git a/pkg/tidb/client.go b/pkg/tidb/client.go index a670b9a2e2..a63a3abe9d 100644 --- a/pkg/tidb/client.go +++ b/pkg/tidb/client.go @@ -163,6 +163,11 @@ func (c *Client) OpenSQLConn(user string, pass string) (*gorm.DB, error) { if err := db.Exec(fmt.Sprintf("SET SESSION max_execution_time = '%d'", defaultTiDBSQLExecutionTimeoutMs)).Error; err != nil { log.Error("Failed to set max_execution_time", zap.Error(err)) + if d, err := db.DB(); err == nil && db != nil { + if cerr := d.Close(); cerr != nil { + log.Error("Failed to close database after setting max_execution_time", zap.Error(cerr)) + } + } return nil, ErrTiDBClientRequestFailed.Wrap(err, "failed to set max_execution_time") } diff --git a/pkg/tidb/proxy.go b/pkg/tidb/proxy.go index 2b2fa8db58..7be2faa43a 100644 --- a/pkg/tidb/proxy.go +++ b/pkg/tidb/proxy.go @@ -111,15 +111,29 @@ func (p *proxy) serve(in net.Conn) { _ = in.Close() return } + deadline := time.Now().Add(10 * time.Minute) + if err := in.SetDeadline(deadline); err != nil { + log.Warn("input set deadline failed", zap.Error(err)) + _ = in.Close() + return + } + if err := out.SetReadDeadline(deadline); err != nil { + log.Warn("output set deadline failed", zap.Error(err)) + _ = in.Close() + return + } + done := make(chan struct{}) // bidirectional copy go func() { + defer func() { + done <- struct{}{} + }() // nolint io.Copy(in, out) - _ = in.Close() - _ = out.Close() }() // nolint io.Copy(out, in) + <-done _ = out.Close() _ = in.Close() }