diff --git a/cli/kill.go b/cli/kill.go index bb5e7a795..645e231c6 100644 --- a/cli/kill.go +++ b/cli/kill.go @@ -129,13 +129,15 @@ func kill(ctx context.Context, containerID, signal string, all bool) error { return err } - // container MUST be created, running or paused - if status.State.State != vc.StateReady && status.State.State != vc.StateRunning && status.State.State != vc.StatePaused { - return fmt.Errorf("Container %s not ready, running or paused, cannot send a signal", containerID) - } + kataLog.WithField("signal", signal).WithField("container state", status.State.State).Info("kill") - if err := vci.KillContainer(ctx, sandboxID, containerID, signum, all); err != nil { - return err + // container MUST be created, running or paused + if status.State.State == vc.StateReady || status.State.State == vc.StateRunning || status.State.State == vc.StatePaused { + if err := vci.KillContainer(ctx, sandboxID, containerID, signum, all); err != nil { + return err + } + } else if !all { + return fmt.Errorf("container not running") } if signum != syscall.SIGKILL && signum != syscall.SIGTERM { diff --git a/cli/kill_test.go b/cli/kill_test.go index 470020fa1..41094fce3 100644 --- a/cli/kill_test.go +++ b/cli/kill_test.go @@ -396,3 +396,33 @@ func TestKillCLIFunctionKillContainerFailure(t *testing.T) { execCLICommandFunc(assert, killCLICommand, set, true) } + +func TestKillCLIFunctionInvalidStateStoppedAllSuccess(t *testing.T) { + assert := assert.New(t) + + state := vc.State{ + State: vc.StateStopped, + } + + testingImpl.KillContainerFunc = testKillContainerFuncReturnNil + + path, err := createTempContainerIDMapping(testContainerID, testSandboxID) + assert.NoError(err) + defer os.RemoveAll(path) + + testingImpl.StatusContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) { + return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil + } + + defer func() { + testingImpl.KillContainerFunc = nil + testingImpl.StatusContainerFunc = nil + }() + + set := flag.NewFlagSet("", 0) + var all bool + set.BoolVar(&all, "all", false, "") + set.Parse([]string{"-all", testContainerID, "10"}) + + execCLICommandFunc(assert, killCLICommand, set, false) +} diff --git a/virtcontainers/container.go b/virtcontainers/container.go index fa645169b..de593134c 100644 --- a/virtcontainers/container.go +++ b/virtcontainers/container.go @@ -404,6 +404,7 @@ func (c *Container) setContainerState(state stateString) error { return errNeedState } + c.Logger().Debugf("Setting container state from %v to %v", c.state.State, state) // update in-memory state c.state.State = state diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 6a0c45c28..eab398ac2 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -1591,6 +1591,11 @@ func (s *Sandbox) Stop() error { span, _ := s.trace("stop") defer span.Finish() + if s.state.State == StateStopped { + s.Logger().Info("sandbox already stopped") + return nil + } + if err := s.state.validTransition(s.state.State, StateStopped); err != nil { return err } diff --git a/virtcontainers/sandbox_test.go b/virtcontainers/sandbox_test.go index 08604efb7..8503a9a15 100644 --- a/virtcontainers/sandbox_test.go +++ b/virtcontainers/sandbox_test.go @@ -1754,3 +1754,10 @@ func TestStartNetworkMonitor(t *testing.T) { err = s.startNetworkMonitor() assert.Nil(t, err) } + +func TestSandboxStopStopped(t *testing.T) { + s := &Sandbox{state: State{State: StateStopped}} + err := s.Stop() + + assert.Nil(t, err) +}