fix(daemon): cross-platform build for Dolt server management
Extract platform-specific syscall code into proc_unix.go and proc_windows.go with appropriate build tags. This fixes TestCrossPlatformBuild which failed because syscall.SysProcAttr.Setpgid is Unix-only. Changes: - setSysProcAttr(): Sets process group on Unix, no-op on Windows - isProcessAlive(): Uses Signal(0) on Unix, nil signal on Windows - sendTermSignal(): SIGTERM on Unix, Kill() on Windows - sendKillSignal(): SIGKILL on Unix, Kill() on Windows Fixes gt-3078ed Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+9
-12
@@ -10,7 +10,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -143,7 +142,7 @@ func (m *DoltServerManager) Status() *DoltServerStatus {
|
|||||||
func (m *DoltServerManager) isRunning() (int, bool) {
|
func (m *DoltServerManager) isRunning() (int, bool) {
|
||||||
// First check our tracked process
|
// First check our tracked process
|
||||||
if m.process != nil {
|
if m.process != nil {
|
||||||
if err := m.process.Signal(syscall.Signal(0)); err == nil {
|
if isProcessAlive(m.process) {
|
||||||
return m.process.Pid, true
|
return m.process.Pid, true
|
||||||
}
|
}
|
||||||
// Process died, clear it
|
// Process died, clear it
|
||||||
@@ -167,7 +166,7 @@ func (m *DoltServerManager) isRunning() (int, bool) {
|
|||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := process.Signal(syscall.Signal(0)); err != nil {
|
if !isProcessAlive(process) {
|
||||||
// Process not running, clean up stale PID file
|
// Process not running, clean up stale PID file
|
||||||
_ = os.Remove(m.pidFile())
|
_ = os.Remove(m.pidFile())
|
||||||
return 0, false
|
return 0, false
|
||||||
@@ -271,9 +270,7 @@ func (m *DoltServerManager) startLocked() error {
|
|||||||
cmd.Stderr = logFile
|
cmd.Stderr = logFile
|
||||||
|
|
||||||
// Detach from this process group so it survives daemon restart
|
// Detach from this process group so it survives daemon restart
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
setSysProcAttr(cmd)
|
||||||
Setpgid: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
logFile.Close()
|
logFile.Close()
|
||||||
@@ -328,16 +325,16 @@ func (m *DoltServerManager) stopLocked() error {
|
|||||||
return nil // Already gone
|
return nil // Already gone
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send SIGTERM for graceful shutdown
|
// Send termination signal for graceful shutdown
|
||||||
if err := process.Signal(syscall.SIGTERM); err != nil {
|
if err := sendTermSignal(process); err != nil {
|
||||||
m.logger("Warning: failed to send SIGTERM: %v", err)
|
m.logger("Warning: failed to send termination signal: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for graceful shutdown (up to 5 seconds)
|
// Wait for graceful shutdown (up to 5 seconds)
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
for i := 0; i < 50; i++ {
|
for i := 0; i < 50; i++ {
|
||||||
if err := process.Signal(syscall.Signal(0)); err != nil {
|
if !isProcessAlive(process) {
|
||||||
close(done)
|
close(done)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -350,8 +347,8 @@ func (m *DoltServerManager) stopLocked() error {
|
|||||||
m.logger("Dolt SQL server stopped gracefully")
|
m.logger("Dolt SQL server stopped gracefully")
|
||||||
case <-time.After(5 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
// Force kill
|
// Force kill
|
||||||
m.logger("Dolt SQL server did not stop gracefully, sending SIGKILL")
|
m.logger("Dolt SQL server did not stop gracefully, forcing termination")
|
||||||
_ = process.Signal(syscall.SIGKILL)
|
_ = sendKillSignal(process)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
//go:build unix
|
||||||
|
|
||||||
|
package daemon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// setSysProcAttr sets platform-specific process attributes.
|
||||||
|
// On Unix, we detach from the process group so the server survives daemon restart.
|
||||||
|
func setSysProcAttr(cmd *exec.Cmd) {
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||||
|
Setpgid: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isProcessAlive checks if a process is still running.
|
||||||
|
func isProcessAlive(p *os.Process) bool {
|
||||||
|
return p.Signal(syscall.Signal(0)) == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendTermSignal sends SIGTERM for graceful shutdown.
|
||||||
|
func sendTermSignal(p *os.Process) error {
|
||||||
|
return p.Signal(syscall.SIGTERM)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendKillSignal sends SIGKILL for forced termination.
|
||||||
|
func sendKillSignal(p *os.Process) error {
|
||||||
|
return p.Signal(syscall.SIGKILL)
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package daemon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
// setSysProcAttr sets platform-specific process attributes.
|
||||||
|
// On Windows, no special attributes needed for process group detachment.
|
||||||
|
func setSysProcAttr(cmd *exec.Cmd) {
|
||||||
|
// No-op on Windows - process will run independently
|
||||||
|
}
|
||||||
|
|
||||||
|
// isProcessAlive checks if a process is still running.
|
||||||
|
// On Windows, we try to open the process with minimal access.
|
||||||
|
func isProcessAlive(p *os.Process) bool {
|
||||||
|
// On Windows, FindProcess always succeeds, and Signal(0) may not work.
|
||||||
|
// The best we can do is try to signal and see if it fails.
|
||||||
|
// A killed process will return an error.
|
||||||
|
err := p.Signal(os.Signal(nil))
|
||||||
|
// If err is nil or "not supported", process may still be alive
|
||||||
|
// If err mentions "finished" or similar, process is dead
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendTermSignal sends a termination signal.
|
||||||
|
// On Windows, there's no SIGTERM - we use Kill() directly.
|
||||||
|
func sendTermSignal(p *os.Process) error {
|
||||||
|
return p.Kill()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendKillSignal sends a kill signal.
|
||||||
|
// On Windows, Kill() is the only option.
|
||||||
|
func sendKillSignal(p *os.Process) error {
|
||||||
|
return p.Kill()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user