Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1637 from marcov/kill-hyp
Browse files Browse the repository at this point in the history
virtcontainers: kill hypervisor if startSandbox fails
  • Loading branch information
Julio Montes authored May 23, 2019
2 parents 9a65c18 + f89834a commit 890a3d5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 47 deletions.
97 changes: 55 additions & 42 deletions virtcontainers/fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,54 @@ func (fc *firecracker) fcInit(timeout int) error {
return fc.store.Store(store.Hypervisor, fc.info)
}

func (fc *firecracker) fcEnd() (err error) {
span, _ := fc.trace("fcEnd")
defer span.Finish()

fc.Logger().Info("Stopping firecracker VM")

defer func() {
if err != nil {
fc.Logger().Info("fcEnd failed")
} else {
fc.Logger().Info("Firecracker VM stopped")
}
}()

pid := fc.info.PID

// Check if VM process is running, in case it is not, let's
// return from here.
if err = syscall.Kill(pid, syscall.Signal(0)); err != nil {
return nil
}

// Send a SIGTERM to the VM process to try to stop it properly
if err = syscall.Kill(pid, syscall.SIGTERM); err != nil {
return err
}

// Wait for the VM process to terminate
tInit := time.Now()
for {
if err = syscall.Kill(pid, syscall.Signal(0)); err != nil {
return nil
}

if time.Since(tInit).Seconds() >= fcStopSandboxTimeout {
fc.Logger().Warnf("VM still running after waiting %ds", fcStopSandboxTimeout)
break
}

// Let's avoid to run a too busy loop
time.Sleep(time.Duration(50) * time.Millisecond)
}

// Let's try with a hammer now, a SIGKILL should get rid of the
// VM process.
return syscall.Kill(pid, syscall.SIGKILL)
}

func (fc *firecracker) client() *client.Firecracker {
span, _ := fc.trace("client")
defer span.Finish()
Expand Down Expand Up @@ -370,6 +418,12 @@ func (fc *firecracker) startSandbox(timeout int) error {
return err
}

defer func() {
if err != nil {
fc.fcEnd()
}
}()

if err := fc.fcSetVMBaseConfig(int64(fc.config.MemorySize),
int64(fc.config.NumVCPUs),
false); err != nil {
Expand Down Expand Up @@ -463,48 +517,7 @@ func (fc *firecracker) stopSandbox() (err error) {
span, _ := fc.trace("stopSandbox")
defer span.Finish()

fc.Logger().Info("Stopping firecracker VM")

defer func() {
if err != nil {
fc.Logger().Info("stopSandbox failed")
} else {
fc.Logger().Info("Firecracker VM stopped")
}
}()

pid := fc.info.PID

// Check if VM process is running, in case it is not, let's
// return from here.
if err = syscall.Kill(pid, syscall.Signal(0)); err != nil {
return nil
}

// Send a SIGTERM to the VM process to try to stop it properly
if err = syscall.Kill(pid, syscall.SIGTERM); err != nil {
return err
}

// Wait for the VM process to terminate
tInit := time.Now()
for {
if err = syscall.Kill(pid, syscall.Signal(0)); err != nil {
return nil
}

if time.Since(tInit).Seconds() >= fcStopSandboxTimeout {
fc.Logger().Warnf("VM still running after waiting %ds", fcStopSandboxTimeout)
break
}

// Let's avoid to run a too busy loop
time.Sleep(time.Duration(50) * time.Millisecond)
}

// Let's try with a hammer now, a SIGKILL should get rid of the
// VM process.
return syscall.Kill(pid, syscall.SIGKILL)
return fc.fcEnd()
}

func (fc *firecracker) pauseSandbox() error {
Expand Down
7 changes: 2 additions & 5 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,11 +972,8 @@ func (s *Sandbox) startVM() (err error) {
if err != nil {
return err
}
err = vm.assignSandbox(s)
if err != nil {
return err
}
return nil

return vm.assignSandbox(s)
}

return s.hypervisor.startSandbox(vmStartTimeout)
Expand Down

0 comments on commit 890a3d5

Please sign in to comment.