fix(summer): forward SIGINT and SIGTERM to the app binary

- runAppBinary registers SIGINT and SIGTERM after its build and recursion
  checks and runChild forwards each signal to the app binary, then waits
  for it to exit, so stopping summer serve frees the port (WR-08)
- newAppCmd keeps the inherited stdin, env and output, and a context
  cancel sends SIGTERM with a 15-second appShutdownGrace before SIGKILL
- the child stays in summer's process group so the admin password
  prompts can read the terminal
- helper-process tests cover SIGINT, SIGTERM and context cancel
- console introduction notes the signal forwarding
This commit is contained in:
Jakub Zych
2026-10-08 16:12:01 +02:00
parent b9ef034272
commit 50c8cee541
3 changed files with 187 additions and 4 deletions

View File

@@ -1,10 +1,20 @@
package main
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"runtime"
"slices"
"strings"
"sync"
"syscall"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/bonfire"
)
@@ -113,3 +123,115 @@ func TestDelegateCommandsHelp(t *testing.T) {
}
}
}
// TestSignalHelperProcess is not a real test: the signal tests re-execute the
// test binary with SUMMER_SIGNAL_HELPER=1 to get a child that shuts down
// gracefully on SIGINT or SIGTERM, like the app binary's serve.
func TestSignalHelperProcess(t *testing.T) {
if os.Getenv("SUMMER_SIGNAL_HELPER") != "1" {
return
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
fmt.Println("ready")
select {
case <-ctx.Done():
fmt.Println("graceful shutdown")
os.Exit(0)
case <-time.After(20 * time.Second):
os.Exit(3)
}
}
// syncBuffer is a bytes.Buffer safe for the child's writes and the test's
// reads at the same time.
type syncBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}
func (b *syncBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}
func (b *syncBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}
func helperArgv() []string {
return []string{"-test.run=^TestSignalHelperProcess$"}
}
func helperEnv(cmd *exec.Cmd) {
cmd.Env = append(os.Environ(), "SUMMER_SIGNAL_HELPER=1")
}
func waitForOutput(t *testing.T, out *syncBuffer, want string) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for !strings.Contains(out.String(), want) {
if time.Now().After(deadline) {
t.Fatalf("child never printed %q; output:\n%s", want, out.String())
}
time.Sleep(10 * time.Millisecond)
}
}
func TestRunChildForwardsSignals(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("signal forwarding is POSIX-only")
}
for _, sig := range []os.Signal{os.Interrupt, syscall.SIGTERM} {
t.Run(sig.String(), func(t *testing.T) {
var out syncBuffer
cmd := newAppCmd(context.Background(), os.Args[0], ".", helperArgv(), &out)
helperEnv(cmd)
sigs := make(chan os.Signal, 1)
done := make(chan error, 1)
go func() { done <- runChild(cmd, sigs) }()
waitForOutput(t, &out, "ready")
sigs <- sig
select {
case err := <-done:
if err != nil {
t.Fatalf("runChild: %v\n%s", err, out.String())
}
case <-time.After(10 * time.Second):
t.Fatal("runChild did not return after the signal")
}
if !strings.Contains(out.String(), "graceful shutdown") {
t.Fatalf("child did not shut down gracefully:\n%s", out.String())
}
})
}
}
func TestNewAppCmdCancelSendsSIGTERM(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("SIGTERM is POSIX-only")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var out syncBuffer
cmd := newAppCmd(ctx, os.Args[0], ".", helperArgv(), &out)
helperEnv(cmd)
done := make(chan error, 1)
go func() { done <- runChild(cmd, nil) }()
waitForOutput(t, &out, "ready")
cancel()
select {
case err := <-done:
if !errors.Is(err, context.Canceled) {
t.Fatalf("err = %v, want context.Canceled\n%s", err, out.String())
}
case <-time.After(10 * time.Second):
t.Fatal("child did not exit after cancel")
}
if !strings.Contains(out.String(), "graceful shutdown") {
t.Fatalf("child was not stopped with SIGTERM:\n%s", out.String())
}
}