Skip to content

Commit 9a07b63

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 9a07b63

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

cmd/vfkit/main.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) e
156156
}
157157
log.Infof("virtual machine is running")
158158

159-
for _, vsock := range vmConfig.VirtioVsockDevices() {
159+
vsockDevs := vmConfig.VirtioVsockDevices()
160+
releases := make([]func(), 0, len(vsockDevs))
161+
for _, vsock := range vsockDevs {
160162
port := vsock.Port
161163
socketURL := vsock.SocketURL
162164
if socketURL == "" {
@@ -168,11 +170,19 @@ func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) e
168170
listenStr = " (listening)"
169171
}
170172
log.Infof("Exposing vsock port %d on %s%s", port, socketURL, listenStr)
171-
if err := vf.ExposeVsock(vm, port, socketURL, vsock.Listen); err != nil {
173+
if release, err := vf.ExposeVsock(vm, port, socketURL, vsock.Listen); err != nil {
172174
log.Warnf("error exposing vsock port %d: %v", port, err)
175+
} else {
176+
releases = append(releases, release)
173177
}
174178
}
175179

180+
defer func() {
181+
for _, r := range releases {
182+
r()
183+
}
184+
}()
185+
176186
if err := setupGuestTimeSync(vm, vmConfig.TimeSync()); err != nil {
177187
log.Warnf("Error configuring guest time synchronization")
178188
log.Debugf("%v", err)

pkg/vf/vsock.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"inet.af/tcpproxy"
1212
)
1313

14-
func ExposeVsock(vm *vz.VirtualMachine, port uint, vsockPath string, listen bool) error {
14+
func ExposeVsock(vm *vz.VirtualMachine, port uint, vsockPath string, listen bool) (release func(), err error) {
1515
if listen {
1616
return listenVsock(vm, port, vsockPath)
1717
}
@@ -36,7 +36,7 @@ func ConnectVsockSync(vm *vz.VirtualMachine, port uint) (net.Conn, error) {
3636

3737
// connectVsock proxies connections from a host unix socket to a vsock port
3838
// This allows the host to initiate connections to the guest over vsock
39-
func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
39+
func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) (release func(), err error) {
4040

4141
var proxy tcpproxy.Proxy
4242
// listen for connections on the host unix socket
@@ -70,12 +70,15 @@ func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
7070
}
7171
},
7272
})
73-
return proxy.Start()
73+
74+
return func() {
75+
_ = proxy.Close()
76+
}, proxy.Start()
7477
}
7578

7679
// listenVsock proxies connections from a vsock port to a host unix socket.
7780
// This allows the guest to initiate connections to the host over vsock
78-
func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
81+
func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) (release func(), err error) {
7982
var proxy tcpproxy.Proxy
8083
// listen for connections on the vsock port
8184
proxy.ListenFunc = func(_, laddr string) (net.Listener, error) {
@@ -116,6 +119,7 @@ func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
116119
}
117120
},
118121
})
119-
// FIXME: defer proxy.Close()
120-
return proxy.Start()
122+
return func() {
123+
_ = proxy.Close()
124+
}, proxy.Start()
121125
}

0 commit comments

Comments
 (0)