Skip to content

Commit 409e071

Browse files
committed
unixgram: close previous vfkit connection using cancel context
1 parent 4b8408d commit 409e071

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

cmd/crc/cmd/daemon.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -209,11 +210,20 @@ func run(configuration *types.Configuration) error {
209210
}()
210211

211212
go func() {
213+
var ctx context.Context
214+
var cancel context.CancelFunc
212215
for {
213-
_, err := unixgramListener(vn)
216+
conn, err := setupUnixgramListener()
214217
if err != nil && !errors.Is(err, net.ErrClosed) {
215218
logging.Errorf("unixgramListener error: %v", err)
216219
}
220+
if cancel != nil {
221+
logging.Warnf("New connection to %s. Closing old connection", constants.UnixgramSocketPath)
222+
cancel()
223+
}
224+
225+
ctx, cancel = context.WithCancel(context.Background())
226+
go handleUnixgramConnection(ctx, vn, conn)
217227
time.Sleep(1 * time.Second)
218228
}
219229
}()

cmd/crc/cmd/daemon_darwin.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,26 @@ func httpListener() (net.Listener, error) {
3333
return ln, nil
3434
}
3535

36-
func unixgramListener(vn *virtualnetwork.VirtualNetwork) (*net.UnixConn, error) {
36+
func setupUnixgramListener() (net.Conn, error) {
3737
_ = os.Remove(constants.UnixgramSocketPath)
3838
conn, err := transport.ListenUnixgram(fmt.Sprintf("unixgram://%v", constants.UnixgramSocketPath))
3939
if err != nil {
40-
return conn, errors.Wrap(err, "failed to listen unixgram")
40+
return nil, errors.Wrap(err, "failed to listen unixgram")
4141
}
42-
logging.Infof("listening on %s:", constants.UnixgramSocketPath)
42+
logging.Infof("listening on %s", constants.UnixgramSocketPath)
4343
vfkitConn, err := transport.AcceptVfkit(conn)
4444
if err != nil {
45-
return conn, errors.Wrap(err, "failed to accept vfkit connection")
45+
return nil, errors.Wrap(err, "failed to accept vfkit connection")
4646
}
47-
go func() {
48-
err := vn.AcceptVfkit(context.Background(), vfkitConn)
49-
if err != nil {
50-
logging.Errorf("failed to accept vfkit connection: %v", err)
51-
return
52-
}
53-
}()
54-
return conn, err
47+
return vfkitConn, nil
48+
}
49+
50+
func handleUnixgramConnection(ctx context.Context, vn *virtualnetwork.VirtualNetwork, vfkitConn net.Conn) {
51+
defer vfkitConn.Close()
52+
if err := vn.AcceptVfkit(ctx, vfkitConn); err != nil {
53+
logging.Errorf("failed to accept vfkit connection: %v", err)
54+
}
55+
logging.Debugf("Closed connection from %s", vfkitConn.LocalAddr().String())
5556
}
5657

5758
func checkIfDaemonIsRunning() (bool, error) {

0 commit comments

Comments
 (0)