From f85ab931a9d2a5671e2e08eaa11a255e58b833c9 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Tue, 3 Nov 2026 12:19:00 +0100 Subject: [PATCH 1/4] iouring: a close seam and an ownership trap that fail a descriptor number closed twice, and the exit paths pinned with it (Refs #1242) Every close of an engine-owned descriptor goes through closeFD/closeFDErr and fdCloseSeam. The trap dup3s a placeholder over each closed number, so a later close of a number still naming it is a stale close whatever else runs. The worker exit paths (cancel, pause and resume, Goexit, Goexit in the pause's close, ring setup failure, first submit failure, bind failure, a panic) close each number once. The stale closes the canary saw are two tests' own: leaveGroupAfterExit's unit test cleans up with an open-check, and the shutdown gate tests' rig closes what a pass closed. Both fail here. --- internal/engine/iouring/driver.go | 4 +- internal/engine/iouring/fd_close_seam_1242.go | 36 ++ internal/engine/iouring/fd_lifetime.go | 8 +- .../iouring/fd_ownership_1242_linux_test.go | 583 ++++++++++++++++++ internal/engine/iouring/probe.go | 2 +- internal/engine/iouring/ring.go | 4 +- .../iouring/shutdown_gate_884_linux_test.go | 18 +- internal/engine/iouring/transplant.go | 2 +- internal/engine/iouring/transplant_source.go | 4 +- internal/engine/iouring/worker.go | 26 +- .../worker_exit_order_884_linux_test.go | 28 +- internal/engine/iouring/zc_send_buffer.go | 4 +- 12 files changed, 679 insertions(+), 40 deletions(-) create mode 100644 internal/engine/iouring/fd_close_seam_1242.go create mode 100644 internal/engine/iouring/fd_ownership_1242_linux_test.go diff --git a/internal/engine/iouring/driver.go b/internal/engine/iouring/driver.go index bc0000cb8..f592d2a37 100644 --- a/internal/engine/iouring/driver.go +++ b/internal/engine/iouring/driver.go @@ -243,7 +243,7 @@ func (w *Worker) RegisterConn(fd int, onRecv func([]byte), onClose func(error)) } if refused != nil { w.driverMu.Unlock() - _ = unix.Close(opFD) + closeFD(opFD) return refused } if w.driverConns == nil { @@ -790,7 +790,7 @@ func closeDriverFDs(conns []*driverConn, sem chan struct{}) { // closeFD is the close(2) of one engine descriptor; its error is dropped, as // everywhere the engine closes a duplicate it has no use for any more. -func closeFD(fd int) { _ = unix.Close(fd) } +func closeFD(fd int) { _ = closeFDErr(fd) } // closeUnderBound runs closeFn(fd) holding a token of sem: it waits for one, // and gives it back when closeFn returns. A goroutine waiting for a token diff --git a/internal/engine/iouring/fd_close_seam_1242.go b/internal/engine/iouring/fd_close_seam_1242.go new file mode 100644 index 000000000..2c026bded --- /dev/null +++ b/internal/engine/iouring/fd_close_seam_1242.go @@ -0,0 +1,36 @@ +//go:build linux + +package iouring + +import ( + "sync/atomic" + + "golang.org/x/sys/unix" +) + +// fdCloseSeam is a test seam (celeris#1242): when set, every close(2) the +// engine makes of a descriptor it owns (closeFD, closeFDErr) goes through it +// instead of unix.Close, on whichever goroutine closes. A test makes it take +// the number out of use in the same step as the close (dup3 of a placeholder +// over it, so no other open can get the number in between) and then reports a +// second close of a number nothing could have re-opened, which is a stale +// close in a real process: the number may by then name another connection or +// a sibling's listener. Several workers call it, so a test keeps its state +// atomic. Nil in production, where each close costs one load of this +// variable. An atomic.Pointer, as ringWaitSeam is, because a worker an +// earlier test left running may still read it when a later test sets or +// restores it (celeris#1229). +var fdCloseSeam atomic.Pointer[func(fd int) error] + +// closeFDErr is close(2) of one descriptor number the engine owns, through +// fdCloseSeam. Every close of an engine-owned descriptor in this package goes +// through here (or closeFD, which drops the error): a bare unix.Close is +// invisible to the seam test of celeris#1242. The one other closer is the +// wake eventfd's handle (internal/wakefd), which retires the number under its +// lock before it closes it (celeris#655) and so cannot close it twice. +func closeFDErr(fd int) error { + if h := fdCloseSeam.Load(); h != nil { + return (*h)(fd) + } + return unix.Close(fd) +} diff --git a/internal/engine/iouring/fd_lifetime.go b/internal/engine/iouring/fd_lifetime.go index 4d661621b..12927d5e1 100644 --- a/internal/engine/iouring/fd_lifetime.go +++ b/internal/engine/iouring/fd_lifetime.go @@ -539,7 +539,7 @@ func (w *Worker) closedFDNamed(cs *connState) bool { // release closes it when cs goes, or earlier, once only a SEND_ZC // notification is still owed (celeris#798). Worker thread only. func (w *Worker) releaseKeptFD(e *pendingReleaseEntry) { - _ = unix.Close(int(e.fd)) + closeFD(int(e.fd)) e.holdsFD = false w.closeFDOwed-- } @@ -558,7 +558,7 @@ func keptFD(fd int, owed bool) int { // drainPendingRelease closes at the last owed op's terminal CQE. func closeUnlessOwed(fd int, owed bool) { if !owed { - _ = unix.Close(fd) + closeFD(fd) } } @@ -728,7 +728,7 @@ func (w *Worker) endOwedOpsAtShutdown() { w.staleConnCQE(c, fd, ud) case udAccept: if c.Res >= 0 && !w.fixedFiles { - _ = unix.Close(int(c.Res)) + closeFD(int(c.Res)) } // No more completions from this accept: it is over, and // has let go of the listen socket's file. A multishot @@ -777,7 +777,7 @@ func (w *Worker) endOwedOpsAtShutdown() { } for i := range w.pendingRelease { if e := &w.pendingRelease[i]; e.holdsFD { - _ = unix.Close(int(e.fd)) + closeFD(int(e.fd)) e.holdsFD = false } } diff --git a/internal/engine/iouring/fd_ownership_1242_linux_test.go b/internal/engine/iouring/fd_ownership_1242_linux_test.go new file mode 100644 index 000000000..dcbacd6ca --- /dev/null +++ b/internal/engine/iouring/fd_ownership_1242_linux_test.go @@ -0,0 +1,583 @@ +//go:build linux + +package iouring + +import ( + "context" + "fmt" + "net" + "os" + "os/exec" + "runtime" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "golang.org/x/sys/unix" +) + +// celeris#1242. A stale-close canary (sockets of its own, checked by fstat) +// found one close of a descriptor number its owner no longer held, in the +// Goexit exit path of a worker. The number may by then name another +// connection or a sibling worker's listener, so a real server sees a random +// connection or listener vanish. +// +// fdTrap1242 is the ownership check behind fdCloseSeam. Every close the engine +// makes goes through it, and it does not close(2): it dup3s a placeholder (the +// read end of a pipe of its own) over the number, which closes what the number +// named and keeps the number taken. Nothing can re-open a number the trap +// holds, so a later close of a number that still names the placeholder is the +// engine closing something it no longer owns, whatever else runs, and the +// verdict does not depend on timing. A close of a number that is not open at +// all is the same fault with no reuse yet. A number that no longer names the +// placeholder was released by someone else and may be opened again: its next +// close is a first one. +type fdTrap1242 struct { + tomb int // the read end of the placeholder pipe + tombDev uint64 + tombIno uint64 + pipeSpare int // the write end, kept open so the placeholder never reads EOF + mu sync.Mutex + last map[int]string // number -> the site that closed it + bad []string + n atomic.Int64 + keep int // when > 0, the placeholders of the oldest closes are released past this many (a whole-package run would run out of descriptor numbers) + order []int + echo bool // also write each close and each fault to stderr as it happens (a panicking worker ends the process before any verdict) +} + +// newFDTrap1242 makes a trap with its placeholder; release gives back the +// descriptors it holds and the numbers it kept. +func newFDTrap1242() (*fdTrap1242, error) { + var p [2]int + if err := unix.Pipe2(p[:], unix.O_CLOEXEC); err != nil { + return nil, err + } + var st unix.Stat_t + if err := unix.Fstat(p[0], &st); err != nil { + return nil, err + } + return &fdTrap1242{tomb: p[0], pipeSpare: p[1], tombDev: st.Dev, tombIno: st.Ino, last: map[int]string{}}, nil +} + +func (tr *fdTrap1242) release() { + tr.mu.Lock() + defer tr.mu.Unlock() + for fd := range tr.last { + if tr.isPlaceholder(fd) { + _ = unix.Close(fd) + } + } + _ = unix.Close(tr.tomb) + _ = unix.Close(tr.pipeSpare) +} + +// installFDTrap1242 sets the seam for the test and, when the test ends, puts +// back what it replaced and releases the numbers the trap kept. +func installFDTrap1242(t *testing.T) *fdTrap1242 { + t.Helper() + tr, err := newFDTrap1242() + if err != nil { + t.Fatalf("fd trap: %v", err) + } + fn := tr.closeFD + prev := fdCloseSeam.Swap(&fn) + t.Cleanup(func() { + fdCloseSeam.Store(prev) + tr.release() + }) + return tr +} + +// isPlaceholder reports whether fd still names the trap's placeholder. +func (tr *fdTrap1242) isPlaceholder(fd int) bool { + var st unix.Stat_t + return unix.Fstat(fd, &st) == nil && st.Dev == tr.tombDev && st.Ino == tr.tombIno +} + +// site names the engine frames of the close, innermost first, skipping the +// seam's own. +func (tr *fdTrap1242) site() string { + pcs := make([]uintptr, 12) + n := runtime.Callers(4, pcs) // closeFD, closeFDErr, the seam, then the caller + var out []string + frames := runtime.CallersFrames(pcs[:n]) + for { + f, more := frames.Next() + name := f.Function[strings.LastIndex(f.Function, "/")+1:] + if !strings.HasPrefix(name, "testing.") && !strings.HasPrefix(name, "runtime.") { + out = append(out, fmt.Sprintf("%s (%s:%d)", name, f.File[strings.LastIndex(f.File, "/")+1:], f.Line)) + } + if !more || len(out) == 4 { + break + } + } + return strings.Join(out, " <- ") +} + +func (tr *fdTrap1242) fault(msg string) { + tr.bad = append(tr.bad, msg) + if tr.echo { + fmt.Fprintf(os.Stderr, "celeris1242 FAULT %s\n", msg) + } +} + +func (tr *fdTrap1242) closeFD(fd int) error { + tr.n.Add(1) + site := tr.site() + tr.mu.Lock() + defer tr.mu.Unlock() + if tr.echo { + fmt.Fprintf(os.Stderr, "celeris1242 CLOSE fd=%d at %s\n", fd, site) + } + if first, ok := tr.last[fd]; ok && tr.isPlaceholder(fd) { + tr.fault(fmt.Sprintf("fd %d closed a second time with nothing in between that could have re-opened it:\n\tfirst close: %s\n\tsecond close: %s", fd, first, site)) + return unix.EBADF + } + if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err != nil { + tr.fault(fmt.Sprintf("fd %d closed while not open (%v):\n\tclose: %s", fd, err, site)) + return err + } + if err := unix.Dup3(tr.tomb, fd, unix.O_CLOEXEC); err != nil { + return err + } + tr.last[fd] = site + if tr.keep > 0 { + for i, o := range tr.order { // the number was opened again since: one entry + if o == fd { + tr.order = append(tr.order[:i], tr.order[i+1:]...) + break + } + } + tr.order = append(tr.order, fd) + for len(tr.order) > tr.keep { + old := tr.order[0] + tr.order = tr.order[1:] + if tr.isPlaceholder(old) { + _ = unix.Close(old) + } + delete(tr.last, old) + } + } + return nil +} + +// verdict fails the test on every fault the trap saw, naming the number and +// both sites. +func (tr *fdTrap1242) verdict(t *testing.T) { + t.Helper() + tr.mu.Lock() + defer tr.mu.Unlock() + for _, b := range tr.bad { + t.Errorf("celeris#1242: %s", b) + } +} + +// fdID1242 is what a descriptor number named when a test took it: a test +// that tears down a descriptor the code under test may have closed already +// asks fdID1242.names first, and closes only a number that still names the +// same file. fdOpen704 (is anything open at this number) is not that +// question: a number the code under test freed is taken by the next open in +// the process (a sibling test's engine that is still running, the Go +// runtime's own sockets), and closing it then closes that one (celeris#1242). +// Sockets have an inode each; do not use it for io_uring rings on kernels +// that share one inode between them. +type fdID1242 struct{ dev, ino uint64 } + +func fdIdentity1242(t testing.TB, fd int) fdID1242 { + t.Helper() + var st unix.Stat_t + if err := unix.Fstat(fd, &st); err != nil { + t.Fatalf("fstat(%d): %v", fd, err) + } + return fdID1242{st.Dev, st.Ino} +} + +// names reports whether fd still names the file the identity was taken from. +func (id fdID1242) names(fd int) bool { + var st unix.Stat_t + return unix.Fstat(fd, &st) == nil && st.Dev == id.dev && st.Ino == id.ino +} + +// stopAtCleanup1242 stops the engine when the test ends, whether or not the +// test got as far as stop848, and waits for Listen to return. +func stopAtCleanup1242(t *testing.T, cancel context.CancelFunc, done chan error) { + t.Helper() + t.Cleanup(func() { + cancel() + select { + case err := <-done: + done <- err + case <-time.After(15 * time.Second): + t.Error("Listen did not return within 15 s of the cleanup's cancel") + } + }) +} + +// Every way a worker can end closes each descriptor number it owns once. The +// canary of celeris#1242 pointed at the Goexit path, and the engine's closes +// on it are single (the stale ones were two tests' own, above); the sub-tests +// pin that for each exit path, in a child process each (child848: a worker +// that Goexits leaves descriptors open, and a sibling test's leftover engine +// would add closes of its own to the trap): +// +// cancel the normal stop, with connections open +// pause-resume the pause's close, the re-created listener, the stop +// goexit runtime.Goexit in a handler (leaveGroupAfterExit) +// goexit-in-pause-close runtime.Goexit in what the pause's close runs (closingFD) +// ring-setup-failure one worker's ring cannot be set up (releaseFailedInit) +// initial-submit-failure the first accept cannot be submitted (releaseFailedInit) +// bind-failure the listen socket cannot be bound +// ring-wait-failure a ring wait fails for good: the loop returns through shutdown +// lost-listener the resume cannot re-create the listener: the loop returns +// +// The panic path ends the process: TestPanickedWorkerClosesEachDescriptorOnce1242. +func TestEveryExitPathClosesEachDescriptorOnce1242(t *testing.T) { + t.Run("cancel", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + e, _, cancel, done := start848(t, "127.0.0.1:0", false) + stopAtCleanup1242(t, cancel, done) + // Keep-alive connections the stop has to close. + for i := 0; i < 6; i++ { + c, err := net.DialTimeout("tcp", e.Addr().String(), 2*time.Second) + if err != nil { + t.Fatal(err) + } + defer func() { _ = c.Close() }() + _ = c.SetDeadline(time.Now().Add(2 * time.Second)) + _, _ = c.Write([]byte("GET /ok HTTP/1.1\r\nHost: x\r\n\r\n")) + if n, err := c.Read(make([]byte, 256)); err != nil || n == 0 { + t.Fatalf("PREMISE: /ok: %d bytes, %v", n, err) + } + } + stop848(t, cancel, done) + tr.requireCloses(t) + tr.verdict(t) + }) + t.Run("pause-resume", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + e, _, cancel, done := start848(t, "127.0.0.1:0", true) + stopAtCleanup1242(t, cancel, done) + for i := 0; i < 2; i++ { + if err := e.PauseAccept(); err != nil { + t.Fatalf("PauseAccept: %v", err) + } + if err := e.ResumeAccept(); err != nil { + t.Fatalf("ResumeAccept: %v", err) + } + } + // The workers re-create their listeners on the next iteration. + if _, err := okOnce848(e.Addr().String()); err != nil { + t.Fatalf("PREMISE: no answer after the resume: %v", err) + } + stop848(t, cancel, done) + tr.requireCloses(t) + tr.verdict(t) + }) + t.Run("goexit", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + e, _, cancel, done := start848(t, "127.0.0.1:0", true) + stopAtCleanup1242(t, cancel, done) + addr := e.Addr().String() + for i := 0; i < 12; i++ { + if _, err := okOnce848(addr); err != nil { + t.Fatal(err) + } + } + boom, err := net.DialTimeout("tcp", addr, 2*time.Second) + if err != nil { + t.Fatal(err) + } + defer func() { _ = boom.Close() }() + _, _ = boom.Write([]byte("GET /boom HTTP/1.1\r\nHost: x\r\n\r\n")) + for dl := time.Now().Add(5 * time.Second); time.Now().Before(dl) && e.abnormalWorkerExits.Load() == 0; time.Sleep(time.Millisecond) { + } + if got := e.abnormalWorkerExits.Load(); got != 1 { + t.Fatalf("PREMISE: abnormalWorkerExits = %d, want 1", got) + } + // The lifecycle the canary saw the stale close in: two pauses and + // resumes around the dead worker, then the stop. + for i := 0; i < 2; i++ { + if err := e.PauseAccept(); err != nil { + t.Fatalf("PauseAccept: %v", err) + } + if err := e.ResumeAccept(); err != nil { + t.Fatalf("ResumeAccept: %v", err) + } + } + stop848(t, cancel, done) + tr.requireCloses(t) + tr.verdict(t) + }) + t.Run("goexit-in-pause-close", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + e, _, cancel, done := start848(t, "127.0.0.1:0", true) + stopAtCleanup1242(t, cancel, done) + var fired atomic.Bool + setCancelAcceptSeam1229(t, func(*Worker, int) { + if fired.CompareAndSwap(false, true) { + runtime.Goexit() + } + }) + if err := e.PauseAccept(); err != nil { + t.Fatalf("PauseAccept: %v", err) + } + if e.abnormalWorkerExits.Load() != 1 { + t.Fatalf("PREMISE: abnormalWorkerExits = %d, want 1", e.abnormalWorkerExits.Load()) + } + if err := e.ResumeAccept(); err != nil { + t.Fatalf("ResumeAccept: %v", err) + } + stop848(t, cancel, done) + tr.requireCloses(t) + tr.verdict(t) + }) + t.Run("ring-setup-failure", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + e := newEngine656(t, freeAddr639(t), workers1242(t)) + failRingSetup656(t, func(call int32) bool { return call == 1 }) + listenExpectingInjectedFailure(t, e) + tr.requireCloses(t) + tr.verdict(t) + }) + t.Run("initial-submit-failure", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + e := newEngine656(t, freeAddr639(t), workers1242(t)) + orig := submitInitialAccept + submitInitialAccept = func(*Ring) (int, error) { return 0, errInjected656 } + t.Cleanup(func() { submitInitialAccept = orig }) + listenExpectingInjectedFailure(t, e) + tr.requireCloses(t) + tr.verdict(t) + }) + t.Run("bind-failure", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + hold, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer func() { _ = hold.Close() }() + e := newEngine656(t, hold.Addr().String(), workers1242(t)) + if err := e.Listen(t.Context()); err == nil || !strings.Contains(err.Error(), "address already in use") { + t.Fatalf("PREMISE: Listen on a held port returned %v, want address already in use", err) + } + tr.requireCloses(t) + tr.verdict(t) + }) + t.Run("ring-wait-failure", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + var failed atomic.Pointer[Worker] + setRingWaitSeam884(t, func(w *Worker, _ ringWaitMode, err error) error { + if err == nil && failed.CompareAndSwap(nil, w) { + // Not EBUSY or EAGAIN, which the loop reaps and retries. + return fmt.Errorf("io_uring_enter submit+wait: %w", unix.EIO) + } + return err + }) + e, logs, cancel, done := start848(t, "127.0.0.1:0", false) + stopAtCleanup1242(t, cancel, done) + for dl := time.Now().Add(5 * time.Second); time.Now().Before(dl); time.Sleep(time.Millisecond) { + if w := failed.Load(); w != nil && w.exited.Load() { + break + } + } + if w := failed.Load(); w == nil || !w.exited.Load() { + t.Fatal("PREMISE: no worker left its loop after a failed ring wait") + } + if e.abnormalWorkerExits.Load() != 0 || len(logs.exits()) != 0 { + t.Fatalf("PREMISE: a failed ring wait is a return, not a Goexit: counted %d, logged %d", e.abnormalWorkerExits.Load(), len(logs.exits())) + } + stop848(t, cancel, done) + tr.requireCloses(t) + tr.verdict(t) + }) + t.Run("lost-listener", func(t *testing.T) { + if !child848(t) { + return + } + tr := installFDTrap1242(t) + addr := freeAddr639(t) + e, _, cancel, done := start848(t, addr, true) + stopAtCleanup1242(t, cancel, done) + if err := e.PauseAccept(); err != nil { + t.Fatal(err) + } + ln, err := net.Listen("tcp", addr) + if err != nil { + t.Fatalf("a fixed port has no hold: %v", err) + } + defer func() { _ = ln.Close() }() + if err := e.ResumeAccept(); err != nil { + t.Fatal(err) + } + want := uint64(e.Metrics().Workers) + var lost uint64 + for dl := time.Now().Add(5 * time.Second); time.Now().Before(dl) && lost < want; time.Sleep(5 * time.Millisecond) { + lost = e.Metrics().ErrorListenerRecreate + } + if want == 0 || lost != want { + t.Fatalf("PREMISE: ErrorListenerRecreate = %d, want %d (one per worker)", lost, want) + } + stop848(t, cancel, done) + tr.requireCloses(t) + tr.verdict(t) + }) +} + +// workers1242 is two workers, so that one can fail and its sibling be +// cancelled, or the default count (capped to one) where memlock funds no more +// (the unit job's 8 MiB): a configured count under two is refused. +func workers1242(t *testing.T) int { + t.Helper() + needWorkers895(t, 1) + if maxW := MaxWorkersForMemlock(); maxW >= 0 && maxW < 2 { + return 0 + } + return 2 +} + +// requireCloses fails the test when the engine closed nothing through the +// seam: a green verdict of a trap that saw nothing would prove nothing. +func (tr *fdTrap1242) requireCloses(t *testing.T) { + t.Helper() + if tr.n.Load() == 0 { + t.Fatal("PREMISE: the engine closed nothing through fdCloseSeam") + } +} + +// A panic that escapes a handler on the worker goroutine ends the process, +// after the deferred report has run leaveGroupAfterExit, so the verdict is the +// child's own output: the trap writes each close and each fault to stderr as +// it happens, and the parent reads them. Same shape as +// TestSyncWorkerPanicIsRecordedBeforeTheCrash848. +func TestPanickedWorkerClosesEachDescriptorOnce1242(t *testing.T) { + const childEnv = "CELERIS_1242_PANIC_CHILD" + if os.Getenv(childEnv) == "1" { + tr := installFDTrap1242(t) + tr.echo = true + e, _, _, _ := start848(t, "127.0.0.1:0", false) + c, err := net.DialTimeout("tcp", e.Addr().String(), 2*time.Second) + if err != nil { + t.Fatal(err) + } + _, _ = c.Write([]byte("GET /panic HTTP/1.1\r\nHost: x\r\n\r\n")) + time.Sleep(5 * time.Second) + t.Log("celeris1242 child survived the panic") + return + } + exe, err := os.Executable() + if err != nil { + t.Fatalf("os.Executable: %v", err) + } + cmd := exec.Command(exe, "-test.run=^TestPanickedWorkerClosesEachDescriptorOnce1242$", "-test.v", "-test.count=1", "-test.timeout=60s") + cmd.Env = append(os.Environ(), childEnv+"=1") + out, err := cmd.CombinedOutput() + text := string(out) + if strings.Contains(text, "--- SKIP") { + t.Skipf("skipped in the child process:\n%s", text) + } + if err == nil || !strings.Contains(text, "panic: celeris848 panic in a handler") { + t.Fatalf("PREMISE: the child did not die of the panic (err %v):\n%.3000s", err, text) + } + // The listener and the ring are the descriptors leaveGroupAfterExit closes. + if closes := strings.Count(text, "celeris1242 CLOSE fd="); closes < 2 { + t.Fatalf("PREMISE: %d closes seen before the crash, want at least the listener and the ring:\n%.3000s", closes, text) + } + for rest := text; ; { + i := strings.Index(rest, "celeris1242 FAULT ") + if i < 0 { + break + } + rest = rest[i+len("celeris1242 FAULT "):] + t.Errorf("celeris#1242: %.400s", rest) + } +} + +// faultsFor returns the faults reported for descriptor number fd. +func (tr *fdTrap1242) faultsFor(fd int) []string { + tr.mu.Lock() + defer tr.mu.Unlock() + var out []string + for _, b := range tr.bad { + if strings.HasPrefix(b, fmt.Sprintf("fd %d ", fd)) { + out = append(out, b) + } + } + return out +} + +// The trap itself: a second close of a number nothing re-opened is a fault +// naming the number and both sites; a number that was released and opened +// again is closed once more without one; a close of a number that is not +// open is a fault too. +func TestFDTrapFlagsOnlyAStaleClose1242(t *testing.T) { + tr := installFDTrap1242(t) + sock := func() int { + fd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0) + if err != nil { + t.Fatal(err) + } + return fd + } + + fd := sock() + closeFD(fd) + if f := tr.faultsFor(fd); len(f) != 0 { + t.Fatalf("a first close is a fault: %v", f) + } + closeFD(fd) + if f := tr.faultsFor(fd); len(f) != 1 || !strings.Contains(f[0], "closed a second time") || + strings.Count(f[0], "TestFDTrapFlagsOnlyAStaleClose1242") != 2 { + t.Fatalf("a second close was not reported with the number and both sites: %v", f) + } + tr.mu.Lock() + tr.bad = nil // this test's verdict is the checks here + tr.mu.Unlock() + + // Released by someone else, and taken again: the next close is a first one. + fd = sock() + closeFD(fd) + if err := unix.Close(fd); err != nil { // the placeholder, closed by its owner + t.Fatal(err) + } + got := sock() + closeFD(got) + if got == fd && len(tr.faultsFor(fd)) != 0 { + t.Fatalf("a close of a number that was released and opened again is a fault: %v", tr.faultsFor(fd)) + } + + const never = 1 << 20 + closeFD(never) + if f := tr.faultsFor(never); len(f) != 1 || !strings.Contains(f[0], "closed while not open") { + t.Fatalf("a close of a number that is not open was not reported: %v", f) + } + tr.mu.Lock() + tr.bad = nil + tr.mu.Unlock() +} diff --git a/internal/engine/iouring/probe.go b/internal/engine/iouring/probe.go index 47d048990..fb92346e0 100644 --- a/internal/engine/iouring/probe.go +++ b/internal/engine/iouring/probe.go @@ -807,7 +807,7 @@ func probeMultishotAcceptOnce() (bool, string, probeOutcome) { // Close the accepted FD so it doesn't leak — we have its fd in res. if res > 0 { - _ = unix.Close(int(res)) + closeFD(int(res)) } return multishotAcceptCQE(res, flags) } diff --git a/internal/engine/iouring/ring.go b/internal/engine/iouring/ring.go index 50b402660..71d705c8f 100644 --- a/internal/engine/iouring/ring.go +++ b/internal/engine/iouring/ring.go @@ -203,7 +203,7 @@ func NewRingCPU(entries uint32, flags uint32, sqPollIdle uint32, cpuID int) (*Ri } if err := r.mmap(); err != nil { - _ = unix.Close(int(fd)) + closeFD(int(fd)) return nil, err } @@ -609,5 +609,5 @@ func (r *Ring) Close() error { if r.sqRing != nil { _ = unix.Munmap(r.sqRing) } - return unix.Close(r.fd) + return closeFDErr(r.fd) } diff --git a/internal/engine/iouring/shutdown_gate_884_linux_test.go b/internal/engine/iouring/shutdown_gate_884_linux_test.go index b8ac89b59..8af07e029 100644 --- a/internal/engine/iouring/shutdown_gate_884_linux_test.go +++ b/internal/engine/iouring/shutdown_gate_884_linux_test.go @@ -204,6 +204,10 @@ func TestGateLookAtAConn884(t *testing.T) { // of a socketpair each), each idle HTTP/1, as closeConn needs to close them. func gateRig884(t *testing.T, n int) (*Worker, []*connState, []int) { t.Helper() + // Every descriptor of a rig is closed once, whoever closes it: the pass + // under test, the test, or the rig's cleanup (celeris#1242). + tr := installFDTrap1242(t) + t.Cleanup(func() { tr.verdict(t) }) ring := newTestRing(t) w := &Worker{ ring: ring, @@ -218,6 +222,7 @@ func gateRig884(t *testing.T, n int) (*Worker, []*connState, []int) { w.cachedNow = mono.Now() var css []*connState var peers []int + var ids []fdID1242 for range n { fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0) if err != nil { @@ -230,6 +235,7 @@ func gateRig884(t *testing.T, n int) (*Worker, []*connState, []int) { cs.buf = make([]byte, 4096) css = append(css, cs) peers = append(peers, peer) + ids = append(ids, fdIdentity1242(t, local)) } w.conns = make([]*connState, 0) maxfd := 0 @@ -245,9 +251,11 @@ func gateRig884(t *testing.T, n int) (*Worker, []*connState, []int) { } t.Cleanup(func() { // What the test left open: closeConn's close is deferred behind the - // recv-cancel's completion, which no loop runs here. + // recv-cancel's completion, which no loop runs here. A conn a pass + // closed, or a test closed itself, is not closed again: its number is + // free, and the next open in the process takes it (celeris#1242). for _, cs := range css { - _ = unix.Close(cs.fd) + closeFD(cs.fd) } }) return w, css, peers @@ -332,8 +340,9 @@ func TestGatePassWaitsForTheKernelSendQueue884(t *testing.T) { w.removeLiveConn(old) w.clearConnSlot(old.fd) w.connCount-- - _ = unix.Close(old.fd) + closeFD(old.fd) _ = peers + _ = fdIdentity1242(t, local) cs := gateConn884(local) cs.generation = 9 cs.buf = make([]byte, 4096) @@ -374,7 +383,8 @@ func TestGatePassWaitsForTheKernelSendQueue884(t *testing.T) { if w.conns[local] == cs { t.Error("the pass left an idle conn with an empty send queue open") } - _ = unix.Close(local) + // The pass closed it when the test passes: not closed again. + closeFD(local) } // TestGateReadsOnNeverReadsH1StateOfAGoroutinesConn884 is the regression test of diff --git a/internal/engine/iouring/transplant.go b/internal/engine/iouring/transplant.go index 7fdcccb61..6577c0e03 100644 --- a/internal/engine/iouring/transplant.go +++ b/internal/engine/iouring/transplant.go @@ -111,7 +111,7 @@ func (w *Worker) refuseAdopt(fd int, carry engine.Carryover) { } if fd >= 0 { _ = unix.Shutdown(fd, unix.SHUT_WR) - _ = unix.Close(fd) + closeFD(fd) } if w.closeCount != nil { w.closeCount.Add(1) diff --git a/internal/engine/iouring/transplant_source.go b/internal/engine/iouring/transplant_source.go index dae0eaee8..488138ad5 100644 --- a/internal/engine/iouring/transplant_source.go +++ b/internal/engine/iouring/transplant_source.go @@ -178,7 +178,7 @@ func (w *Worker) handOff(cs *connState, fd int, h *transplantTargetHolder, detac return false } if serr := unix.SetNonblock(newFD, true); serr != nil { - _ = unix.Close(newFD) + closeFD(newFD) cs.reapSuppressed = true return false } @@ -218,7 +218,7 @@ func (w *Worker) handOff(cs *connState, fd int, h *transplantTargetHolder, detac } else { w.queuePendingRelease(cs) } - _ = unix.Close(fd) + closeFD(fd) if err := h.target.AdoptConn(newFD, carry); err != nil { w.reclaimTransplant(newFD, carry, err) diff --git a/internal/engine/iouring/worker.go b/internal/engine/iouring/worker.go index be64d3533..e782edb8d 100644 --- a/internal/engine/iouring/worker.go +++ b/internal/engine/iouring/worker.go @@ -1543,7 +1543,7 @@ func (w *Worker) run(ctx context.Context) { // celeris#655: producers hold the handle, never the number, so the // descriptor cannot be written once shutdown has closed it. if !w.wakeFD.Set(efd) && efd >= 0 { - _ = unix.Close(efd) + closeFD(efd) } w.prepareAccept() @@ -2731,7 +2731,7 @@ func (w *Worker) onAcceptedFD(ctx context.Context, newFD int, now int64, isFixed // Bounds check: reject FDs outside the flat conn array. if newFD < 0 || newFD >= len(w.conns) { if !isFixedFile { - _ = unix.Close(newFD) + closeFD(newFD) } w.errs.ConnTableCap.Add(1) return @@ -2901,7 +2901,7 @@ func (w *Worker) closeListenerAfterDrain(ctx context.Context) { // accept it now instead and serve it like any other connection // (celeris#662). w.acceptQueuedOnPause(ctx, lfd) - _ = unix.Close(lfd) + closeFD(lfd) w.closingFD = 0 w.lingerUntil = 0 deferlinger.NoteClose() @@ -3026,7 +3026,7 @@ func (w *Worker) stopAccepting(ctx context.Context) { w.listenFD = -1 // before the cancel's completions: handleAccept re-arms on a listenFD >= 0 w.closingFD = int32(lfd) + 1 w.cancelAccept(ctx, lfd) - _ = unix.Close(lfd) + closeFD(lfd) w.closingFD = 0 w.lingerUntil = 0 } @@ -3078,13 +3078,13 @@ func (w *Worker) leaveGroupAfterExit() (keptZC int) { w.ring = nil } if w.listenFD >= 0 { - _ = unix.Close(w.listenFD) + closeFD(w.listenFD) w.listenFD = -1 } if w.closingFD > 0 { // The Goexit came from a handler the pause's close or stopAccepting // ran, after they took the descriptor out of listenFD. - _ = unix.Close(int(w.closingFD) - 1) + closeFD(int(w.closingFD) - 1) w.closingFD = 0 } w.lingerUntil = 0 @@ -8629,7 +8629,7 @@ func (w *Worker) shutdown() { // endOwedOpsAtShutdown has ended the ops owed on fd, so no op can // resolve the number once it is free (celeris#685). if !cs.fixedFile { - _ = unix.Close(fd) + closeFD(fd) } // Do NOT releaseConnState here, detached or not. Conns being torn // down by shutdown almost always have a recv SQE armed on cs.buf, @@ -8653,7 +8653,7 @@ func (w *Worker) shutdown() { // retracts it — the sweep does not run after shutdown. w.sweepRetract() if w.listenFD >= 0 { - _ = unix.Close(w.listenFD) + closeFD(w.listenFD) w.listenFD = -1 // a Goexit in a later step must not close the number twice (leaveGroupAfterExit) } // The worker has left the SO_REUSEPORT group for good. PauseAccept waits @@ -8765,7 +8765,7 @@ func (w *Worker) releaseFailedInit() { // unchanged. w.wakeFD.Close() if w.listenFD >= 0 { - _ = unix.Close(w.listenFD) + closeFD(w.listenFD) w.listenFD = -1 // Keep the invariant the running loop maintains (celeris#656): the // socket is out of the SO_REUSEPORT group, so the flag PauseAccept @@ -8798,11 +8798,11 @@ func createListenSocket(addr string, deferAccept bool) (int, error) { } if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1); err != nil { - _ = unix.Close(fd) + closeFD(fd) return -1, fmt.Errorf("SO_REUSEADDR: %w", err) } if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil { - _ = unix.Close(fd) + closeFD(fd) return -1, fmt.Errorf("SO_REUSEPORT: %w", err) } @@ -8818,12 +8818,12 @@ func createListenSocket(addr string, deferAccept bool) (int, error) { if err := bindiag.BindWithRetry(fd, sa); err != nil { diag := bindiag.Format(fd, sa) - _ = unix.Close(fd) + closeFD(fd) return -1, fmt.Errorf("bind: %w [%s]", err, diag) } if err := unix.Listen(fd, 4096); err != nil { diag := bindiag.Format(fd, sa) - _ = unix.Close(fd) + closeFD(fd) return -1, fmt.Errorf("listen: %w [%s]", err, diag) } diff --git a/internal/engine/iouring/worker_exit_order_884_linux_test.go b/internal/engine/iouring/worker_exit_order_884_linux_test.go index 3f04e789a..c069cdd48 100644 --- a/internal/engine/iouring/worker_exit_order_884_linux_test.go +++ b/internal/engine/iouring/worker_exit_order_884_linux_test.go @@ -32,6 +32,8 @@ import ( // (listenFDClosed, which PauseAccept waits on). Red with the ring's Close // removed, or with the listener's. func TestLeaveGroupAfterExitClosesTheRingAndTheListener884(t *testing.T) { + tr := installFDTrap1242(t) + t.Cleanup(func() { tr.verdict(t) }) ring, err := NewRing(64, 0, 0) if err != nil { skipOrFail656(t, "io_uring unavailable: %v", err) @@ -52,9 +54,14 @@ func TestLeaveGroupAfterExitClosesTheRingAndTheListener884(t *testing.T) { } sa, _ := unix.Getsockname(lfd) addr := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: sa.(*unix.SockaddrInet4).Port} + // What leaveGroupAfterExit has closed is not closed again here: the + // numbers are free the moment it returns, and whatever opens next in the + // process gets one (celeris#1242). + var w *Worker + lfdID, ringID := fdIdentity1242(t, lfd), fdIdentity1242(t, ringFD) t.Cleanup(func() { if fdOpen704(lfd) { - _ = unix.Close(lfd) + closeFD(lfd) } if fdOpen704(ringFD) { _ = ring.Close() @@ -67,17 +74,17 @@ func TestLeaveGroupAfterExitClosesTheRingAndTheListener884(t *testing.T) { _ = c.Close() } - w := &Worker{id: 3, ring: ring, listenFD: lfd, handoffLoss: &handoffLossStats{}} + w = &Worker{id: 3, ring: ring, listenFD: lfd, handoffLoss: &handoffLossStats{}} w.leaveGroupAfterExit() if _, err := net.DialTimeout("tcp", addr.String(), time.Second); !errors.Is(err, syscall.ECONNREFUSED) { t.Errorf("a connect after leaveGroupAfterExit gave %v, want connection refused: the listener is still in the group", err) } - if w.ring != nil || fdOpen704(ringFD) { - t.Errorf("the ring is not closed after leaveGroupAfterExit: w.ring=%v fd open=%v", w.ring != nil, fdOpen704(ringFD)) + if w.ring != nil || ringID.names(ringFD) { + t.Errorf("the ring is not closed after leaveGroupAfterExit: w.ring=%v fd open=%v", w.ring != nil, ringID.names(ringFD)) } - if w.listenFD != -1 || fdOpen704(lfd) { - t.Errorf("the listener is not closed after leaveGroupAfterExit: listenFD=%d fd open=%v", w.listenFD, fdOpen704(lfd)) + if w.listenFD != -1 || lfdID.names(lfd) { + t.Errorf("the listener is not closed after leaveGroupAfterExit: listenFD=%d fd open=%v", w.listenFD, lfdID.names(lfd)) } if !w.listenFDClosed.Load() { t.Error("listenFDClosed is not set after leaveGroupAfterExit: a pause would wait for it") @@ -163,6 +170,8 @@ func TestGoexitedWorkerLeavesTheGroupBeforeItIsMarkedExited884(t *testing.T) { // be set; and a goroutine told by the pause's channel (the notify) must find it // set too. Red with leaveGroupAfterExit moved after the flag and the notify. func TestReportWorkerExitLeavesTheGroupBeforeTheFlag884(t *testing.T) { + tr := installFDTrap1242(t) + t.Cleanup(func() { tr.verdict(t) }) const rounds = 400 e := &Engine{} early := 0 @@ -179,6 +188,7 @@ func TestReportWorkerExitLeavesTheGroupBeforeTheFlag884(t *testing.T) { _ = unix.Close(lfd) t.Fatal(err) } + lfdID := fdIdentity1242(t, lfd) w := &Worker{id: round, listenFD: lfd, handoffLoss: &handoffLossStats{}, pause: &deferlinger.PauseState{}} changed := w.pause.Changed() saw := make(chan bool, 1) @@ -200,8 +210,10 @@ func TestReportWorkerExitLeavesTheGroupBeforeTheFlag884(t *testing.T) { if !<-notified { early++ } - if fdOpen704(lfd) { - _ = unix.Close(lfd) + if w.listenFD != -1 || lfdID.names(lfd) { + // Only a number that still names the listener: a closed one was + // taken by the next open in the process (celeris#1242). + closeFD(lfd) t.Fatalf("round %d: reportWorkerExit returned with the listener open", round) } } diff --git a/internal/engine/iouring/zc_send_buffer.go b/internal/engine/iouring/zc_send_buffer.go index c9b0899e6..968352dfa 100644 --- a/internal/engine/iouring/zc_send_buffer.go +++ b/internal/engine/iouring/zc_send_buffer.go @@ -5,8 +5,6 @@ package iouring import ( "sync" "time" - - "golang.org/x/sys/unix" ) // The send buffer of a SEND_ZC (celeris#812). @@ -314,7 +312,7 @@ func (w *Worker) retainZCSendBufsAtShutdown() { w.staleConnCQE(c, int(ud&fdMask), ud) case udAccept: if c.Res >= 0 && !w.fixedFiles { - _ = unix.Close(int(c.Res)) + closeFD(int(c.Res)) } } } From 9c5fe752916d9ee1caad7b4f8859dc58de6824d6 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Tue, 3 Nov 2026 12:36:18 +0100 Subject: [PATCH 2/4] iouring tests: close a descriptor only when the code under test has not (Refs #1242) Seven tests closed a number the engine had already closed: the cleanup of TestLeaveGroupAfterExitClosesTheRingAndTheListener884 (an open-check, then Ring.Close and close(2) again), the shutdown gate rig and TestGatePassWaitsForTheKernelSendQueue884, and the close-accounting, unregister-close, transplant egress, refused-adopt and wake-eventfd tests. Once a number is free the next open in the process takes it, so the second close destroys whatever that is: the canary's sockets, a sibling test's engine still shutting down, the Go runtime's own. A test now closes a number only while it still names the file it was taken for (fdID1242). --- internal/engine/iouring/close_accounting_test.go | 6 +++++- .../engine/iouring/driver_unregister_close_test.go | 3 ++- .../engine/iouring/shutdown_gate_884_linux_test.go | 12 ++++++++---- internal/engine/iouring/transplant_egress_test.go | 6 +++--- .../iouring/transplant_handoff_failure_test.go | 3 ++- .../engine/iouring/wakefd_after_shutdown_test.go | 13 +++++++++++-- .../iouring/worker_exit_order_884_linux_test.go | 8 +++++--- 7 files changed, 36 insertions(+), 15 deletions(-) diff --git a/internal/engine/iouring/close_accounting_test.go b/internal/engine/iouring/close_accounting_test.go index fc9a76a89..70acf4a97 100644 --- a/internal/engine/iouring/close_accounting_test.go +++ b/internal/engine/iouring/close_accounting_test.go @@ -53,6 +53,7 @@ func TestFinishCloseCountsAMissingConnState(t *testing.T) { t.Run(tc.name, func(t *testing.T) { fd, other := socketPairFDs(t) defer func() { _ = unix.Close(other) }() + fdID := fdIdentity1242(t, fd) // Non-blocking so the close path's recv drain can never park on // a socketpair that has no data and a peer still open. _ = unix.SetNonblock(fd, true) @@ -95,7 +96,10 @@ func TestFinishCloseCountsAMissingConnState(t *testing.T) { if got := w.activeConns.Load(); got != 0 { t.Errorf("activeConns = %d, want 0", got) } - if !tc.nilState { + // The close path closed it, unless it stopped short: not closed + // twice, the number is free once the first close is made + // (celeris#1242). + if fdID.names(fd) { _ = unix.Close(fd) } }) diff --git a/internal/engine/iouring/driver_unregister_close_test.go b/internal/engine/iouring/driver_unregister_close_test.go index 64c48829a..d25636acf 100644 --- a/internal/engine/iouring/driver_unregister_close_test.go +++ b/internal/engine/iouring/driver_unregister_close_test.go @@ -662,10 +662,11 @@ func TestDriverFailureCloseCompletionSparesConnOnReusedNumber(t *testing.T) { select { case err := <-n.closed: t.Errorf("N: onClose(%v) fired, with N never unregistered: a close completion of A's reached it (celeris#707)", err) + _ = unix.Close(n.fd) default: + // Closes n.fd and its peer. n.unregisterAndWait(t, wl) } - _ = unix.Close(n.fd) q.d.unregisterAndWait(t, wl) p.d.unregisterAndWait(t, wl) } diff --git a/internal/engine/iouring/shutdown_gate_884_linux_test.go b/internal/engine/iouring/shutdown_gate_884_linux_test.go index 8af07e029..7798d1dec 100644 --- a/internal/engine/iouring/shutdown_gate_884_linux_test.go +++ b/internal/engine/iouring/shutdown_gate_884_linux_test.go @@ -254,8 +254,10 @@ func gateRig884(t *testing.T, n int) (*Worker, []*connState, []int) { // recv-cancel's completion, which no loop runs here. A conn a pass // closed, or a test closed itself, is not closed again: its number is // free, and the next open in the process takes it (celeris#1242). - for _, cs := range css { - closeFD(cs.fd) + for i, cs := range css { + if ids[i].names(cs.fd) { + closeFD(cs.fd) + } } }) return w, css, peers @@ -342,7 +344,7 @@ func TestGatePassWaitsForTheKernelSendQueue884(t *testing.T) { w.connCount-- closeFD(old.fd) _ = peers - _ = fdIdentity1242(t, local) + localID := fdIdentity1242(t, local) cs := gateConn884(local) cs.generation = 9 cs.buf = make([]byte, 4096) @@ -384,7 +386,9 @@ func TestGatePassWaitsForTheKernelSendQueue884(t *testing.T) { t.Error("the pass left an idle conn with an empty send queue open") } // The pass closed it when the test passes: not closed again. - closeFD(local) + if localID.names(local) { + closeFD(local) + } } // TestGateReadsOnNeverReadsH1StateOfAGoroutinesConn884 is the regression test of diff --git a/internal/engine/iouring/transplant_egress_test.go b/internal/engine/iouring/transplant_egress_test.go index 603437d27..9eb61fa90 100644 --- a/internal/engine/iouring/transplant_egress_test.go +++ b/internal/engine/iouring/transplant_egress_test.go @@ -49,6 +49,7 @@ func TestFinishAsyncTransplantHoldsForInflightEgress(t *testing.T) { t.Run(tc.name, func(t *testing.T) { fd, other := socketPairFDs(t) defer func() { _ = unix.Close(other) }() + fdID := fdIdentity1242(t, fd) w := newDirtyTestWorker(fd) tgt := &recordingTarget{} @@ -83,10 +84,9 @@ func TestFinishAsyncTransplantHoldsForInflightEgress(t *testing.T) { } // A held-back transplant must leave the original fd open: the // conn stays in place and is retried at its next park boundary. - _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0) - closed := err != nil + closed := !fdID.names(fd) if closed != tc.wantClosed { - t.Errorf("original fd closed = %v, want %v (err=%v)", closed, tc.wantClosed, err) + t.Errorf("original fd closed = %v, want %v", closed, tc.wantClosed) } if !closed { _ = unix.Close(fd) diff --git a/internal/engine/iouring/transplant_handoff_failure_test.go b/internal/engine/iouring/transplant_handoff_failure_test.go index 6e8ba4f4e..72b900856 100644 --- a/internal/engine/iouring/transplant_handoff_failure_test.go +++ b/internal/engine/iouring/transplant_handoff_failure_test.go @@ -32,6 +32,7 @@ func newRefusalWorker(fds ...int) *Worker { func TestRefuseAdoptClosesAndFiresTheHook(t *testing.T) { fd, other := socketPairFDs(t) defer func() { _ = unix.Close(other) }() + fdID := fdIdentity1242(t, fd) w := newRefusalWorker() // Size the conn table so this exact descriptor is one slot past its end, @@ -68,7 +69,7 @@ func TestRefuseAdoptClosesAndFiresTheHook(t *testing.T) { if got := w.transplantCount.Load(); got != 0 { t.Errorf("transplantCount = %d, want 0 — a refused adopt is not an adopt", got) } - if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err == nil { + if fdID.names(fd) { t.Error("the refused descriptor was left open") _ = unix.Close(fd) } diff --git a/internal/engine/iouring/wakefd_after_shutdown_test.go b/internal/engine/iouring/wakefd_after_shutdown_test.go index 2b854d28c..f569feac6 100644 --- a/internal/engine/iouring/wakefd_after_shutdown_test.go +++ b/internal/engine/iouring/wakefd_after_shutdown_test.go @@ -50,14 +50,23 @@ func newWakeWorker655(t *testing.T) (*Worker, int) { } // socketFor655 returns one end of a socketpair. Both ends are closed by -// cleanup; nothing under test closes a driver-registered fd. +// cleanup, unless a shutdown under test closed one already. func socketFor655(t *testing.T) int { t.Helper() pair, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0) if err != nil { t.Fatalf("socketpair: %v", err) } - t.Cleanup(func() { _ = unix.Close(pair[0]); _ = unix.Close(pair[1]) }) + ids := [2]fdID1242{fdIdentity1242(t, pair[0]), fdIdentity1242(t, pair[1])} + t.Cleanup(func() { + // A shutdown that ran over a conn on it closed it already: not again, + // the number is free by then (celeris#1242). + for i, fd := range pair { + if ids[i].names(fd) { + _ = unix.Close(fd) + } + } + }) return pair[0] } diff --git a/internal/engine/iouring/worker_exit_order_884_linux_test.go b/internal/engine/iouring/worker_exit_order_884_linux_test.go index c069cdd48..35f7ea3d0 100644 --- a/internal/engine/iouring/worker_exit_order_884_linux_test.go +++ b/internal/engine/iouring/worker_exit_order_884_linux_test.go @@ -60,10 +60,10 @@ func TestLeaveGroupAfterExitClosesTheRingAndTheListener884(t *testing.T) { var w *Worker lfdID, ringID := fdIdentity1242(t, lfd), fdIdentity1242(t, ringFD) t.Cleanup(func() { - if fdOpen704(lfd) { + if lfdID.names(lfd) && (w == nil || w.listenFD >= 0) { closeFD(lfd) } - if fdOpen704(ringFD) { + if ringID.names(ringFD) && (w == nil || w.ring != nil) { _ = ring.Close() } }) @@ -213,7 +213,9 @@ func TestReportWorkerExitLeavesTheGroupBeforeTheFlag884(t *testing.T) { if w.listenFD != -1 || lfdID.names(lfd) { // Only a number that still names the listener: a closed one was // taken by the next open in the process (celeris#1242). - closeFD(lfd) + if lfdID.names(lfd) { + closeFD(lfd) + } t.Fatalf("round %d: reportWorkerExit returned with the listener open", round) } } From 0f54737c3c63322337136934f56f40f6f1b252a6 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Tue, 3 Nov 2026 13:32:11 +0100 Subject: [PATCH 3/4] iouring tests: make a removed trap harmless, check the ring and the nil-state close properly, and list the hijack close beside wakefd (Refs #1242) A trap that has been removed closes plainly, so a worker an earlier test leaked cannot dup3 a placeholder that is gone onto a reused number. The ring of the leave-group test is judged by the worker's own state and by what the number names (an io_uring file), not by an inode that rings may share. The nil-state close test fails when finishClose leaves the descriptor open. The unregister-close test no longer dup3s over a number that something else took. The seam's comment names the hijack close as outside it. --- .../engine/iouring/close_accounting_test.go | 9 ++-- .../iouring/driver_unregister_close_test.go | 5 ++ internal/engine/iouring/fd_close_seam_1242.go | 10 ++-- .../iouring/fd_ownership_1242_linux_test.go | 49 ++++++++++++++++++- .../worker_exit_order_884_linux_test.go | 9 ++-- 5 files changed, 71 insertions(+), 11 deletions(-) diff --git a/internal/engine/iouring/close_accounting_test.go b/internal/engine/iouring/close_accounting_test.go index 70acf4a97..6dd78b09f 100644 --- a/internal/engine/iouring/close_accounting_test.go +++ b/internal/engine/iouring/close_accounting_test.go @@ -96,9 +96,12 @@ func TestFinishCloseCountsAMissingConnState(t *testing.T) { if got := w.activeConns.Load(); got != 0 { t.Errorf("activeConns = %d, want 0", got) } - // The close path closed it, unless it stopped short: not closed - // twice, the number is free once the first close is made - // (celeris#1242). + // The nil-state path ends in the close; the live one may stop short + // at the ring this unit Worker does not have. Not closed twice: + // the number is free once the first close is made (celeris#1242). + if tc.nilState && fdID.names(fd) { + t.Error("finishClose left the descriptor open") + } if fdID.names(fd) { _ = unix.Close(fd) } diff --git a/internal/engine/iouring/driver_unregister_close_test.go b/internal/engine/iouring/driver_unregister_close_test.go index d25636acf..0bbe1cb5a 100644 --- a/internal/engine/iouring/driver_unregister_close_test.go +++ b/internal/engine/iouring/driver_unregister_close_test.go @@ -640,6 +640,11 @@ func TestDriverFailureCloseCompletionSparesConnOnReusedNumber(t *testing.T) { n.fd, n.peer = nonblockSocketPair(t) defer n.closePeer() if n.fd != a.fd { + // A's number is free (closed above): dup3 over a number that is open + // closes whatever took it, so only a free one is taken (celeris#1242). + if fdOpen704(a.fd) { + t.Fatalf("PREMISE: descriptor %d, the number of A, was taken by another open", a.fd) + } if err := unix.Dup3(n.fd, a.fd, unix.O_CLOEXEC); err != nil { t.Fatalf("dup3: %v", err) } diff --git a/internal/engine/iouring/fd_close_seam_1242.go b/internal/engine/iouring/fd_close_seam_1242.go index 2c026bded..21a6ca755 100644 --- a/internal/engine/iouring/fd_close_seam_1242.go +++ b/internal/engine/iouring/fd_close_seam_1242.go @@ -25,9 +25,13 @@ var fdCloseSeam atomic.Pointer[func(fd int) error] // closeFDErr is close(2) of one descriptor number the engine owns, through // fdCloseSeam. Every close of an engine-owned descriptor in this package goes // through here (or closeFD, which drops the error): a bare unix.Close is -// invisible to the seam test of celeris#1242. The one other closer is the -// wake eventfd's handle (internal/wakefd), which retires the number under its -// lock before it closes it (celeris#655) and so cannot close it twice. +// invisible to the seam test of celeris#1242. Two closes are outside it, by +// design. The wake eventfd's handle (internal/wakefd) retires the number +// under its lock before it closes it (celeris#655) and so cannot close it +// twice. A hijacked connection's descriptor is closed through the *os.File +// that net.FileConn was given (hijackConn): routing that close through +// closeFD would leave the file's finalizer to close the number again at a +// later GC, after it has been reused, which is the bug this seam hunts. func closeFDErr(fd int) error { if h := fdCloseSeam.Load(); h != nil { return (*h)(fd) diff --git a/internal/engine/iouring/fd_ownership_1242_linux_test.go b/internal/engine/iouring/fd_ownership_1242_linux_test.go index dcbacd6ca..6f03d1719 100644 --- a/internal/engine/iouring/fd_ownership_1242_linux_test.go +++ b/internal/engine/iouring/fd_ownership_1242_linux_test.go @@ -45,6 +45,7 @@ type fdTrap1242 struct { n atomic.Int64 keep int // when > 0, the placeholders of the oldest closes are released past this many (a whole-package run would run out of descriptor numbers) order []int + removed bool // release ran: a late close (a worker an earlier test leaked) is a plain close(2) echo bool // also write each close and each fault to stderr as it happens (a panicking worker ends the process before any verdict) } @@ -65,6 +66,7 @@ func newFDTrap1242() (*fdTrap1242, error) { func (tr *fdTrap1242) release() { tr.mu.Lock() defer tr.mu.Unlock() + tr.removed = true for fd := range tr.last { if tr.isPlaceholder(fd) { _ = unix.Close(fd) @@ -129,6 +131,11 @@ func (tr *fdTrap1242) closeFD(fd int) error { site := tr.site() tr.mu.Lock() defer tr.mu.Unlock() + if tr.removed { + // The placeholder is gone with the trap: a close that loaded it just + // before the seam was put back closes the number, as without a trap. + return unix.Close(fd) + } if tr.echo { fmt.Fprintf(os.Stderr, "celeris1242 CLOSE fd=%d at %s\n", fd, site) } @@ -182,8 +189,8 @@ func (tr *fdTrap1242) verdict(t *testing.T) { // question: a number the code under test freed is taken by the next open in // the process (a sibling test's engine that is still running, the Go // runtime's own sockets), and closing it then closes that one (celeris#1242). -// Sockets have an inode each; do not use it for io_uring rings on kernels -// that share one inode between them. +// Sockets have an inode each. io_uring rings do not on every kernel: use +// ringNamed1242 for one. type fdID1242 struct{ dev, ino uint64 } func fdIdentity1242(t testing.TB, fd int) fdID1242 { @@ -201,6 +208,18 @@ func (id fdID1242) names(fd int) bool { return unix.Fstat(fd, &st) == nil && st.Dev == id.dev && st.Ino == id.ino } +// ringNamed1242 reports whether fd still names the io_uring ring the identity +// was taken from: the same inode and still an io_uring file, so that on a +// kernel where rings share an inode a socket that took the number is not +// taken for the ring. (Another ring on the number would be, there.) +func ringNamed1242(id fdID1242, fd int) bool { + if !id.names(fd) { + return false + } + link, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%d", fd)) + return err == nil && link == "anon_inode:[io_uring]" +} + // stopAtCleanup1242 stops the engine when the test ends, whether or not the // test got as far as stop848, and waits for Listen to return. func stopAtCleanup1242(t *testing.T, cancel context.CancelFunc, done chan error) { @@ -581,3 +600,29 @@ func TestFDTrapFlagsOnlyAStaleClose1242(t *testing.T) { tr.bad = nil tr.mu.Unlock() } + +// A trap that has been removed is out of the way: a worker an earlier test +// leaked may still call the function it loaded before the seam was put back, +// and its close must be a plain close(2), not a dup3 of a placeholder that is +// gone onto a number that may be someone else's by now. +func TestARemovedFDTrapClosesPlainly1242(t *testing.T) { + tr, err := newFDTrap1242() + if err != nil { + t.Fatal(err) + } + fd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0) + if err != nil { + t.Fatal(err) + } + tr.release() + if err := tr.closeFD(fd); err != nil { + t.Fatalf("a close after the removal failed: %v", err) + } + if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err == nil { + _ = unix.Close(fd) + t.Fatal("a close after the removal left the descriptor open (a placeholder was put on it)") + } + if f := tr.faultsFor(fd); len(f) != 0 { + t.Fatalf("a removed trap reported: %v", f) + } +} diff --git a/internal/engine/iouring/worker_exit_order_884_linux_test.go b/internal/engine/iouring/worker_exit_order_884_linux_test.go index 35f7ea3d0..cb5c99524 100644 --- a/internal/engine/iouring/worker_exit_order_884_linux_test.go +++ b/internal/engine/iouring/worker_exit_order_884_linux_test.go @@ -59,11 +59,14 @@ func TestLeaveGroupAfterExitClosesTheRingAndTheListener884(t *testing.T) { // process gets one (celeris#1242). var w *Worker lfdID, ringID := fdIdentity1242(t, lfd), fdIdentity1242(t, ringFD) + ringOpen := func() bool { return ringNamed1242(ringID, ringFD) } t.Cleanup(func() { if lfdID.names(lfd) && (w == nil || w.listenFD >= 0) { closeFD(lfd) } - if ringID.names(ringFD) && (w == nil || w.ring != nil) { + // By the worker's own word, not by what the number names: rings may + // share one inode. + if w == nil || w.ring != nil { _ = ring.Close() } }) @@ -80,8 +83,8 @@ func TestLeaveGroupAfterExitClosesTheRingAndTheListener884(t *testing.T) { if _, err := net.DialTimeout("tcp", addr.String(), time.Second); !errors.Is(err, syscall.ECONNREFUSED) { t.Errorf("a connect after leaveGroupAfterExit gave %v, want connection refused: the listener is still in the group", err) } - if w.ring != nil || ringID.names(ringFD) { - t.Errorf("the ring is not closed after leaveGroupAfterExit: w.ring=%v fd open=%v", w.ring != nil, ringID.names(ringFD)) + if w.ring != nil || ringOpen() { + t.Errorf("the ring is not closed after leaveGroupAfterExit: w.ring=%v fd open=%v", w.ring != nil, ringOpen()) } if w.listenFD != -1 || lfdID.names(lfd) { t.Errorf("the listener is not closed after leaveGroupAfterExit: listenFD=%d fd open=%v", w.listenFD, lfdID.names(lfd)) From 285b320a5a08a904916105f4d5521cb2d3ffc920 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Tue, 3 Nov 2026 16:04:48 +0100 Subject: [PATCH 4/4] iouring tests: wait for each premise of the exit-path tests, bounded, instead of dialing once (Refs #1242) ResumeAccept returns before the workers have re-created their listeners (they do it on their next iteration), so the dial that followed the pause-resume sub-test's resume was refused whenever the worker was slow to run: always on one CPU shared with other work under -race -covermode=atomic (Coverage, run 41638098277). Every dial of these tests now retries to a 15 s bound, and the polls for an exit, a lost listener or a free port are bounded waits, not fixed windows; a premise that is still false then fails with what was seen. --- .../iouring/fd_ownership_1242_linux_test.go | 106 +++++++++++------- 1 file changed, 68 insertions(+), 38 deletions(-) diff --git a/internal/engine/iouring/fd_ownership_1242_linux_test.go b/internal/engine/iouring/fd_ownership_1242_linux_test.go index 6f03d1719..d99ad6857 100644 --- a/internal/engine/iouring/fd_ownership_1242_linux_test.go +++ b/internal/engine/iouring/fd_ownership_1242_linux_test.go @@ -220,6 +220,52 @@ func ringNamed1242(id fdID1242, fd int) bool { return err == nil && link == "anon_inode:[io_uring]" } +// premiseWithin1242 is how long a premise of these tests may take to come +// true: the engine's own steps (a worker re-creating its listener after a +// resume, a handler running after its request arrived) are asynchronous and +// slow down under -race -covermode=atomic on one CPU shared with other work. +// A premise is waited for to this bound, never slept for, and one that is not +// true by then fails with what was seen. +const premiseWithin1242 = 15 * time.Second + +// okWithin1242 dials until a /ok is answered, up to premiseWithin1242, and +// returns the keep-alive connection that was answered. A refused or reset +// dial is retried: a listener a worker has not re-created yet refuses. +func okWithin1242(t *testing.T, addr string) (net.Conn, time.Duration) { + t.Helper() + start := time.Now() + var last error + for time.Since(start) < premiseWithin1242 { + c, err := net.DialTimeout("tcp", addr, 2*time.Second) + if err == nil { + _ = c.SetDeadline(time.Now().Add(2 * time.Second)) + _, _ = c.Write([]byte("GET /ok HTTP/1.1\r\nHost: x\r\n\r\n")) + buf := make([]byte, 256) + var n int + if n, err = c.Read(buf); err == nil && strings.HasPrefix(string(buf[:n]), "HTTP/1.1 200") { + return c, time.Since(start) + } else if err == nil { + err = fmt.Errorf("not a 200: %q", buf[:n]) + } + _ = c.Close() + } + last = err + time.Sleep(2 * time.Millisecond) + } + t.Fatalf("PREMISE: no /ok answered within %v at %s, last error: %v", premiseWithin1242, addr, last) + return nil, 0 +} + +// waitFor1242 polls cond until it holds, up to premiseWithin1242. +func waitFor1242(cond func() bool) bool { + for dl := time.Now().Add(premiseWithin1242); time.Now().Before(dl); time.Sleep(time.Millisecond) { + if cond() { + return true + } + } + return cond() +} + // stopAtCleanup1242 stops the engine when the test ends, whether or not the // test got as far as stop848, and waits for Listen to return. func stopAtCleanup1242(t *testing.T, cancel context.CancelFunc, done chan error) { @@ -263,16 +309,8 @@ func TestEveryExitPathClosesEachDescriptorOnce1242(t *testing.T) { stopAtCleanup1242(t, cancel, done) // Keep-alive connections the stop has to close. for i := 0; i < 6; i++ { - c, err := net.DialTimeout("tcp", e.Addr().String(), 2*time.Second) - if err != nil { - t.Fatal(err) - } + c, _ := okWithin1242(t, e.Addr().String()) defer func() { _ = c.Close() }() - _ = c.SetDeadline(time.Now().Add(2 * time.Second)) - _, _ = c.Write([]byte("GET /ok HTTP/1.1\r\nHost: x\r\n\r\n")) - if n, err := c.Read(make([]byte, 256)); err != nil || n == 0 { - t.Fatalf("PREMISE: /ok: %d bytes, %v", n, err) - } } stop848(t, cancel, done) tr.requireCloses(t) @@ -293,10 +331,12 @@ func TestEveryExitPathClosesEachDescriptorOnce1242(t *testing.T) { t.Fatalf("ResumeAccept: %v", err) } } - // The workers re-create their listeners on the next iteration. - if _, err := okOnce848(e.Addr().String()); err != nil { - t.Fatalf("PREMISE: no answer after the resume: %v", err) - } + // ResumeAccept returns before the workers have re-created their + // listeners (they do it on their next iteration), so a dial right + // after it can be refused: wait for the answer, bounded. + c, took := okWithin1242(t, e.Addr().String()) + _ = c.Close() + t.Logf("answered %v after the resume", took) stop848(t, cancel, done) tr.requireCloses(t) tr.verdict(t) @@ -310,18 +350,13 @@ func TestEveryExitPathClosesEachDescriptorOnce1242(t *testing.T) { stopAtCleanup1242(t, cancel, done) addr := e.Addr().String() for i := 0; i < 12; i++ { - if _, err := okOnce848(addr); err != nil { - t.Fatal(err) - } - } - boom, err := net.DialTimeout("tcp", addr, 2*time.Second) - if err != nil { - t.Fatal(err) + c, _ := okWithin1242(t, addr) + _ = c.Close() } + boom, _ := okWithin1242(t, addr) defer func() { _ = boom.Close() }() _, _ = boom.Write([]byte("GET /boom HTTP/1.1\r\nHost: x\r\n\r\n")) - for dl := time.Now().Add(5 * time.Second); time.Now().Before(dl) && e.abnormalWorkerExits.Load() == 0; time.Sleep(time.Millisecond) { - } + waitFor1242(func() bool { return e.abnormalWorkerExits.Load() != 0 }) if got := e.abnormalWorkerExits.Load(); got != 1 { t.Fatalf("PREMISE: abnormalWorkerExits = %d, want 1", got) } @@ -355,6 +390,7 @@ func TestEveryExitPathClosesEachDescriptorOnce1242(t *testing.T) { if err := e.PauseAccept(); err != nil { t.Fatalf("PauseAccept: %v", err) } + waitFor1242(func() bool { return e.abnormalWorkerExits.Load() != 0 }) if e.abnormalWorkerExits.Load() != 1 { t.Fatalf("PREMISE: abnormalWorkerExits = %d, want 1", e.abnormalWorkerExits.Load()) } @@ -421,11 +457,7 @@ func TestEveryExitPathClosesEachDescriptorOnce1242(t *testing.T) { }) e, logs, cancel, done := start848(t, "127.0.0.1:0", false) stopAtCleanup1242(t, cancel, done) - for dl := time.Now().Add(5 * time.Second); time.Now().Before(dl); time.Sleep(time.Millisecond) { - if w := failed.Load(); w != nil && w.exited.Load() { - break - } - } + waitFor1242(func() bool { w := failed.Load(); return w != nil && w.exited.Load() }) if w := failed.Load(); w == nil || !w.exited.Load() { t.Fatal("PREMISE: no worker left its loop after a failed ring wait") } @@ -447,9 +479,12 @@ func TestEveryExitPathClosesEachDescriptorOnce1242(t *testing.T) { if err := e.PauseAccept(); err != nil { t.Fatal(err) } - ln, err := net.Listen("tcp", addr) - if err != nil { - t.Fatalf("a fixed port has no hold: %v", err) + // The pause's close takes the workers' listeners out of the group + // asynchronously: bind the port once they are gone. + var ln net.Listener + var lerr error + if !waitFor1242(func() bool { ln, lerr = net.Listen("tcp", addr); return lerr == nil }) { + t.Fatalf("PREMISE: the port was never free to hold after the pause: %v", lerr) } defer func() { _ = ln.Close() }() if err := e.ResumeAccept(); err != nil { @@ -457,9 +492,7 @@ func TestEveryExitPathClosesEachDescriptorOnce1242(t *testing.T) { } want := uint64(e.Metrics().Workers) var lost uint64 - for dl := time.Now().Add(5 * time.Second); time.Now().Before(dl) && lost < want; time.Sleep(5 * time.Millisecond) { - lost = e.Metrics().ErrorListenerRecreate - } + waitFor1242(func() bool { lost = e.Metrics().ErrorListenerRecreate; return lost >= want }) if want == 0 || lost != want { t.Fatalf("PREMISE: ErrorListenerRecreate = %d, want %d (one per worker)", lost, want) } @@ -501,12 +534,9 @@ func TestPanickedWorkerClosesEachDescriptorOnce1242(t *testing.T) { tr := installFDTrap1242(t) tr.echo = true e, _, _, _ := start848(t, "127.0.0.1:0", false) - c, err := net.DialTimeout("tcp", e.Addr().String(), 2*time.Second) - if err != nil { - t.Fatal(err) - } + c, _ := okWithin1242(t, e.Addr().String()) _, _ = c.Write([]byte("GET /panic HTTP/1.1\r\nHost: x\r\n\r\n")) - time.Sleep(5 * time.Second) + time.Sleep(30 * time.Second) // the panic ends the process long before t.Log("celeris1242 child survived the panic") return }