Skip to content
23 changes: 23 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ type Config struct {
// IdleTimeout (alone, as net/http's idle timer) from the last stream
// activity, which those frames do not move. A response the client does not
// take, from a client that keeps sending frames, is not bounded by ReadTimeout.
//
// The native engines look at ReadTimeout, IdleTimeout and WriteTimeout in a
// sweep of their connections, so one is enforced up to a sweep period after
// its time: 25 ms on epoll when ReadHeaderTimeout is set (the default), and on
// io_uring about 0.8 s (0.83 s measured on an idle worker; about 1 s with
// ReadHeaderTimeout off). With ReadHeaderTimeout off epoll has no such timer and
// looks much more rarely (a 1 s IdleTimeout closed an idle connection at 3 to 6 s
// in measurement). A timeout of a few seconds or less is approximate by that
// much on io_uring. The HTTP/1 header deadline, and the budget of a connection
// whose protocol is not decided yet, have a timer of their own on io_uring and
// are not; the HTTP/2 header-block bound of ReadHeaderTimeout is checked by the
// sweep and is late by up to a sweep period.
ReadTimeout time.Duration
// ReadHeaderTimeout caps the read of just the request line + headers
// (separately from the request body, which ReadTimeout covers). This
Expand All @@ -292,6 +304,17 @@ type Config struct {
// connection's first byte or about a second after its SYN, whichever
// comes first; std counts from the handshake.
//
// So the bound on a connection that sends nothing is its connect, plus
// about one second of deferral on epoll, io_uring and adaptive (measured
// 1.00 to 1.07 s), plus ReadHeaderTimeout, plus
// the lateness of the engine's check: about 30 ms on epoll (a 25 ms timer)
// and on io_uring (a timer of the connection's own), measured on docker
// arm64 with 2 CPUs. With DisableDeferAccept the deferral is gone: a silent
// connection is closed at ReadHeaderTimeout from its connect, and a
// ReadHeaderTimeout of one second closed it at 1.00 to 1.03 s. The HTTP/2
// header-block bound below is checked by the engine's sweep, not by a timer,
// and is late by up to a sweep period (see ReadTimeout).
//
// On HTTP/2 (epoll, io_uring, adaptive) it also bounds a header block: one
// that begins with a HEADERS frame without END_HEADERS and is not complete this
// long after that frame ends the connection with GOAWAY(ENHANCE_YOUR_CALM).
Expand Down
58 changes: 58 additions & 0 deletions internal/engine/epoll/silent_conn_bound_1189_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//go:build linux

package epoll

import (
"context"
"net"
"testing"
"time"

"github.com/goceleris/celeris/internal/mono/monotest"
"github.com/goceleris/celeris/internal/protocol/h2/stream"
"github.com/goceleris/celeris/internal/resource"
)

// celeris#1189. A connection that sends nothing is closed at ReadHeaderTimeout from
// the engine's accept: no later than the 25 ms sweep of the timerfd after it, and,
// with TCP_DEFER_ACCEPT on (the default), the kernel's deferral of a silent connection
// (about 1 s) is added before the accept. See monotest.SilentConnIsCutAtReadHeaderTimeout.
func TestSilentConnIsCutAtReadHeaderTimeout1189(t *testing.T) {
monotest.SilentConnIsCutAtReadHeaderTimeout(t, startSilent1189)
}

func TestSilentConnIsCutAfterDeferAccept1189(t *testing.T) {
monotest.SilentConnIsCutAfterDeferAccept(t, startSilent1189)
}

func startSilent1189(t *testing.T, cfg resource.Config, h stream.Handler) string {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("pick port: %v", err)
}
cfg.Addr = ln.Addr().String()
_ = ln.Close()
e, err := New(cfg, h)
if err != nil {
t.Fatalf("epoll engine: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
errCh := make(chan error, 1)
go func() { errCh <- e.Listen(ctx) }()
t.Cleanup(func() {
cancel()
select {
case <-errCh:
case <-time.After(10 * time.Second):
t.Error("epoll engine did not stop within 10 s")
}
})
for dl := time.Now().Add(10 * time.Second); e.Addr() == nil && time.Now().Before(dl); {
time.Sleep(10 * time.Millisecond)
}
if e.Addr() == nil {
t.Fatal("engine did not bind")
}
return e.Addr().String()
}
116 changes: 116 additions & 0 deletions internal/engine/iouring/accept_churn_1189_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//go:build linux

package iouring

import (
"context"
"fmt"
"io"
"log/slog"
"net"
"sync"
"testing"
"time"

"github.com/goceleris/celeris/internal/engine"
"github.com/goceleris/celeris/internal/resource"
)

// BenchmarkAcceptChurnAuto1189 is the cost of arming the header timer of a
// connection at its accept, which is a connection whose protocol is not decided
// yet (celeris#1189): Protocol Auto, as the default is, with ReadHeaderTimeout
// set (the default is 10 s) or off (the control: the change is not reached). One
// op is one connection, on 8 client goroutines and one worker. "request" sends one
// request with Connection: close and reads the response to the end: the timer
// armed at the accept is the one the request would have armed at detection, so
// this arm must not move. "bare" connects and closes without a byte: it pays the
// arm and the cancel the request arm did not, so it is the worst case for the
// change. The kernel's connect and close dominate each op, so read the figures
// against an A/A twin of the same binary (RULE 74).
func BenchmarkAcceptChurnAuto1189(b *testing.B) {
for _, rht := range []time.Duration{10 * time.Second, -1} {
for _, arm := range []string{"request", "bare"} {
b.Run(fmt.Sprintf("%s/rht=%v", arm, rht), func(b *testing.B) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
b.Fatalf("pick port: %v", err)
}
addr := ln.Addr().String()
_ = ln.Close()
e, err := New(resource.Config{
Addr: addr,
Protocol: engine.Auto,
Resources: resource.Resources{Workers: 2},
ReadHeaderTimeout: rht,
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
}, newCarry543Handler())
if err != nil {
b.Skipf("iouring engine unavailable: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { _ = e.Listen(ctx); close(done) }()
defer func() { cancel(); <-done }()
for dl := time.Now().Add(15 * time.Second); (e.Addr() == nil || e.NumWorkers() == 0) && time.Now().Before(dl); {
time.Sleep(2 * time.Millisecond)
}
if e.Addr() == nil {
b.Fatal("engine did not bind")
}
req := []byte("GET /bench HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n")
one := func() error {
c, err := net.Dial("tcp", addr)
if err != nil {
return err
}
defer func() { _ = c.Close() }()
if arm == "bare" {
return nil
}
if _, err := c.Write(req); err != nil {
return err
}
_, err = io.Copy(io.Discard, c)
return err
}
// Warm: the first connections after an idle ring wait for the worker's
// idle wake.
for range 2000 {
if err := one(); err != nil {
b.Fatalf("warm: %v", err)
}
}
base := e.Metrics().CloseCount
b.ReportAllocs()
b.ResetTimer()
var wg sync.WaitGroup
next := make(chan struct{}, 256)
for range 8 {
wg.Add(1)
go func() {
defer wg.Done()
for range next {
if err := one(); err != nil {
b.Errorf("conn: %v", err)
return
}
}
}()
}
for range b.N {
next <- struct{}{}
}
close(next)
wg.Wait()
dl := time.Now().Add(60 * time.Second)
for e.Metrics().CloseCount-base < uint64(b.N) {
if time.Now().After(dl) {
b.Fatalf("the engine closed %d of %d connections", e.Metrics().CloseCount-base, b.N)
}
time.Sleep(50 * time.Microsecond)
}
b.StopTimer()
})
}
}
}
138 changes: 138 additions & 0 deletions internal/engine/iouring/close_at_accept_1189_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//go:build linux

package iouring

import (
"io"
"log/slog"
"net"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/goceleris/celeris/internal/engine"
"github.com/goceleris/celeris/internal/resource"
)

// A connection whose header timer finds its budget already spent when it is armed
// at the accept is closed there, and nothing may then be armed on it
// (celeris#1189, review of #1193). The budget is taken from the CQE batch's clock,
// so it is spent at the accept when the batch ran for longer than ReadHeaderTimeout
// before it reached the accept: an OnConnect hook, or a slow inline handler of an
// earlier CQE of the batch. Here OnConnect takes 300 ms against a ReadHeaderTimeout
// of 200 ms. A recv armed on the closed connection is a kernel op on a released
// descriptor number and on a connState that is queued for release (celeris#685,
// celeris#256), and nothing accounts for it: the connState is held by the 5 s
// pendingRelease backstop, which then releases it with a "kernel ops unaccounted"
// warning. The check is the sweep's own, so it does not wait for the backstop: no
// connState of pendingRelease may have an op in flight and no closedOps entry that
// will account for its completion.
func TestConnClosedByTheArmAtAcceptGetsNoRecv1189(t *testing.T) {
var (
mu sync.Mutex
addr string
looked int
orphans int
)
hook := func(w *Worker) {
mu.Lock()
defer mu.Unlock()
// The hook is the package's, not the engine's: workers of an earlier test may
// still be winding down.
if addr == "" || w.cfg.Addr != addr {
return
}
for i := range w.pendingRelease {
cs := w.pendingRelease[i].cs
looked++
if cs.kernelInflight > 0 && w.closedOps[encodeConnOpKey(cs.fd, cs.generation)] == nil {
orphans++
}
}
}
testHookCheckTimeouts.Store(&hook)
t.Cleanup(func() { testHookCheckTimeouts.Store(nil) })
logs := &syncBuf806{}
var (
first atomic.Bool
connReturned atomic.Pointer[time.Time] // when the slow OnConnect returned
closedAt atomic.Pointer[time.Time] // when the first connection's OnDisconnect ran
)
r := startEngine806(t, loadHandler806{}, func(c *resource.Config) {
c.Protocol = engine.Auto
c.ReadHeaderTimeout = 200 * time.Millisecond
c.DisableDeferAccept = true
c.Logger = slog.New(slog.NewTextHandler(io.Writer(logs), &slog.HandlerOptions{Level: slog.LevelWarn}))
c.OnConnect = func(string) {
if first.CompareAndSwap(false, true) {
time.Sleep(300 * time.Millisecond)
now := time.Now()
connReturned.Store(&now)
}
}
c.OnDisconnect = func(string) {
now := time.Now()
closedAt.CompareAndSwap(nil, &now)
}
})
mu.Lock()
addr = r.addr
mu.Unlock()

c, err := net.DialTimeout("tcp", r.addr, 3*time.Second)
if err != nil {
t.Fatal(err)
}
defer func() { _ = c.Close() }()
_ = c.SetReadDeadline(time.Now().Add(3 * time.Second))
if _, err := c.Read(make([]byte, 1)); err != io.EOF {
t.Fatalf("the connection whose budget was spent at the accept: read = %v, want it closed (EOF)", err)
}

// The scenario happened: the arm closed the connection at the accept, as soon as
// OnConnect returned, not at a timer or a sweep after it. Without this the test
// passes when the OnConnect did not outlast the budget (nothing is closed at the
// accept), or when a timer or the sweep closed the connection, which have nothing
// to leave behind. The close is within microseconds of the return when it is the
// arm's (measured 6 to 75 microseconds, under -race too); a timer
// would be ReadHeaderTimeout (200 ms) after the accept, which is 200 ms after the return, and the sweep's 100 ms or more.
ret, closed := connReturned.Load(), closedAt.Load()
if ret == nil || closed == nil {
t.Fatalf("OnConnect returned: %v, OnDisconnect ran: %v: the scenario did not occur", ret != nil, closed != nil)
}
if gap := closed.Sub(*ret); gap < 0 || gap > 50*time.Millisecond {
t.Fatalf("the connection was closed %v after its slow OnConnect returned, want within 50ms: it was not the arm at the accept that closed it", gap)
}
t.Logf("closed %v after OnConnect returned", closed.Sub(*ret))

// A healthy connection is served.
c2, err := net.DialTimeout("tcp", r.addr, 3*time.Second)
if err != nil {
t.Fatal(err)
}
defer func() { _ = c2.Close() }()
_ = c2.SetDeadline(time.Now().Add(3 * time.Second))
if _, err := io.WriteString(c2, "GET / HTTP/1.1\r\nHost: x\r\n\r\n"); err != nil {
t.Fatal(err)
}
buf := make([]byte, 12)
if _, err := io.ReadFull(c2, buf); err != nil || !strings.HasPrefix(string(buf), "HTTP/1.1 200") {
t.Fatalf("the healthy connection was not served: %q, %v", buf, err)
}

// The sweeps come within about 70, 250 and 430 ms of the activity of an idle
// worker (its idle backoff starts again), so a second is several looks; the 5 s
// backstop is not waited for.
time.Sleep(time.Second)
mu.Lock()
defer mu.Unlock()
t.Logf("sweeps looked at a connState of pendingRelease %d times", looked)
if orphans > 0 {
t.Errorf("a connState queued for release had a kernel op in flight with no closedOps entry in %d of %d looks (a recv armed on a closed connection)", orphans, looked)
}
if l := logs.String(); strings.Contains(l, "unaccounted") {
t.Errorf("warning logged: %s", l)
}
}
Loading
Loading