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

centralize registration of exit handlers #230

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions cmd/vfkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
restvf "github.com/crc-org/vfkit/pkg/rest/vf"
"github.com/crc-org/vfkit/pkg/vf"
log "github.com/sirupsen/logrus"

"github.com/crc-org/vfkit/pkg/util"
)

func newLegacyBootloader(opts *cmdline.Options) config.Bootloader {
Expand Down Expand Up @@ -121,6 +123,8 @@ func runVFKit(vmConfig *config.VirtualMachine, opts *cmdline.Options) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

util.SetupExitSignalHandling()

gpuDevs := vmConfig.VirtioGPUDevices()
if opts.UseGUI && len(gpuDevs) > 0 {
gpuDevs[0].UsesGUI = true
Expand Down Expand Up @@ -239,6 +243,11 @@ func startIgnitionProvisionerServer(ignitionReader io.Reader, ignitionSocketPath
if err != nil {
return err
}

util.RegisterExitHandler(func() {
os.Remove(ignitionSocketPath)
})

defer func() {
if err := listener.Close(); err != nil {
log.Error(err)
Expand Down
44 changes: 44 additions & 0 deletions pkg/util/exithandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package util

import (
"log"
"os"
"os/signal"
"syscall"
)

var exitHandlers []func()

// RegisterExitHandler appends a func Exit handler to the list of handlers.
// The handlers will be invoked when vfkit receives a termination or interruption signal
//
// This method is useful when a caller wishes to execute a func before a shutdown.
func RegisterExitHandler(handler func()) {
exitHandlers = append(exitHandlers, handler)
}

// SetupExitSignalHandling sets up a signal channel to listen for termination or interruption signals.
// When one of these signals is received, all the registered exit handlers will be invoked, just
// before terminating the program.
func SetupExitSignalHandling() {
setupExitSignalHandling(true)
}

// setupExitSignalHandling sets up a signal channel to listen for termination or interruption signals.
// When one of these signals is received, all the registered exit handlers will be invoked.
// It is possible to prevent the program from exiting by setting the doExit param to false (used for testing)
func setupExitSignalHandling(doExit bool) {
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
for sig := range sigChan {
log.Printf("captured %v, calling exit handlers and exiting..", sig)
for _, handler := range exitHandlers {
handler()
}
if doExit {
os.Exit(1)
}
}
}()
}
29 changes: 29 additions & 0 deletions pkg/util/exithandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package util

import (
"syscall"
"testing"
"time"
)

func TestExitHandlerCalled(t *testing.T) {
setupExitSignalHandling(false)

ch := make(chan struct{})
RegisterExitHandler(func() {
close(ch)
})

err := syscall.Kill(syscall.Getpid(), syscall.SIGINT)

if err != nil {
t.Errorf("failed at sending SIGINT signal")
}

select {
case <-ch:
// exit handler was called
case <-time.After(5 * time.Second):
t.Errorf("Exit handler not called - timed out")
}
}
16 changes: 2 additions & 14 deletions pkg/vf/virtionet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"math/rand"
"net"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/crc-org/vfkit/pkg/config"
"github.com/crc-org/vfkit/pkg/util"

"github.com/Code-Hex/vz/v3"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -102,7 +102,7 @@ func (dev *VirtioNet) connectUnixPath() error {
dev.Socket = fd
dev.localAddr = &localAddr
dev.UnixSocketPath = ""
registerExitHandler(func() { _ = dev.Shutdown() })
util.RegisterExitHandler(func() { _ = dev.Shutdown() })
return nil
}

Expand Down Expand Up @@ -173,15 +173,3 @@ func (dev *VirtioNet) Shutdown() error {

return nil
}

func registerExitHandler(handler func()) {
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
for sig := range sigChan {
log.Printf("captured %v, calling exit handlers and exiting..", sig)
handler()
os.Exit(1)
}
}()
}