Skip to content

Commit ce33685

Browse files
committed
try to fix windows build
1 parent 12fa9a4 commit ce33685

3 files changed

Lines changed: 66 additions & 30 deletions

File tree

pkg/frontend/manager.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ package frontend
22

33
import (
44
"bufio"
5-
"errors"
65
"fmt"
76
"io"
87
"os"
98
"os/exec"
10-
"runtime"
119
"sort"
1210
"strings"
1311
"sync"
14-
"syscall"
1512
"time"
1613

1714
"github.com/bjartek/aether/pkg/events"
@@ -90,9 +87,7 @@ func (m *FrontendManager) Start(teaProgram *tea.Program) error {
9087
}
9188

9289
cmd := exec.Command(parts[0], parts[1:]...)
93-
if runtime.GOOS != "windows" {
94-
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
95-
}
90+
setupProcessGroup(cmd)
9691

9792
if wd, err := os.Getwd(); err != nil {
9893
m.logger.Warn().Err(err).Msg("Failed to get working directory for frontend process; using shell default")
@@ -245,30 +240,7 @@ func (m *FrontendManager) Stop() error {
245240
}
246241

247242
m.logger.Info().Msg("Stopping frontend process")
248-
var err error
249-
250-
if runtime.GOOS == "windows" {
251-
err = m.cmd.Process.Kill()
252-
} else {
253-
pgid, pgErr := syscall.Getpgid(m.cmd.Process.Pid)
254-
if pgErr != nil {
255-
err = m.cmd.Process.Kill()
256-
} else {
257-
// Attempt graceful shutdown
258-
termErr := syscall.Kill(-pgid, syscall.SIGTERM)
259-
if termErr != nil && !errors.Is(termErr, syscall.ESRCH) {
260-
err = termErr
261-
}
262-
263-
// Give process a brief moment to exit before forcing
264-
time.Sleep(250 * time.Millisecond)
265-
266-
killErr := syscall.Kill(-pgid, syscall.SIGKILL)
267-
if killErr != nil && !errors.Is(killErr, syscall.ESRCH) {
268-
err = killErr
269-
}
270-
}
271-
}
243+
err := stopProcess(m.cmd)
272244

273245
if err != nil {
274246
m.logger.Error().Err(err).Msg("Failed to stop frontend process")

pkg/frontend/manager_unix.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:build !windows
2+
3+
package frontend
4+
5+
import (
6+
"errors"
7+
"os/exec"
8+
"syscall"
9+
"time"
10+
)
11+
12+
// setupProcessGroup configures the command to run in its own process group
13+
func setupProcessGroup(cmd *exec.Cmd) {
14+
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
15+
}
16+
17+
// stopProcess stops the process and its children gracefully on Unix systems
18+
func stopProcess(cmd *exec.Cmd) error {
19+
if cmd == nil || cmd.Process == nil {
20+
return nil
21+
}
22+
23+
pgid, pgErr := syscall.Getpgid(cmd.Process.Pid)
24+
if pgErr != nil {
25+
return cmd.Process.Kill()
26+
}
27+
28+
// Attempt graceful shutdown with SIGTERM
29+
termErr := syscall.Kill(-pgid, syscall.SIGTERM)
30+
if termErr != nil && !errors.Is(termErr, syscall.ESRCH) {
31+
return termErr
32+
}
33+
34+
// Give process a brief moment to exit before forcing
35+
time.Sleep(250 * time.Millisecond)
36+
37+
// Force kill with SIGKILL
38+
killErr := syscall.Kill(-pgid, syscall.SIGKILL)
39+
if killErr != nil && !errors.Is(killErr, syscall.ESRCH) {
40+
return killErr
41+
}
42+
43+
return nil
44+
}

pkg/frontend/manager_windows.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build windows
2+
3+
package frontend
4+
5+
import (
6+
"os/exec"
7+
)
8+
9+
// setupProcessGroup is a no-op on Windows
10+
func setupProcessGroup(cmd *exec.Cmd) {
11+
// Windows doesn't use process groups the same way
12+
}
13+
14+
// stopProcess stops the process on Windows
15+
func stopProcess(cmd *exec.Cmd) error {
16+
if cmd == nil || cmd.Process == nil {
17+
return nil
18+
}
19+
return cmd.Process.Kill()
20+
}

0 commit comments

Comments
 (0)