Skip to content

Commit 66c0f4b

Browse files
committed
Handle vfkit connection in goroutine & close old one on new connection
Moved AcceptVfkit handling into a goroutine to prevent blocking the listener and ensure responsiveness. Added logic to cancel any existing context before accepting a new connection.
1 parent 3f50a6e commit 66c0f4b

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

cmd/crc/cmd/daemon.go

Lines changed: 11 additions & 1 deletion
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 from %s. Closing old connection", conn.LocalAddr().String())
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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +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-
err = vn.AcceptVfkit(context.Background(), vfkitConn)
48-
return conn, errors.Wrap(err, "failed to accept vfkit connection")
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())
4956
}
5057

5158
func checkIfDaemonIsRunning() (bool, error) {

cmd/crc/cmd/daemon_linux.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"context"
45
"fmt"
56
"net"
67
"os"
@@ -125,10 +126,12 @@ func httpListener() (net.Listener, error) {
125126
return ln, nil
126127
}
127128

128-
func unixgramListener(_ *virtualnetwork.VirtualNetwork) (*net.UnixConn, error) {
129+
func setupUnixgramListener() (net.Conn, error) {
129130
return nil, nil
130131
}
131132

133+
func handleUnixgramConnection(_ context.Context, _ *virtualnetwork.VirtualNetwork, _ net.Conn) {}
134+
132135
func startupDone() {
133136
_, _ = daemon.SdNotify(false, daemon.SdNotifyReady)
134137
}

cmd/crc/cmd/daemon_windows.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/Microsoft/go-winio"
77
"github.com/containers/gvisor-tap-vsock/pkg/transport"
8-
"github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork"
98
"github.com/crc-org/crc/v2/pkg/crc/constants"
109
"github.com/crc-org/crc/v2/pkg/crc/logging"
1110
)
@@ -36,9 +35,11 @@ func checkIfDaemonIsRunning() (bool, error) {
3635
return checkDaemonVersion()
3736
}
3837

39-
func unixgramListener(_ *virtualnetwork.VirtualNetwork) (*net.UnixConn, error) {
38+
func setupUnixgramListener() (net.Conn, error) {
4039
return nil, nil
4140
}
4241

42+
func handleUnixgramConnection(_ context.Context, _ *virtualnetwork.VirtualNetwork, _ net.Conn) {}
43+
4344
func startupDone() {
4445
}

0 commit comments

Comments
 (0)