From d61c8c05a6611b2dfa54760a322867ce47719acc Mon Sep 17 00:00:00 2001 From: sawka Date: Thu, 9 Nov 2023 18:29:11 -0800 Subject: [PATCH] SIGKILL will also kill waveshell. clear PendingStateCmds and WaitingCmds on disconnect (remote.go) --- scripthaus.md | 2 +- waveshell/pkg/base/base.go | 1 - waveshell/pkg/shexec/shexec.go | 14 +++++++++++++- wavesrv/pkg/remote/remote.go | 2 ++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/scripthaus.md b/scripthaus.md index 3cba8e85..82e94236 100644 --- a/scripthaus.md +++ b/scripthaus.md @@ -84,7 +84,7 @@ CGO_ENABLED=1 go build -ldflags "-X main.BuildTime=$(date +'%Y%m%d%H%M')" -o ../ # @scripthaus command build-waveshell cd waveshell GO_LDFLAGS="-s -w -X main.BuildTime=$(date +'%Y%m%d%H%M')" -go build -ldflags="$GO_LDFLAGS" -o ../bin/mshell-v0.3-darwin.amd64 main-waveshell.go +go build -ldflags="$GO_LDFLAGS" -o ../bin/mshell/mshell-v0.3-darwin.amd64 main-waveshell.go ``` ```bash diff --git a/waveshell/pkg/base/base.go b/waveshell/pkg/base/base.go index 7782d62f..096ffb19 100644 --- a/waveshell/pkg/base/base.go +++ b/waveshell/pkg/base/base.go @@ -152,7 +152,6 @@ func (ckey CommandKey) Validate(typeStr string) error { func HasDebugFlag(envMap map[string]string, flagName string) bool { msDebug := envMap[MShellDebugVarName] flags := strings.Split(msDebug, ",") - Logf("hasdebugflag[%s]: %s [%#v]\n", flagName, msDebug, flags) for _, flag := range flags { if strings.TrimSpace(flag) == flagName { return true diff --git a/waveshell/pkg/shexec/shexec.go b/waveshell/pkg/shexec/shexec.go index 3069d368..82151231 100644 --- a/waveshell/pkg/shexec/shexec.go +++ b/waveshell/pkg/shexec/shexec.go @@ -47,6 +47,7 @@ const MaxMaxPtySize = 100 * 1024 * 1024 const MaxRunDataSize = 1024 * 1024 const MaxTotalRunDataSize = 10 * MaxRunDataSize const ShellVarName = "SHELL" +const SigKillWaitTime = 2 * time.Second const GetStateTimeout = 5 * time.Second @@ -1040,8 +1041,19 @@ trap _mshell_exittrap EXIT } func (s *ShExecType) SendSignal(sig syscall.Signal) { - base.Logf("signal start\n") + base.Logf("signal start %v\n", sig) + if sig == syscall.SIGKILL { + // SIGKILL is special, it also needs to kill waveshell if it's hanging + go func() { + wsPid := syscall.Getpid() + base.Logf("special sigkill handling waveshell-pid:%d\n", wsPid) + time.Sleep(SigKillWaitTime) + base.Logf("running self-sigkill %d\n", wsPid) + syscall.Kill(wsPid, syscall.SIGKILL) + }() + } if s.Cmd == nil || s.Cmd.Process == nil || s.IsExited() { + base.Logf("signal, no cmd or exited (exited:%v)\n", s.IsExited()) return } pgroup := false diff --git a/wavesrv/pkg/remote/remote.go b/wavesrv/pkg/remote/remote.go index e9277fe5..e89b05d4 100644 --- a/wavesrv/pkg/remote/remote.go +++ b/wavesrv/pkg/remote/remote.go @@ -1673,6 +1673,8 @@ func (msh *MShellProc) notifyHangups_nolock() { sstore.MainBus.SendScreenUpdate(ck.GetGroupId(), update) } msh.RunningCmds = make(map[base.CommandKey]RunCmdType) + msh.PendingStateCmds = make(map[pendingStateKey]base.CommandKey) + msh.WaitingCmds = nil } func (msh *MShellProc) handleCmdDonePacket(donePk *packet.CmdDonePacketType) {