diff --git a/config.go b/config.go index 2ff089bc7..e3959425e 100644 --- a/config.go +++ b/config.go @@ -832,7 +832,10 @@ type Config struct { // It runs on the event-loop goroutine on the native engines, whether the // connection's routes are [Route.Async] or not, so it must not call // runtime.Goexit: that ends the loop for good and the engine only logs an - // Error (see [Route.Sync]). + // Error (see [Route.Sync]). The [Std] engine runs it on a helper goroutine + // the accept path waits for, so a Goexit there ends only that helper and is + // logged as an Error: the connection proceeds and the engine keeps + // accepting. OnConnect func(addr string) // OnDisconnect is called when a connection is closed. The addr is the // remote peer address. Must be fast — blocks the event loop. diff --git a/internal/engine/std/engine.go b/internal/engine/std/engine.go index c901b12fb..4dbb8eed8 100644 --- a/internal/engine/std/engine.go +++ b/internal/engine/std/engine.go @@ -510,7 +510,7 @@ func (e *Engine) connStateHook(conn net.Conn, state http.ConnState) { case http.StateNew: e.metrics.activeConns.Add(1) if e.cfg.OnConnect != nil { - e.safeCallback(e.cfg.OnConnect, conn.RemoteAddr().String()) + e.connectCallback(e.cfg.OnConnect, conn.RemoteAddr().String()) } case http.StateClosed, http.StateHijacked: e.metrics.activeConns.Add(-1) @@ -521,6 +521,32 @@ func (e *Engine) connStateHook(conn net.Conn, state http.ConnState) { } } +// connectCallback runs OnConnect on a helper goroutine that the hook waits +// for (celeris#889). net/http runs the StateNew hook inline in Server.Serve, +// the goroutine that accepts: a callback that ends its goroutine with +// runtime.Goexit (t.FailNow in a test) would end Serve, whose deferred +// listener close then stops the engine accepting for good, silently. A +// Goexit cannot be recovered, only outlived: here it ends the helper, the +// join below still fires, and it is logged as the native engines log one +// (celeris#848). The connection proceeds as if the callback had returned. +func (e *Engine) connectCallback(fn func(string), arg string) { + done := make(chan struct{}) + go func() { + defer close(done) + returned := false + defer func() { + if !returned { + e.logger.Error("std engine: OnConnect ended its goroutine with runtime.Goexit; "+ + "callbacks must not call it (t.FailNow, t.Fatal and t.SkipNow do)", + "remote", arg) + } + }() + e.safeCallback(fn, arg) // recovers a panic, so returning means it returned or panicked + returned = true + }() + <-done +} + // safeCallback invokes a user-provided callback with panic recovery to prevent // a panicking callback from crashing the connection state handler. func (e *Engine) safeCallback(fn func(string), arg string) { diff --git a/internal/engine/std/onconnect_goexit_889_test.go b/internal/engine/std/onconnect_goexit_889_test.go new file mode 100644 index 000000000..ff2c2306b --- /dev/null +++ b/internal/engine/std/onconnect_goexit_889_test.go @@ -0,0 +1,104 @@ +package std + +import ( + "bytes" + "context" + "io" + "log/slog" + "net/http" + "runtime" + "sync" + "testing" + "time" + + "github.com/goceleris/celeris/internal/engine" + "github.com/goceleris/celeris/internal/resource" +) + +// celeris#889: net/http runs the StateNew ConnState hook inline in +// Server.Serve, before it starts the connection's goroutine. An OnConnect +// that calls runtime.Goexit (t.FailNow in a test's callback) used to end the +// Serve goroutine, whose deferred listener close then stopped the engine +// accepting for good, with nothing logged. safeCallback's recover cannot stop +// a Goexit, so OnConnect runs on a helper goroutine that the hook joins. + +type syncBuf889 struct { + mu sync.Mutex + b bytes.Buffer +} + +func (s *syncBuf889) Write(p []byte) (int, error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.b.Write(p) +} + +func (s *syncBuf889) String() string { + s.mu.Lock() + defer s.mu.Unlock() + return s.b.String() +} + +func TestOnConnectGoexitKeepsAccepting889(t *testing.T) { + var logs syncBuf889 + var mu sync.Mutex + calls := 0 + e, err := New(resource.Config{ + Addr: "127.0.0.1:0", + Engine: engine.Std, + Protocol: engine.HTTP1, + Logger: slog.New(slog.NewTextHandler(&logs, nil)), + OnConnect: func(string) { + mu.Lock() + calls++ + mu.Unlock() + runtime.Goexit() + }, + }, &echoHandler{}) + if err != nil { + t.Fatalf("New: %v", err) + } + ctx, cancel := context.WithCancel(t.Context()) + 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("Listen did not return") + } + }) + for deadline := time.Now().Add(5 * time.Second); e.Addr() == nil; { + if time.Now().After(deadline) { + t.Fatal("listener not ready") + } + time.Sleep(5 * time.Millisecond) + } + + // Each dial uses a fresh connection, so each one fires OnConnect (and its + // Goexit); every one must be answered, not only the first. + tr := &http.Transport{DisableKeepAlives: true} + t.Cleanup(tr.CloseIdleConnections) + cl := &http.Client{Transport: tr, Timeout: 5 * time.Second} + for i := range 3 { + resp, err := cl.Get("http://" + e.Addr().String() + "/x") + if err != nil { + t.Fatalf("request %d after an OnConnect Goexit: %v (the engine stopped accepting)", i+1, err) + } + body, _ := io.ReadAll(resp.Body) + _ = resp.Body.Close() + if resp.StatusCode != 200 || string(body) != "GET /x" { + t.Fatalf("request %d: %d %q", i+1, resp.StatusCode, body) + } + } + mu.Lock() + n := calls + mu.Unlock() + if n != 3 { + t.Errorf("OnConnect ran %d times, want 3", n) + } + if !bytes.Contains([]byte(logs.String()), []byte("Goexit")) { + t.Errorf("an OnConnect Goexit was not logged; log:\n%s", logs.String()) + } +}