From 239029c35e377766d51fe781228cc06d62940965 Mon Sep 17 00:00:00 2001 From: Mark Yen Date: Tue, 24 Dec 2024 12:22:20 -0800 Subject: [PATCH 1/2] WSL: Enable debug logging for wsl-init If debug mode is set, we enable debug logging for wsl-init (and therefore network-setup). Signed-off-by: Mark Yen --- pkg/rancher-desktop/assets/scripts/wsl-init | 2 +- pkg/rancher-desktop/backend/wsl.ts | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/rancher-desktop/assets/scripts/wsl-init b/pkg/rancher-desktop/assets/scripts/wsl-init index 2bbdbd629d3..a6049b15b85 100644 --- a/pkg/rancher-desktop/assets/scripts/wsl-init +++ b/pkg/rancher-desktop/assets/scripts/wsl-init @@ -21,7 +21,7 @@ if [ $$ -ne "1" ]; then # from WSL. exec /usr/local/bin/network-setup --logfile "$NETWORK_SETUP_LOG" \ --vm-switch-path /usr/local/bin/vm-switch --vm-switch-logfile \ - "$VM_SWITCH_LOG" --unshare-arg "${0}" + "$VM_SWITCH_LOG" ${RD_DEBUG:+-debug} --unshare-arg "${0}" fi # Mark directories that we will need to bind mount as shared mounts. diff --git a/pkg/rancher-desktop/backend/wsl.ts b/pkg/rancher-desktop/backend/wsl.ts index 6706a577ed4..aae9164a9fd 100644 --- a/pkg/rancher-desktop/backend/wsl.ts +++ b/pkg/rancher-desktop/backend/wsl.ts @@ -1041,15 +1041,20 @@ export default class WSLBackend extends events.EventEmitter implements VMBackend // The process should already be gone by this point, but make sure. this.process?.kill('SIGTERM'); + const env: Record = { + ...process.env, + WSLENV: `${ process.env.WSLENV }:DISTRO_DATA_DIRS:LOG_DIR/p:RD_DEBUG`, + DISTRO_DATA_DIRS: DISTRO_DATA_DIRS.join(':'), + LOG_DIR: paths.logs, + }; + + if (this.debug) { + env.RD_DEBUG = '1'; + } this.process = childProcess.spawn('wsl.exe', ['--distribution', INSTANCE_NAME, '--exec', '/usr/local/bin/wsl-init'], { - env: { - ...process.env, - WSLENV: `${ process.env.WSLENV }:DISTRO_DATA_DIRS:LOG_DIR/p`, - DISTRO_DATA_DIRS: DISTRO_DATA_DIRS.join(':'), - LOG_DIR: paths.logs, - }, + env, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true, }); From 35ec4c4a0359e1a22483409c7af05a3845920aa4 Mon Sep 17 00:00:00 2001 From: Mark Yen Date: Tue, 24 Dec 2024 12:29:21 -0800 Subject: [PATCH 2/2] WSL: network-setup try to lock OS thread 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 --- src/go/networking/cmd/network/setup_linux.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/go/networking/cmd/network/setup_linux.go b/src/go/networking/cmd/network/setup_linux.go index 5a7c636bb0d..e5eb08c7072 100644 --- a/src/go/networking/cmd/network/setup_linux.go +++ b/src/go/networking/cmd/network/setup_linux.go @@ -23,6 +23,7 @@ import ( "os" "os/exec" "os/signal" + "runtime" "strconv" "github.com/linuxkit/virtsock/pkg/vsock" @@ -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) @@ -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) @@ -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{ @@ -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 }