Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Sets some best-effort timeouts #115

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions internal/backend/windows/wslexe_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@ 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 {
_, 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 +41,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(), 5*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 +57,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(), 5*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 +69,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(), 5*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
Loading