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

Kill Firecracker if SendCtrlAltDel failed #177

Merged
merged 2 commits into from
Oct 29, 2021
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
29 changes: 17 additions & 12 deletions infrastructure/firecracker/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/url"
"os"

"github.com/go-openapi/strfmt"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -155,22 +156,26 @@ func (p *fcProvider) Delete(ctx context.Context, id string) error {
_, err = client.CreateSyncAction(ctx, &fcmodels.InstanceActionInfo{
ActionType: firecracker.String("SendCtrlAltDel"),
})
if err != nil {
// What errors do we want to ignore?
// Example:
// * net/url.Error happens if the VM is not running or the socket file
// is not there, so we can delete the VM.
if errors.Is(err, &url.Error{}) {
logger.Info("microvm is not running")
} else {
if err == nil {
return nil
}

// What errors do we want to ignore?
// Example:
// * net/url.Error happens if the VM is not running or the socket file
// is not there, so we can delete the VM.
if errors.Is(err, &url.Error{}) {
logger.Info("microvm is not running")
} else if pid, pidErr := vmState.PID(); pidErr != nil {
logger.Infof("sending SIGINT to %d", pid)

if sigErr := process.SendSignal(pid, os.Interrupt); sigErr != nil {
return fmt.Errorf("failed to create halt action: %w", err)
}
} else {
return fmt.Errorf("failed to create halt action: %w", err)
Comment on lines +167 to +176
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am slightly worried that this section could become a bit unwieldy the more error/check variations we add... but happy to leave it until we actually see it happening

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in both cases the result is the same, failed delete and the wrapped error will tell why.

}

// It's strange to call it delete, it terminates the vm, but by the nature of
// firecracker, if it's terminated, it's not there anymore, only the
// resources we created before, but we have steps for them.

logger.Info("deleted microvm")

return nil
Expand Down
18 changes: 18 additions & 0 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,21 @@ func Exists(pid int) (bool, error) {

return exists, nil
}

// SendSignal sends a 'sig' signal to 'pid' process.
func SendSignal(pid int, sig os.Signal) error {
proc, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("unable to find process (%d): %w", pid, err)
}

if err := proc.Signal(os.Interrupt); err != nil {
return fmt.Errorf(
"unable to send signal to process (%s): %w",
sig.String(),
err,
)
}

return nil
}