Skip to content

Commit

Permalink
Sets some best-effort timeouts
Browse files Browse the repository at this point in the history
Those functions used the background context.
When wslservice.exe hangs/crashes, wsl.exe commands might hang for very long.
We're setting some generous timeouts to couple with a variety of hardware.
In the happy paths, 1/100th to 1/10th of those timeouts should be enough.
  • Loading branch information
CarlosNihelton committed Jun 18, 2024
1 parent 9a0c314 commit b9baffc
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions internal/backend/windows/wslexe_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"

"github.com/ubuntu/gowsl/internal/state"
)

// ErrWslTimeout is the error returned when wsl.exe commands don't respond in time.
var ErrWslTimeout = errors.New("wsl.exe did not respond: consider restarting wslservice.exe")

// Shutdown shuts down all distros
//
// It is analogous to
//
// `wsl.exe --Shutdown

func (Backend) Shutdown() error {

Check failure on line 28 in internal/backend/windows/wslexe_windows.go

View workflow job for this annotation

GitHub Actions / Quality checks (windows)

exported: exported method Backend.Shutdown should have comment or be unexported (revive)
_, err := wslExe(context.Background(), "--shutdown")
ctx, cancel := context.WithTimeoutCause(context.Background(), 10*time.Second, ErrWslTimeout)
defer cancel()

_, err := wslExe(ctx, "--shutdown")
if err != nil {
return fmt.Errorf("could not shut WSL down: %w", err)
}
Expand All @@ -33,7 +42,10 @@ func (Backend) Shutdown() error {
//
// `wsl.exe --Terminate <distroName>`
func (Backend) Terminate(distroName string) error {
_, err := wslExe(context.Background(), "--terminate", distroName)
ctx, cancel := context.WithTimeoutCause(context.Background(), 3*time.Second, ErrWslTimeout)
defer cancel()

_, err := wslExe(ctx, "--terminate", distroName)
if err != nil {
return fmt.Errorf("could not terminate distro %q: %w", distroName, err)
}
Expand All @@ -46,7 +58,10 @@ func (Backend) Terminate(distroName string) error {
//
// `wsl.exe --set-default <distroName>`
func (Backend) SetAsDefault(distroName string) error {
_, err := wslExe(context.Background(), "--set-default", distroName)
ctx, cancel := context.WithTimeoutCause(context.Background(), 1*time.Second, ErrWslTimeout)
defer cancel()

_, err := wslExe(ctx, "--set-default", distroName)
if err != nil {
return fmt.Errorf("could not set %q as default: %w", distroName, err)
}
Expand All @@ -55,7 +70,10 @@ func (Backend) SetAsDefault(distroName string) error {

// State returns the state of a particular distro as seen in `wsl.exe -l -v`.
func (Backend) State(distributionName string) (s state.State, err error) {
out, err := wslExe(context.Background(), "--list", "--all", "--verbose")
ctx, cancel := context.WithTimeoutCause(context.Background(), 1*time.Second, ErrWslTimeout)
defer cancel()

out, err := wslExe(ctx, "--list", "--all", "--verbose")
if err != nil {
return s, fmt.Errorf("could not get states of distros: %w", err)
}
Expand Down

0 comments on commit b9baffc

Please sign in to comment.