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

fix: 解决容器终端断开后未退出进程的问题 #779

Merged
merged 1 commit into from
Apr 25, 2023
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
18 changes: 15 additions & 3 deletions backend/app/api/v1/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"strconv"
"time"

Expand Down Expand Up @@ -107,14 +108,15 @@ func (b *BaseApi) RedisWsSsh(c *gin.Context) {
return
}
defer wsConn.Close()
commands := fmt.Sprintf("docker exec -it %s redis-cli", redisConf.ContainerName)
commands := "redis-cli"
if len(redisConf.Requirepass) != 0 {
commands = fmt.Sprintf("docker exec -it %s redis-cli -a %s --no-auth-warning", redisConf.ContainerName, redisConf.Requirepass)
commands = fmt.Sprintf("redis-cli -a %s --no-auth-warning", redisConf.Requirepass)
}
slave, err := terminal.NewCommand(commands)
slave, err := terminal.NewCommand(fmt.Sprintf("docker exec -it %s %s", redisConf.ContainerName, commands))
if wshandleError(wsConn, err) {
return
}
defer killBash(redisConf.ContainerName, commands)
defer slave.Close()

tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave)
Expand Down Expand Up @@ -177,6 +179,7 @@ func (b *BaseApi) ContainerWsSsh(c *gin.Context) {
if wshandleError(wsConn, err) {
return
}
defer killBash(containerID, command)
defer slave.Close()

tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave)
Expand Down Expand Up @@ -216,6 +219,15 @@ func wshandleError(ws *websocket.Conn, err error) bool {
return false
}

func killBash(containerID, comm string) {
sudo := ""
if cmd.HasNoPasswordSudo() {
sudo = "sudo"
}
command := exec.Command("sh", "-c", fmt.Sprintf("%s kill -9 $(docker top %s -eo pid,command | grep '%s' | awk '{print $1}')", sudo, containerID, comm))
_, _ = command.CombinedOutput()
}

var upGrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024 * 1024 * 10,
Expand Down
31 changes: 7 additions & 24 deletions backend/utils/terminal/local_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ type LocalCommand struct {
closeSignal syscall.Signal
closeTimeout time.Duration

cmd *exec.Cmd
pty *os.File
ptyClosed chan struct{}
cmd *exec.Cmd
pty *os.File
}

func NewCommand(commands string) (*LocalCommand, error) {
Expand All @@ -33,15 +32,13 @@ func NewCommand(commands string) (*LocalCommand, error) {
if err != nil {
return nil, errors.Wrapf(err, "failed to start command")
}
ptyClosed := make(chan struct{})

lcmd := &LocalCommand{
closeSignal: DefaultCloseSignal,
closeTimeout: DefaultCloseTimeout,

cmd: cmd,
pty: pty,
ptyClosed: ptyClosed,
cmd: cmd,
pty: pty,
}

return lcmd, nil
Expand All @@ -57,16 +54,10 @@ func (lcmd *LocalCommand) Write(p []byte) (n int, err error) {

func (lcmd *LocalCommand) Close() error {
if lcmd.cmd != nil && lcmd.cmd.Process != nil {
_ = lcmd.cmd.Process.Signal(lcmd.closeSignal)
}
for {
select {
case <-lcmd.ptyClosed:
return nil
case <-lcmd.closeTimeoutC():
_ = lcmd.cmd.Process.Signal(syscall.SIGKILL)
}
_ = lcmd.cmd.Process.Kill()
}
_ = lcmd.pty.Close()
return nil
}

func (lcmd *LocalCommand) ResizeTerminal(width int, height int) error {
Expand Down Expand Up @@ -100,11 +91,3 @@ func (lcmd *LocalCommand) Wait(quitChan chan bool) {
setQuit(quitChan)
}
}

func (lcmd *LocalCommand) closeTimeoutC() <-chan time.Time {
if lcmd.closeTimeout >= 0 {
return time.After(lcmd.closeTimeout)
}

return make(chan time.Time)
}