fix(agent): recover OpenResty runtime after invalid PID (#45)

* fix(agent): recover OpenResty runtime after invalid PID

* fix(agent): scope OpenResty shutdown to instance processes

---------

Co-authored-by: Noru Wyrms <wyrmsnoru@gmail.com>
This commit is contained in:
Ryan
2026-10-08 08:39:40 +08:00
committed by GitHub
parent fec9bd18dd
commit 96a17ae35d
15 changed files with 1000 additions and 91 deletions
@@ -0,0 +1,40 @@
//go:build unix && !linux
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package nginx
import (
"context"
"fmt"
"os"
"os/exec"
"syscall"
)
// Non-Linux Unix systems expose process ownership and start time through ps.
func inspectOpenrestyProcesses(ctx context.Context, binary, configPath string) ([]runtimeProcess, error) {
output, err := exec.CommandContext(ctx, "ps", "-axo", "pid=,ppid=,uid=,lstart=,command=").Output()
if err != nil {
return nil, fmt.Errorf("inspect openresty processes: %w", err)
}
return parsePSRuntime(string(output), binary, configPath, os.Geteuid())
}
func signalOpenrestyProcess(ctx context.Context, binary, config string, process runtimeProcess, quit bool) error {
processes, err := inspectOpenrestyProcesses(ctx, binary, config)
if err != nil {
return err
}
for _, current := range processes {
if current == process && current.Master {
signal := syscall.SIGHUP
if quit {
signal = syscall.SIGQUIT
}
return syscall.Kill(current.PID, signal)
}
}
return fmt.Errorf("openresty master identity changed before signalling pid %d", process.PID)
}