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

Fix unstable vz driver network issue #1543

Merged
merged 1 commit into from
May 19, 2023
Merged
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
21 changes: 21 additions & 0 deletions pkg/networks/usernet/UDPFileConn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package usernet

import (
"errors"
"net"
"time"
)

type UDPFileConn struct {
net.Conn
}

func (conn *UDPFileConn) Read(b []byte) (n int, err error) {
// Check if the connection has been closed
if err := conn.SetReadDeadline(time.Time{}); err != nil {
if opErr, ok := err.(*net.OpError); ok && opErr.Err.Error() == "use of closed network connection" {
return 0, errors.New("UDPFileConn connection closed")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we wrap the original err?

Suggested change
return 0, errors.New("UDPFileConn connection closed")
return 0, fmt.Errorf"the UDPFileConn connection closed: %w", err)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but since we are already checking if error is equal to use of closed network connection

I thought we won't be getting anything new out of error object

}
}
return conn.Conn.Read(b)
}
4 changes: 2 additions & 2 deletions pkg/networks/usernet/gvproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ func listenFD(ctx context.Context, vn *virtualnetwork.VirtualNetwork) error {
files[0].Close()

go func() {
err = vn.AcceptBess(ctx, fileConn)
err = vn.AcceptBess(ctx, &UDPFileConn{Conn: fileConn})
if err != nil {
logrus.Error("FD connection closed with error", err)
}
defer fileConn.Close()
fileConn.Close()
}()
select {
case <-ctx.Done():
Expand Down
20 changes: 18 additions & 2 deletions pkg/vz/vm_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type virtualMachineWrapper struct {
stopped bool
}

// Hold all *os.File created via socketpair() so that they won't get garbage collected. f.FD() gets invalid if f gets garbage collected.
var vmNetworkFiles = make([]*os.File, 1)

func startVM(ctx context.Context, driver *driver.BaseDriver) (*virtualMachineWrapper, chan error, error) {
usernetClient, err := startUsernet(ctx, driver)
if err != nil {
Expand All @@ -57,7 +60,11 @@ func startVM(ctx context.Context, driver *driver.BaseDriver) (*virtualMachineWra
errCh := make(chan error)
go func() {
//Handle errors via errCh and handle stop vm during context close

defer func() {
for i := range vmNetworkFiles {
vmNetworkFiles[i].Close()
}
}()
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -700,5 +707,14 @@ func createSockPair() (*os.File, *os.File, error) {
if err = syscall.SetsockoptInt(clientFD, syscall.SOL_SOCKET, syscall.SO_RCVBUF, 4*1024*1024); err != nil {
return nil, nil, err
}
return os.NewFile(uintptr(serverFD), "server"), os.NewFile(uintptr(clientFD), "client"), nil
server := os.NewFile(uintptr(serverFD), "server")
client := os.NewFile(uintptr(clientFD), "client")
runtime.SetFinalizer(server, func(file *os.File) {
logrus.Debugf("Server network file GC'ed")
})
runtime.SetFinalizer(client, func(file *os.File) {
logrus.Debugf("Client network file GC'ed")
})
vmNetworkFiles = append(vmNetworkFiles, server, client)
return server, client, nil
}