test(volume_server): wait out a server's disk I/O before calling its SIGINT exit a hang (#13085)

* test(volume_server): wait out a server's disk I/O before calling its SIGINT exit a hang

stopProcess gave every server 10s to exit after SIGINT. On a CI runner
with a busy disk, the fsync in volume close can take longer than that:
the server is in uninterruptible sleep, not hung. Sample the server's
threads from /proc while waiting. Once one has been in D state, allow
up to a minute. A server that never touched the disk still fails at
10s, and the failure now lists its threads' states and wait channels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(volume_server): wait for the sleep child before checking its stop

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(volume_server): reap the SIGINT-ignoring child if it never starts sleeping

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-27 23:29:59 -08:00
1 parent e6aed012f8
commit efcee4a186
2 files changed
+152 -10

No files matched your search

+72 -10
View File
@@ -326,9 +326,17 @@ func (c *Cluster) waitForTCP(addr string) error {
return fmt.Errorf("timed out waiting for tcp %s", addr)
}
// stopProcess interrupts cmd and waits for it to exit, killing it after 10s.
// A server that does not exit cleanly on SIGINT fails the test: a panic or a
// hang on shutdown is a bug the tests would otherwise never see.
// stopTimeout bounds a server's exit after SIGINT; stopDiskTimeout replaces it
// once a thread of the server has been seen in an uninterruptible wait, such
// as the close fsync on a slow CI disk.
var (
stopTimeout = 10 * time.Second
stopDiskTimeout = time.Minute
)
// stopProcess interrupts cmd and waits for it to exit, killing it after
// stopTimeout. A server that does not exit cleanly on SIGINT fails the test: a
// panic or a hang on shutdown is a bug the tests would otherwise never see.
func stopProcess(tb testing.TB, name string, cmd *exec.Cmd) {
if cmd == nil || cmd.Process == nil {
return
@@ -340,18 +348,72 @@ func stopProcess(tb testing.TB, name string, cmd *exec.Cmd) {
done <- cmd.Wait()
}()
select {
case <-time.After(10 * time.Second):
start := time.Now()
tick := time.NewTicker(100 * time.Millisecond)
defer tick.Stop()
diskWait := false
for {
select {
case err := <-done:
if err != nil {
tb.Errorf("%s did not exit cleanly on SIGINT: %v", name, err)
}
return
case <-tick.C:
}
threads, inDiskWait := processThreads(cmd.Process.Pid)
diskWait = diskWait || inDiskWait
limit := stopTimeout
if diskWait {
limit = stopDiskTimeout
}
if time.Since(start) < limit {
continue
}
_ = cmd.Process.Kill()
<-done
tb.Errorf("%s did not exit within 10s of SIGINT", name)
case err := <-done:
if err != nil {
tb.Errorf("%s did not exit cleanly on SIGINT: %v", name, err)
}
tb.Errorf("%s did not exit within %v of SIGINT, threads: %s", name, limit, threads)
return
}
}
// processThreads summarizes pid's threads from /proc as "name state wchan"
// with counts, and reports whether any is in uninterruptible sleep (D).
func processThreads(pid int) (string, bool) {
dir := fmt.Sprintf("/proc/%d/task", pid)
tasks, err := os.ReadDir(dir)
if err != nil {
return "unavailable", false
}
counts := map[string]int{}
var order []string
inDiskWait := false
for _, task := range tasks {
stat, err := os.ReadFile(filepath.Join(dir, task.Name(), "stat"))
if err != nil {
continue
}
s := string(stat)
open, end := strings.IndexByte(s, '('), strings.LastIndexByte(s, ')')
if open < 0 || end < open || end+2 >= len(s) {
continue
}
state := s[end+2]
inDiskWait = inDiskWait || state == 'D'
wchan, _ := os.ReadFile(filepath.Join(dir, task.Name(), "wchan"))
key := fmt.Sprintf("%s %c %s", s[open+1:end], state, wchan)
if counts[key] == 0 {
order = append(order, key)
}
counts[key]++
}
parts := make([]string, 0, len(order))
for _, key := range order {
parts = append(parts, fmt.Sprintf("%dx[%s]", counts[key], key))
}
return strings.Join(parts, " "), inDiskWait
}
func newWorkDir() (dir string, keepLogs bool, err error) {
keepLogs = os.Getenv("VOLUME_SERVER_IT_KEEP_LOGS") == "1"
dir, err = os.MkdirTemp("", "seaweedfs_volume_server_it_")
@@ -0,0 +1,80 @@
package framework
import (
"fmt"
"os/exec"
"runtime"
"strings"
"testing"
"time"
)
type errorRecorder struct {
testing.TB
errs []string
}
func (r *errorRecorder) Errorf(format string, args ...any) {
r.errs = append(r.errs, fmt.Sprintf(format, args...))
}
func TestStopProcessReportsTheThreadsOfAServerIgnoringSIGINT(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("thread states come from /proc")
}
defer func(d time.Duration) { stopTimeout = d }(stopTimeout)
stopTimeout = 200 * time.Millisecond
cmd := exec.Command("sh", "-c", `trap "" INT; exec sleep 30`)
if err := cmd.Start(); err != nil {
t.Fatalf("start: %v", err)
}
defer func() {
_ = cmd.Process.Kill()
_ = cmd.Wait()
}()
waitForSleepThread(t, cmd.Process.Pid)
rec := &errorRecorder{TB: t}
stopProcess(rec, "server", cmd)
if len(rec.errs) != 1 || !strings.Contains(rec.errs[0], "server did not exit within") || !strings.Contains(rec.errs[0], "1x[sleep S ") {
t.Fatalf("errors = %q", rec.errs)
}
}
func TestProcessThreadsFindsNoDiskWaitInAnIdleProcess(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("thread states come from /proc")
}
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
t.Fatalf("start: %v", err)
}
defer func() {
_ = cmd.Process.Kill()
_ = cmd.Wait()
}()
waitForSleepThread(t, cmd.Process.Pid)
threads, inDiskWait := processThreads(cmd.Process.Pid)
if inDiskWait || !strings.HasPrefix(threads, "1x[sleep S ") {
t.Fatalf("threads = %q, inDiskWait = %v", threads, inDiskWait)
}
}
// waitForSleepThread waits until pid has exec'd sleep and is sleeping, so
// a SIGINT trap set before the exec is in place.
func waitForSleepThread(t *testing.T, pid int) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for {
threads, _ := processThreads(pid)
if strings.HasPrefix(threads, "1x[sleep S ") {
return
}
if time.Now().After(deadline) {
t.Fatalf("pid %d never reached sleep: %q", pid, threads)
}
time.Sleep(10 * time.Millisecond)
}
}