Fix RunAndWait() process detection

Fixed incorrect browser exit detection in RunAndWait() which could lead to Browser file corruption due to incomplete updates.
This commit is contained in:
Internet Addict 2025-03-28 09:21:38 +00:00
parent f34f335206
commit c1b669e49a

View file

@ -9,7 +9,10 @@ import (
"os"
"os/exec"
"path/filepath"
"syscall"
"strings"
"time"
"golang.org/x/sys/windows"
)
// Run locates and starts the installed Spitfire browser without waiting for it to exit.
@ -33,7 +36,6 @@ func RunAndWait() error {
return fmt.Errorf("failed to get install directory: %w", err)
}
// Construct the browser executable path
exePath := filepath.Join(installDir, "browser", "spitfire.exe")
if _, err := os.Stat(exePath); err != nil {
return fmt.Errorf("browser executable not found at %s: %w", exePath, err)
@ -42,23 +44,29 @@ func RunAndWait() error {
cmd := exec.Command(exePath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = filepath.Join(installDir, "browser")
// Use CREATE_NEW_PROCESS_GROUP flag for Windows
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
// Create job object starting the process
job, err := windows.CreateJobObject(nil, nil)
if err != nil {
return fmt.Errorf("failed to create job object: %w", err)
}
defer windows.CloseHandle(job)
fmt.Printf("Starting browser: %s\n", exePath)
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start browser: %w", err)
}
fmt.Printf("Browser process started with PID %d\n", cmd.Process.Pid)
if err := cmd.Wait(); err != nil {
return fmt.Errorf("browser exited with error: %w", err)
for {
cmd := exec.Command("tasklist", "/FI", "IMAGENAME eq spitfire.exe")
output, _ := cmd.Output()
if !strings.Contains(string(output), "spitfire.exe") {
break
}
time.Sleep(1 * time.Second)
}
fmt.Println("Browser exited successfully.")
fmt.Println("Browser exited.")
return nil
}