From c9b4f8306a9b5d025961fc7f58ef762eaaf73964 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Tue, 5 Dec 2017 09:48:09 -0500 Subject: [PATCH 1/2] Ensure VPNKit process is properly killed when errors occur The log.Fatal* calls will leak the vpnkit process since defer functions are not invoked when os.Exit(int) is invoked We register an ExitHandler with logrus - that'll be invoke when log.Fatal* is called Signed-off-by: Steve Hiehn Signed-off-by: Dave Protasowski --- src/cmd/linuxkit/run_hyperkit.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/cmd/linuxkit/run_hyperkit.go b/src/cmd/linuxkit/run_hyperkit.go index 047c35406..404d80fbf 100644 --- a/src/cmd/linuxkit/run_hyperkit.go +++ b/src/cmd/linuxkit/run_hyperkit.go @@ -240,14 +240,10 @@ func runHyperKit(args []string) { if err != nil { log.Fatalln("Unable to start vpnkit: ", err) } - defer func() { - if vpnkitProcess != nil { - err := vpnkitProcess.Kill() - if err != nil { - log.Println(err) - } - } - }() + defer shutdownVPNKit(vpnkitProcess) + log.RegisterExitHandler(func() { + shutdownVPNKit(vpnkitProcess) + }) // The guest will use this 9P mount to configure which ports to forward h.Sockets9P = []hyperkit.Socket9P{{Path: vpnkitPortSocket, Tag: "port"}} // VSOCK port 62373 is used to pass traffic from host->guest @@ -306,6 +302,16 @@ func runHyperKit(args []string) { } } +func shutdownVPNKit(process *os.Process) { + if process == nil { + return + } + + if err := process.Kill(); err != nil { + log.Println(err) + } +} + // createListenSocket creates a new unix domain socket and returns the open file func createListenSocket(path string) (*os.File, error) { os.Remove(path) From ec4534963801778dbee04abed020b7663c69beee Mon Sep 17 00:00:00 2001 From: Steve Hiehn Date: Tue, 5 Dec 2017 10:05:23 -0500 Subject: [PATCH 2/2] Register vpnkit publish port clean up function as a logrus.ExitHandler Signed-off-by: Dave Protasowski --- src/cmd/linuxkit/run_hyperkit.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cmd/linuxkit/run_hyperkit.go b/src/cmd/linuxkit/run_hyperkit.go index 404d80fbf..a3e846ba6 100644 --- a/src/cmd/linuxkit/run_hyperkit.go +++ b/src/cmd/linuxkit/run_hyperkit.go @@ -291,6 +291,7 @@ func runHyperKit(args []string) { log.Fatalf("Publish ports failed with: %v", err) } defer f() + log.RegisterExitHandler(f) default: log.Fatalf("Port publishing requires %q or %q networking mode", hyperkitNetworkingDockerForMac, hyperkitNetworkingVPNKit) }