Fixed the stale pid issue

This commit is contained in:
mrigangha 2026-05-07 03:32:06 +00:00
parent 4a715605bf
commit 54598aee8e
2 changed files with 126 additions and 9 deletions

View file

@ -46,16 +46,35 @@ func generateToken() string {
return hex.EncodeToString(b) return hex.EncodeToString(b)
} }
// Does a heath check of the port if already a gateway is running // isGatewayAlive performs a health check against the recorded host and port,
func isGatewayAlive(port int) bool { // then verifies the reported PID matches expectedPID to confirm the process
url := fmt.Sprintf("http://localhost:%d/health", port) // is actually a picoclaw gateway and not a foreign service on the same port.
func isGatewayAlive(host string, port int, expectedPID int) bool {
url := fmt.Sprintf("http://%s:%d/health", host, port)
client := &http.Client{Timeout: 2 * time.Second} client := &http.Client{Timeout: 2 * time.Second}
resp, err := client.Get(url) resp, err := client.Get(url)
if err != nil { if err != nil {
// Port not responding — PID belongs to a foreign process
return false return false
} }
defer resp.Body.Close() defer resp.Body.Close()
return resp.StatusCode == 200
if resp.StatusCode != http.StatusOK {
return false
}
var body struct {
PID int `json:"pid"`
}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
return false
}
fmt.Println("HEllo")
// Only treat as alive if the reported PID matches the PID file.
// This prevents an unrelated service on the same port from being
// mistaken for a running gateway.
return body.PID == expectedPID
} }
// WritePidFile creates (or overwrites) the PID file atomically. // WritePidFile creates (or overwrites) the PID file atomically.
@ -76,7 +95,7 @@ func WritePidFile(homePath, host string, port int) (*PidFileData, error) {
// PID file on a shared volume, the host's PID 1 (init) would // PID file on a shared volume, the host's PID 1 (init) would
// pass the isProcessRunning check, blocking new gateway starts. // pass the isProcessRunning check, blocking new gateway starts.
// Treat recorded PID 1 as always stale. // Treat recorded PID 1 as always stale.
if data.PID != 1 && isProcessRunning(data.PID) && isGatewayAlive(data.Port) { if data.PID != 1 && isProcessRunning(data.PID) && isGatewayAlive(data.Host, data.Port, data.PID){
return nil, fmt.Errorf("gateway is already running (PID: %d, version: %s)", data.PID, data.Version) return nil, fmt.Errorf("gateway is already running (PID: %d, version: %s)", data.PID, data.Version)
} }
logger.Warnf("not running (PID: %d) so will remove the pid file: %s", data.PID, pidPath) logger.Warnf("not running (PID: %d) so will remove the pid file: %s", data.PID, pidPath)

View file

@ -2,8 +2,12 @@ package pid
import ( import (
"encoding/json" "encoding/json"
"net"
"net/http"
"net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"testing" "testing"
) )
@ -51,6 +55,100 @@ func TestPidFilePath(t *testing.T) {
} }
} }
// verifies that an unrelated service on the recorded port is not mistaken for the gateway.
func TestWritePidFileHealthPIDMismatch(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Return PID 99999 — does not match the PID file entry
json.NewEncoder(w).Encode(map[string]any{"pid": 99999, "status": "ok"})
})
srv := httptest.NewServer(mux)
defer srv.Close()
host, portStr, _ := net.SplitHostPort(srv.Listener.Addr().String())
port, _ := strconv.Atoi(portStr)
dir := tmpDir(t)
foreign := PidFileData{
PID: os.Getpid(), // real running PID so it reaches isGatewayAlive
Token: "deadbeef12345678deadbeef12345678",
Port: port,
Host: host,
}
raw, _ := json.MarshalIndent(foreign, "", " ")
os.WriteFile(filepath.Join(dir, pidFileName), raw, 0o600)
// Should succeed — health PID (99999) != PID file PID (os.Getpid()) = not our gateway
data, err := WritePidFile(dir, "127.0.0.1", 18790)
if err != nil {
t.Fatalf("WritePidFile should treat health PID mismatch as stale, got error: %v", err)
}
if data.PID != os.Getpid() {
t.Errorf("PID = %d, want %d", data.PID, os.Getpid())
}
}
// verifies that isGatewayAlive uses the host from the PID file instead of hardcoding localhost.
func TestWritePidFileNonLocalhostHost(t *testing.T) {
if !isProcessRunning(os.Getppid()) {
t.Skip("skipping: parent process not running in this environment")
}
mux := http.NewServeMux()
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Return parent PID — matches the PID file entry
json.NewEncoder(w).Encode(map[string]any{"pid": os.Getppid(), "status": "ok"})
})
srv := httptest.NewServer(mux)
defer srv.Close()
host, portStr, _ := net.SplitHostPort(srv.Listener.Addr().String())
port, _ := strconv.Atoi(portStr)
dir := tmpDir(t)
foreign := PidFileData{
PID: os.Getppid(), // parent PID — real, running, but not us
Token: "deadbeef12345678deadbeef12345678",
Port: port,
Host: host,
}
raw, _ := json.MarshalIndent(foreign, "", " ")
os.WriteFile(filepath.Join(dir, pidFileName), raw, 0o600)
// Should block — PID exists, health responds with matching PID on non-localhost host
_, err := WritePidFile(dir, "127.0.0.1", 18790)
if err == nil {
t.Fatal("WritePidFile should block startup when gateway is genuinely alive on non-localhost host")
}
}
//verifies that a foreign process reusing a crashed gateway's PID is treated as stale.
func TestWritePidFileForeignPIDReuse(t *testing.T) {
dir := tmpDir(t)
// PID 1 (init/systemd) is always running but won't respond on port 19999
foreign := PidFileData{
PID: 1,
Token: "deadbeef12345678deadbeef12345678",
Port: 19999, // nothing listening here
Host: "127.0.0.1",
}
raw, _ := json.MarshalIndent(foreign, "", " ")
os.WriteFile(filepath.Join(dir, pidFileName), raw, 0o600)
// Should succeed — foreign PID reuse should be treated as stale
data, err := WritePidFile(dir, "127.0.0.1", 18790)
if err != nil {
t.Fatalf("WritePidFile should treat foreign PID as stale, got error: %v", err)
}
if data.PID != os.Getpid() {
t.Errorf("PID = %d, want %d", data.PID, os.Getpid())
}
}
// TestWritePidFile creates a PID file and verifies its contents. // TestWritePidFile creates a PID file and verifies its contents.
func TestWritePidFile(t *testing.T) { func TestWritePidFile(t *testing.T) {
dir := tmpDir(t) dir := tmpDir(t)