Skip to content

Commit e2478a0

Browse files
committed
refactor(vsock): call proxy.Close when vm stop
When other projects use the `vf.ExposeVsock` method, there will be unexpected issues due to the absence of a close proxy. Signed-off-by: Black-Hole1 <[email protected]>
1 parent 7572104 commit e2478a0

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

cmd/vfkit/main.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package main
2222
import (
2323
"errors"
2424
"fmt"
25+
"io"
2526
"os"
2627
"os/signal"
2728
"runtime"
@@ -156,7 +157,9 @@ func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) e
156157
}
157158
log.Infof("virtual machine is running")
158159

159-
for _, vsock := range vmConfig.VirtioVsockDevices() {
160+
vsockDevs := vmConfig.VirtioVsockDevices()
161+
closes := make([]io.Closer, 0, len(vsockDevs))
162+
for _, vsock := range vsockDevs {
160163
port := vsock.Port
161164
socketURL := vsock.SocketURL
162165
if socketURL == "" {
@@ -168,10 +171,17 @@ func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) e
168171
listenStr = " (listening)"
169172
}
170173
log.Infof("Exposing vsock port %d on %s%s", port, socketURL, listenStr)
171-
if err := vf.ExposeVsock(vm, port, socketURL, vsock.Listen); err != nil {
174+
if closer, err := vf.ExposeVsock(vm, port, socketURL, vsock.Listen); err != nil {
172175
log.Warnf("error exposing vsock port %d: %v", port, err)
176+
} else {
177+
closes = append(closes, closer)
173178
}
174179
}
180+
defer func() {
181+
for _, c := range closes {
182+
_ = c.Close()
183+
}
184+
}()
175185

176186
if err := setupGuestTimeSync(vm, vmConfig.TimeSync()); err != nil {
177187
log.Warnf("Error configuring guest time synchronization")

pkg/vf/vsock.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vf
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"net"
78
"net/url"
89
"strconv"
@@ -11,7 +12,7 @@ import (
1112
"inet.af/tcpproxy"
1213
)
1314

14-
func ExposeVsock(vm *vz.VirtualMachine, port uint, vsockPath string, listen bool) error {
15+
func ExposeVsock(vm *vz.VirtualMachine, port uint, vsockPath string, listen bool) (io.Closer, error) {
1516
if listen {
1617
return listenVsock(vm, port, vsockPath)
1718
}
@@ -36,7 +37,7 @@ func ConnectVsockSync(vm *vz.VirtualMachine, port uint) (net.Conn, error) {
3637

3738
// connectVsock proxies connections from a host unix socket to a vsock port
3839
// This allows the host to initiate connections to the guest over vsock
39-
func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
40+
func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) (io.Closer, error) {
4041

4142
var proxy tcpproxy.Proxy
4243
// listen for connections on the host unix socket
@@ -70,12 +71,12 @@ func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
7071
}
7172
},
7273
})
73-
return proxy.Start()
74+
return &proxy, proxy.Start()
7475
}
7576

7677
// listenVsock proxies connections from a vsock port to a host unix socket.
7778
// This allows the guest to initiate connections to the host over vsock
78-
func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
79+
func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) (io.Closer, error) {
7980
var proxy tcpproxy.Proxy
8081
// listen for connections on the vsock port
8182
proxy.ListenFunc = func(_, laddr string) (net.Listener, error) {
@@ -116,6 +117,6 @@ func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
116117
}
117118
},
118119
})
119-
// FIXME: defer proxy.Close()
120-
return proxy.Start()
120+
121+
return &proxy, proxy.Start()
121122
}

0 commit comments

Comments
 (0)