Skip to content

Commit

Permalink
WSL: network-setup try to lock OS thread
Browse files Browse the repository at this point in the history
We're doing some namespace jiggling; sometimes it seems like we can end up
in the wrong namespace (where we fail to configure the veth pair for
veth-rd-ns), and it's in a situation where veth-rd-wsl is visible.
Assuming this is because we've jumped OS threads due to golang green thread
scheduling issues, try to lock the OS thread to mitigate the issue.

Signed-off-by: Mark Yen <mark.yen@suse.com>
  • Loading branch information
mook-as committed Dec 24, 2024
1 parent 239029c commit 35ec4c4
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/go/networking/cmd/network/setup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/exec"
"os/signal"
"runtime"
"strconv"

"github.com/linuxkit/virtsock/pkg/vsock"
Expand Down Expand Up @@ -88,6 +89,11 @@ func main() {
}
logrus.Debugf("successful connection to host on CID: %v and Port: %d: connection: %+v", vsock.CIDHost, vsockDialPort, vsockConn)

// Ensure we stay on the same OS thread so that we don't switch namespaces
// accidentally.
runtime.LockOSThread()
defer runtime.UnlockOSThread()

originNS, err := netns.Get()
if err != nil {
logrus.Errorf("failed getting a handle to the current namespace: %v", err)
Expand Down Expand Up @@ -151,7 +157,7 @@ func main() {
logrus.Errorf("failed to close original NS, ignoring error: %v", err)
}

logrus.Trace("Network setup complete, waiting for vm-switch")
logrus.Debug("Network setup complete, waiting for vm-switch")

if err := vmSwitchCmd.Wait(); err != nil {
logrus.Errorf("vm-switch exited with error: %v", err)
Expand Down Expand Up @@ -250,7 +256,7 @@ func cleanupVethLink(originNS netns.NsHandle) {
func configureVethPair(vethName, ipAddr string) error {
veth, err := netlink.LinkByName(vethName)
if err != nil {
return err
return fmt.Errorf("failed to get link %s: %w", vethName, err)
}

vethIP := net.IPNet{
Expand All @@ -260,11 +266,11 @@ func configureVethPair(vethName, ipAddr string) error {

addr := &netlink.Addr{IPNet: &vethIP, Label: ""}
if err := netlink.AddrAdd(veth, addr); err != nil {
return err
return fmt.Errorf("failed to add addr %s to %s: %w", addr, vethName, err)
}

if err := netlink.LinkSetUp(veth); err != nil {
return err
return fmt.Errorf("failed to set up link %s: %w", vethName, err)
}
return nil
}
Expand Down

0 comments on commit 35ec4c4

Please sign in to comment.