| 1 | //go:build windows |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "time" |
| 11 | |
| 12 | "reasonix/desktop/internal/instanceidentity" |
| 13 | "reasonix/internal/desktopinstance" |
| 14 | "reasonix/internal/installlayout" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | desktopEndpointImageFn = desktopEndpointImage |
| 19 | handoffNowFn = time.Now |
| 20 | handoffSleepFn = time.Sleep |
| 21 | notifyHandoffBlockedFn = notifyHandoffBlocked |
| 22 | ) |
| 23 | |
| 24 | const desktopHandoffTimeout = 30 * time.Second |
| 25 | |
| 26 | // verifyDesktopHandoff checks the single-instance owner, never all installed processes. |
| 27 | func verifyDesktopHandoff(installDir string, started bool) error { |
| 28 | id := instanceidentity.UpdateID() |
| 29 | home := os.Getenv("REASONIX_HOME") |
| 30 | if !instanceidentity.Valid(id) || !filepath.IsAbs(home) || id != instanceidentity.ForHome(home) { |
| 31 | return fmt.Errorf("update instance identity is unavailable; automatic restart cannot be verified") |
| 32 | } |
| 33 | target := filepath.Join(installDir, installlayout.DesktopBinaryName()) |
| 34 | if installlayout.HasCurrent(installDir) { |
| 35 | var err error |
| 36 | target, err = installlayout.ActiveDesktopPath(installDir) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | if started { |
| 41 | return desktopinstance.VerifyCurrent(installDir, home) |
| 42 | } |
| 43 | } |
| 44 | deadline := handoffNowFn().Add(desktopHandoffTimeout) |
| 45 | for { |
| 46 | path, err := desktopEndpointImageFn(id) |
| 47 | if err != nil && !(started && errors.Is(err, errEndpointStarting)) { |
| 48 | return fmt.Errorf("inspect update instance: %w", err) |
| 49 | } |
| 50 | if path != "" { |
| 51 | same, err := installlayout.SameRegularFile(path, target) |
| 52 | if err != nil { |
| 53 | return fmt.Errorf("verify update instance image: %w", err) |
| 54 | } |
| 55 | if !same { |
| 56 | return fmt.Errorf("another version still owns this data home's desktop endpoint") |
| 57 | } |
| 58 | return nil |
| 59 | } |
| 60 | if !started { |
| 61 | return nil |
| 62 | } |
| 63 | if !handoffNowFn().Before(deadline) { |
| 64 | return fmt.Errorf("new desktop did not acquire its instance endpoint within %s", desktopHandoffTimeout) |
| 65 | } |
| 66 | handoffSleepFn(100 * time.Millisecond) |
| 67 | } |
| 68 | } |
| 69 |