hotfix: cancel ctx (#246)

This commit is contained in:
naison
2024-05-14 10:31:08 +08:00
committed by GitHub
parent 3e51bf0f4d
commit 984ab2ce89
3 changed files with 28 additions and 6 deletions

View File

@@ -11,9 +11,9 @@ import (
var CancelFunc = make(map[string]context.CancelFunc)
func (svr *Server) ConfigAdd(ctx context.Context, req *rpc.ConfigAddRequest) (*rpc.ConfigAddResponse, error) {
var sshConf = util.ParseSshFromRPC(req.SshJump)
file, err := util.ConvertToTempKubeconfigFile([]byte(req.KubeconfigBytes))
func (svr *Server) ConfigAdd(ctx context.Context, req *rpc.ConfigAddRequest) (resp *rpc.ConfigAddResponse, err error) {
var file string
file, err = util.ConvertToTempKubeconfigFile([]byte(req.KubeconfigBytes))
if err != nil {
return nil, err
}
@@ -23,13 +23,19 @@ func (svr *Server) ConfigAdd(ctx context.Context, req *rpc.ConfigAddRequest) (*r
DefValue: file,
})
sshCtx, sshCancel := context.WithCancel(context.Background())
defer func() {
if err != nil {
sshCancel()
}
}()
var path string
var sshConf = util.ParseSshFromRPC(req.SshJump)
path, err = util.SshJump(sshCtx, sshConf, flags, true)
CancelFunc[path] = sshCancel
if err != nil {
return nil, err
}
CancelFunc[path] = sshCancel
return &rpc.ConfigAddResponse{ClusterID: path}, nil
}

View File

@@ -66,6 +66,7 @@ func (svr *Server) ConnectFork(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectF
defer func() {
if err != nil {
connect.Cleanup()
sshCancel()
}
}()
@@ -98,7 +99,7 @@ func (svr *Server) ConnectFork(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectF
return nil
}
func (svr *Server) redirectConnectForkToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServer) error {
func (svr *Server) redirectConnectForkToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServer) (err error) {
cli := svr.GetClient(true)
if cli == nil {
return fmt.Errorf("sudo daemon not start")
@@ -126,6 +127,11 @@ func (svr *Server) redirectConnectForkToSudoDaemon(req *rpc.ConnectRequest, resp
sshCancel()
return nil
})
defer func() {
if err != nil {
sshCancel()
}
}()
var path string
path, err = util.SshJump(sshCtx, sshConf, flags, true)
if err != nil {

View File

@@ -83,6 +83,11 @@ func (svr *Server) Connect(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServe
sshCancel()
return nil
})
defer func() {
if e != nil {
sshCancel()
}
}()
var path string
path, err = util.SshJump(sshCtx, sshConf, flags, false)
if err != nil {
@@ -113,7 +118,7 @@ func (svr *Server) Connect(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServe
return nil
}
func (svr *Server) redirectToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServer) error {
func (svr *Server) redirectToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServer) (e error) {
cli := svr.GetClient(true)
if cli == nil {
return fmt.Errorf("sudo daemon not start")
@@ -142,6 +147,11 @@ func (svr *Server) redirectToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon
sshCancel()
return nil
})
defer func() {
if e != nil {
sshCancel()
}
}()
var path string
path, err = util.SshJump(sshCtx, sshConf, flags, true)
if err != nil {