Skip to content

Commit

Permalink
ci: add workaround for WSL hanging in e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Vazquez <[email protected]>
  • Loading branch information
austinvazquez committed Jun 21, 2024
1 parent 9c1caf0 commit fc3240d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions e2e/vm/vm_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ var resetDisks = func(_ *option.Option, installed bool) {
}
gomega.Expect(os.RemoveAll(dataDiskDir)).ShouldNot(gomega.HaveOccurred())
}

var shutdownWSL = func() error {
// no-op on darwin
return nil
}
5 changes: 2 additions & 3 deletions e2e/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package vm

import (
"os/exec"
"runtime"
"time"

Expand All @@ -29,7 +28,7 @@ var resetVM = func(o *option.Option) {
// clean up iptables
//nolint:lll // link to explanation
// https://docs.rancherdesktop.io/troubleshooting-tips/#q-how-do-i-fix-fata0005-subnet-1040024-overlaps-with-other-one-on-this-address-space-when-running-a-container-using-nerdctl-run
gomega.Expect(exec.Command("wsl", "--shutdown").Run()).Should(gomega.BeNil())
gomega.Expect(shutdownWSL()).Should(gomega.BeNil())
}

ginkgo.DeferCleanup(func() {
Expand All @@ -38,7 +37,7 @@ var resetVM = func(o *option.Option) {
time.Sleep(1 * time.Second)
command.New(o, virtualMachineRootCmd, "remove", "-f").WithoutCheckingExitCode().WithTimeoutInSeconds(10).Run()
if runtime.GOOS == "windows" {
gomega.Expect(exec.Command("wsl", "--shutdown").Run()).Should(gomega.BeNil())
gomega.Expect(shutdownWSL()).Should(gomega.BeNil())
}
time.Sleep(1 * time.Second)
command.New(o, virtualMachineRootCmd, "init").WithoutCheckingExitCode().WithTimeoutInSeconds(160).Run()
Expand Down
38 changes: 38 additions & 0 deletions e2e/vm/vm_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
package vm

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
Expand Down Expand Up @@ -60,3 +65,36 @@ var resetDisks = func(_ *option.Option, _ bool) {
dataDiskDir := filepath.Join(finchRootDir, ".finch", ".disks")
gomega.Expect(os.RemoveAll(dataDiskDir)).ShouldNot(gomega.HaveOccurred())
}

// shutdownWSL is a wrapper function for "wsl --shutdown".
//
// This is a workaround for https://github.com/microsoft/WSL/issues/8529
//
// If WSL is suspected of hanging for longer than 10 seconds, then
// kill the WSL service and retry the shutdown command.
//
// This function will at maximum run for 20 seconds before returning
// context.DeadlineExceeded error.
var shutdownWSL = func() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := exec.CommandContext(ctx, "wsl", "--shutdown").Run(); err != nil && errors.Is(err, context.DeadlineExceeded) {
// wsl is hung, kill the wsl service and try again.
// https://github.com/microsoft/WSL/issues/8529
killCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := exec.CommandContext(killCtx, "taskkill", "/f", "/im", "wslservice.exe").Run(); err != nil {
return fmt.Errorf("unable to kill wsl service: %w", err)
}

retryCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return exec.CommandContext(retryCtx, "wsl", "--shutdown").Run()
} else if err != nil {
return err
}

return nil
}

0 comments on commit fc3240d

Please sign in to comment.