Skip to content

Commit 9b00d75

Browse files
committed
rename ErrRelaunchAttempt to ErrRelaunchSucceeded and fix elevated error handling
The old ErrRelaunchAttempt name was ambiguous — it reads as though the relaunch attempt failed, when it actually signals success. Rename to ErrRelaunchSucceeded and update comments at every call site to clarify that this is not a real error but a sentinel indicating the elevated child process completed the operation successfully. Also fix a bug in WSL's launchElevate where a failed elevated process was incorrectly wrapped with the sentinel, causing callers to treat the failure as success and print "Machine init complete." Signed-off-by: lstocchi <lstocchi@redhat.com>
1 parent cbcf486 commit 9b00d75

6 files changed

Lines changed: 25 additions & 24 deletions

File tree

cmd/podman/machine/init.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,13 @@ func initMachine(cmd *cobra.Command, args []string) error {
284284

285285
err = shim.Init(initOpts, machineProvider)
286286
if err != nil {
287-
// The installation is partially complete and podman should
288-
// exit gracefully with no error and no success message.
289-
// Examples:
290-
// - a user has chosen to perform their own reboot
291-
// - reexec for limited admin operations, returning to parent
292-
if errors.Is(err, define.ErrRelaunchAttempt) {
287+
// ErrRelaunchSucceeded is not a real error: it signals that
288+
// an elevated child process completed init successfully.
289+
// Exit gracefully with a success message.
290+
//
291+
// This can happen with WSL when installing the WSL features
292+
// or with HyperV when adding entries to the Registry
293+
if errors.Is(err, define.ErrRelaunchSucceeded) {
293294
fmt.Println("Machine init complete")
294295
if now {
295296
fmt.Printf("Machine %q started successfully\n", initOpts.Name)

cmd/podman/machine/rm.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ func rm(_ *cobra.Command, args []string) error {
6363
}
6464

6565
if err := shim.Remove(mc, vmProvider, destroyOptions); err != nil {
66-
// The removal is partially complete and podman should
67-
// exit gracefully with no error and no success message.
68-
// Examples:
69-
// - reexec for limited admin operations, returning to parent
70-
if errors.Is(err, define.ErrRelaunchAttempt) {
66+
// ErrRelaunchSucceeded is not a real error: it signals that
67+
// an elevated child process completed the removal successfully.
68+
// Exit gracefully.
69+
if errors.Is(err, define.ErrRelaunchSucceeded) {
7170
return nil
7271
}
7372
return err

pkg/machine/define/errors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
)
77

88
var (
9-
ErrWrongState = errors.New("VM in wrong state to perform action")
10-
ErrNotImplemented = errors.New("functionality not implemented")
11-
ErrRelaunchAttempt = errors.New("stopping execution: command relaunched with --reexec flag for elevated privileges")
12-
ErrRebootInitiated = errors.New("system reboot initiated")
9+
ErrWrongState = errors.New("VM in wrong state to perform action")
10+
ErrNotImplemented = errors.New("functionality not implemented")
11+
ErrRelaunchSucceeded = errors.New("stopping execution: command relaunched with --reexec flag for elevated privileges succeeded")
12+
ErrRebootInitiated = errors.New("system reboot initiated")
1313
)
1414

1515
type ErrVMAlreadyExists struct {

pkg/machine/hyperv/stubber.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func launchElevate(message string) error {
268268
windows.DumpOutputFile()
269269
return fmt.Errorf("elevated process failed with error: %w", err)
270270
}
271-
return define.ErrRelaunchAttempt
271+
return define.ErrRelaunchSucceeded
272272
}
273273

274274
// createErrorLogCallback creates a callback function that logs errors to file when --reexec is detected

pkg/machine/shim/host.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error {
7676

7777
callbackFuncs := machine.CleanUp()
7878
defer func() {
79-
// Do not clean up on relaunch: the elevated child process
80-
// completed init successfully and is using the resources
81-
// (e.g. the disk image) that cleanup would remove.
82-
if !errors.Is(err, machineDefine.ErrRelaunchAttempt) {
79+
// ErrRelaunchSucceeded is not a real error: it signals that
80+
// an elevated child process completed the operation successfully.
81+
// Skip cleanup so we don't remove resources (e.g. the disk image)
82+
// that the child process created and that are now in use.
83+
if !errors.Is(err, machineDefine.ErrRelaunchSucceeded) {
8384
callbackFuncs.CleanIfErr(&err)
8485
}
8586
}()

pkg/machine/wsl/machine.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func attemptFeatureInstall(reExec, admin bool) error {
323323
"If you prefer, you may abort now, and perform a manual installation using the \"wsl --install\" command."
324324

325325
if !reExec && winutil.MessageBox(message, "Podman Machine", false) != 1 {
326-
return fmt.Errorf("the WSL installation aborted: %w", define.ErrRelaunchAttempt)
326+
return fmt.Errorf("the WSL installation aborted: %w", define.ErrRelaunchSucceeded)
327327
}
328328

329329
if !reExec && !admin {
@@ -341,16 +341,16 @@ func launchElevate(operation string) error {
341341
if eerr, ok := err.(*winutil.ExitCodeError); ok {
342342
if eerr.Code == ErrorSuccessRebootRequired {
343343
fmt.Println("Reboot is required to continue installation, please reboot at your convenience")
344-
return define.ErrRelaunchAttempt
344+
return define.ErrRelaunchSucceeded
345345
}
346346
}
347347

348348
fmt.Fprintf(os.Stderr, "Elevated process failed with error: %v\n\n", err)
349349
winutil.DumpOutputFile()
350350
fmt.Fprintf(os.Stderr, wslInstallError, operation)
351-
return fmt.Errorf("%w: %w", err, define.ErrRelaunchAttempt)
351+
return fmt.Errorf("elevated process failed to %s: %w", operation, err)
352352
}
353-
return define.ErrRelaunchAttempt
353+
return define.ErrRelaunchSucceeded
354354
}
355355

356356
func installWsl() error {

0 commit comments

Comments
 (0)