Skip to content

Commit 97c5364

Browse files
Merge pull request #283 from evidolob/fix-exithadler
Call exit handlers when process exits normally
2 parents 894d684 + 3bef45e commit 97c5364

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

cmd/vfkit/root.go

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66

77
"github.com/crc-org/vfkit/pkg/cmdline"
8+
"github.com/crc-org/vfkit/pkg/util"
89
"github.com/sirupsen/logrus"
910
"github.com/spf13/cobra"
1011
)
@@ -28,6 +29,9 @@ var rootCmd = &cobra.Command{
2829
if err != nil {
2930
return err
3031
}
32+
// if vfkit stop execution by itself, i.e. when VM stops by guest OS
33+
// we need to call ExecuteExitHandlers to clean up
34+
defer util.ExecuteExitHandlers()
3135
return runVFKit(vmConfig, opts)
3236
},
3337
Version: cmdline.Version(),

pkg/util/exithandler.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,20 @@ func setupExitSignalHandling(doExit bool) {
4141
go func() {
4242
for sig := range sigChan {
4343
log.Printf("captured %v, calling exit handlers and exiting..", sig)
44-
exitRegistry.mutex.Lock()
45-
for _, handler := range exitRegistry.handlers {
46-
handler()
47-
}
48-
exitRegistry.mutex.Unlock()
44+
ExecuteExitHandlers()
4945
if doExit {
5046
os.Exit(1)
5147
}
5248
}
5349
}()
5450
}
51+
52+
// ExecuteExitHandlers is call all registered exit handlers
53+
// This function should be called when program finish work(i.e. when VM is turned off by guest OS)
54+
func ExecuteExitHandlers() {
55+
exitRegistry.mutex.Lock()
56+
for _, handler := range exitRegistry.handlers {
57+
handler()
58+
}
59+
exitRegistry.mutex.Unlock()
60+
}

pkg/vf/virtionet.go

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func (dev *VirtioNet) connectUnixPath() error {
5959
if len(localSocketPath) >= maxUnixgramPathLen {
6060
return fmt.Errorf("unixgram path '%s' is too long: %d >= %d bytes", localSocketPath, len(localSocketPath), maxUnixgramPathLen)
6161
}
62-
// FIXME: need to remove localSocketPath at process exit
6362
localAddr := net.UnixAddr{
6463
Name: localSocketPath,
6564
Net: "unixgram",

0 commit comments

Comments
 (0)