carlos / platform Public

edge: close idle connections on the public servers

Every server the edge exposes (:80, :443, the dev listener, the devbox
listener and the edge-local door) set only ReadHeaderTimeout. With
IdleTimeout and ReadTimeout both zero, net/http keeps a keep-alive or
idle HTTP/2 connection, its goroutine and its TLS state for as long as
the client likes. They now set IdleTimeout to one constant,
publicIdleTimeout = 120 s. ReadTimeout and WriteTimeout stay off: they
would cut long uploads, edge-held streams (#432) and WebSockets (#471).
IdleTimeout touches none of those (open requests and hijacked
connections are never idle).

Why 120 s: the region NLB in front of a fixed edge
(infra/modules/region-host/ingress/main.tf) has TCP listeners with no
tcp_idle_timeout_seconds, so AWS's default of 350 s applies. The edge
has to close first, with a FIN the client sees, rather than the NLB
silently dropping the flow and the client's next request on it getting
a reset. 120 s is well under that, and above the 90 s IdleConnTimeout
of the edge's own backhaul client, so a box-to-box hop's pool drops a
connection before the far edge does.

purge.go's control listener (a per-instance unix socket only the box's
own processes dial) is not public and is left as it was.

Tests: TestPublicServersCloseIdleConnections checks :80 and :443;
TestEveryEdgeServerSetsIdleTimeout reads the package source and fails
on any http.Server literal (outside purge.go) without IdleTimeout and
ReadHeaderTimeout, or with ReadTimeout/WriteTimeout. Watched it fail
with the door's IdleTimeout removed.

Audit 2026-11-16, activator-edge finding 10a.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 275369d49db725a1b20ae5ae6a183f53117138de parent 15b1ee6
5 files changed, +135 −12
  • internal/edge/devbox.go +3 −2
  • internal/edge/door.go +3 −3
  • internal/edge/edge.go +4 −3
  • internal/edge/idletimeout_test.go +98 −0
  • internal/edge/ingressdrain.go +27 −4

CI cancelled — superseded by a newer push — run details

diff --git a/internal/edge/devbox.go b/internal/edge/devbox.go
index 0caff03a5..93e6dab1f 100644
--- a/internal/edge/devbox.go
+++ b/internal/edge/devbox.go
@@ -218,8 +218,8 @@ func serveDevbox(ctx context.Context, cfg DevboxConfig, seams devboxSeams) error
go w.run()
go e.Watch(stop)
- // ReadHeaderTimeout for the same slow-header reason as Run's servers,
- // and no Read/WriteTimeout, which would cut proxied WebSockets.
+ // ReadHeaderTimeout and IdleTimeout for the same reasons as Run's
+ // servers, and no Read/WriteTimeout, which would cut proxied WebSockets.
selfHost, _, _ := net.SplitHostPort(cfg.ListenAddr)
selfIP, _ := netip.ParseAddr(selfHost) // checked before building the edge
srv := &http.Server{
@@ -231,6 +231,7 @@ func serveDevbox(ctx context.Context, cfg DevboxConfig, seams devboxSeams) error
MinVersion: tls.VersionTLS12,
},
ReadHeaderTimeout: 10 * time.Second,
+ IdleTimeout: publicIdleTimeout,
ErrorLog: seams.errorLog,
}
served := make(chan error, 1)
diff --git a/internal/edge/door.go b/internal/edge/door.go
index 12c9b0206..67533f788 100644
--- a/internal/edge/door.go
+++ b/internal/edge/door.go
@@ -196,9 +196,9 @@ func (e *Edge) openDoor(path string) (closeDoor func(), err error) {
ln.Close()
return nil, err
}
- // ReadHeaderTimeout for the public listeners' Slowloris reason; no
- // Read/WriteTimeout, which would cut long-lived streams.
- srv := &http.Server{Handler: http.HandlerFunc(e.serveDoor), ReadHeaderTimeout: 10 * time.Second}
+ // ReadHeaderTimeout and IdleTimeout for the public listeners' reasons;
+ // no Read/WriteTimeout, which would cut long-lived streams.
+ srv := &http.Server{Handler: http.HandlerFunc(e.serveDoor), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: publicIdleTimeout}
go func() {
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("edge: edge-local door on %s stopped: %v", path, err)
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 6eeba4de1..355314446 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -5164,9 +5164,10 @@ func RunWithActivatorReady(args []string, act Activator, cache *Cache, ready fun
log.Printf("edge: edge-local door off in dev mode")
}
// ReadHeaderTimeout guards against Slowloris-style slow-header
- // connection exhaustion. No ReadTimeout/WriteTimeout — those would
- // break long-lived proxied WebSockets.
- srv := &http.Server{Addr: *httpAddr, Handler: e, ReadHeaderTimeout: 10 * time.Second}
+ // connection exhaustion; publicIdleTimeout closes idle keep-alives.
+ // No ReadTimeout/WriteTimeout — those would break long-lived
+ // proxied WebSockets.
+ srv := &http.Server{Addr: *httpAddr, Handler: e, ReadHeaderTimeout: 10 * time.Second, IdleTimeout: publicIdleTimeout}
// Bound here, on this goroutine, as ListenAndServe would; the line
// below means the bind succeeded.
ln, err := listen("http", *httpAddr, ":http")
diff --git a/internal/edge/idletimeout_test.go b/internal/edge/idletimeout_test.go
new file mode 100644
index 000000000..39c624372
--- /dev/null
+++ b/internal/edge/idletimeout_test.go
@@ -0,0 +1,98 @@
+package edge
+
+import (
+ "crypto/tls"
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+)
+
+// TestPublicServersCloseIdleConnections: :80 and :443 close a connection
+// that sits idle, keep the slow-header bound, and set no Read/WriteTimeout
+// (they would cut uploads, held streams and WebSockets). The idle bound
+// stays under the region NLB's 350 s TCP idle timeout.
+func TestPublicServersCloseIdleConnections(t *testing.T) {
+ e := &Edge{}
+ h := http.NotFoundHandler()
+ httpsSrv, httpSrv := e.publicServers(h, h, &tls.Config{}, "", "")
+ for name, srv := range map[string]*http.Server{"https": httpsSrv, "http": httpSrv} {
+ if srv.IdleTimeout != publicIdleTimeout || srv.ReadHeaderTimeout != 10*time.Second {
+ t.Errorf("%s: IdleTimeout %v ReadHeaderTimeout %v, want %v and 10s", name, srv.IdleTimeout, srv.ReadHeaderTimeout, publicIdleTimeout)
+ }
+ if srv.ReadTimeout != 0 || srv.WriteTimeout != 0 {
+ t.Errorf("%s: ReadTimeout %v WriteTimeout %v, want neither", name, srv.ReadTimeout, srv.WriteTimeout)
+ }
+ }
+ if publicIdleTimeout <= 0 || publicIdleTimeout >= 350*time.Second {
+ t.Fatalf("publicIdleTimeout %v must be positive and under the NLB's 350 s", publicIdleTimeout)
+ }
+}
+
+// TestEveryEdgeServerSetsIdleTimeout reads the package's source: every
+// http.Server literal sets IdleTimeout and ReadHeaderTimeout and neither
+// ReadTimeout nor WriteTimeout, so a listener added later (or the dev,
+// devbox and door servers, built inline) cannot quietly hold idle
+// connections forever. The one exception is purge.go's control listener,
+// a per-instance unix socket that only the box's own processes can dial,
+// which is not public and is left as it was.
+func TestEveryEdgeServerSetsIdleTimeout(t *testing.T) {
+ exempt := map[string]bool{"purge.go": true}
+ files, err := filepath.Glob("*.go")
+ if err != nil {
+ t.Fatal(err)
+ }
+ fset := token.NewFileSet()
+ found := 0
+ for _, name := range files {
+ if strings.HasSuffix(name, "_test.go") || exempt[name] {
+ continue
+ }
+ src, err := os.ReadFile(name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ f, err := parser.ParseFile(fset, name, src, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ ast.Inspect(f, func(n ast.Node) bool {
+ lit, ok := n.(*ast.CompositeLit)
+ if !ok {
+ return true
+ }
+ sel, ok := lit.Type.(*ast.SelectorExpr)
+ if !ok || sel.Sel.Name != "Server" {
+ return true
+ }
+ if pkg, ok := sel.X.(*ast.Ident); !ok || pkg.Name != "http" {
+ return true
+ }
+ found++
+ keys := map[string]bool{}
+ for _, el := range lit.Elts {
+ if kv, ok := el.(*ast.KeyValueExpr); ok {
+ if id, ok := kv.Key.(*ast.Ident); ok {
+ keys[id.Name] = true
+ }
+ }
+ }
+ at := fset.Position(lit.Pos())
+ if !keys["IdleTimeout"] || !keys["ReadHeaderTimeout"] {
+ t.Errorf("%s: http.Server without IdleTimeout and ReadHeaderTimeout", at)
+ }
+ if keys["ReadTimeout"] || keys["WriteTimeout"] {
+ t.Errorf("%s: http.Server sets ReadTimeout or WriteTimeout, which cut long streams", at)
+ }
+ return true
+ })
+ }
+ if found < 5 {
+ t.Fatalf("found %d http.Server literals, want at least 5 (:80, :443, dev, devbox, door); has the scan stopped matching?", found)
+ }
+}
diff --git a/internal/edge/ingressdrain.go b/internal/edge/ingressdrain.go
index 54370928f..01710b7ea 100644
--- a/internal/edge/ingressdrain.go
+++ b/internal/edge/ingressdrain.go
@@ -89,14 +89,37 @@ func (e *Edge) plainTracked(h http.Handler) http.Handler {
// address :80's health check never reaches plainTracked or the challenge.
//
// ReadHeaderTimeout guards the public front door against Slowloris-style
-// slow-header connection exhaustion. No ReadTimeout/WriteTimeout: those
-// would break long-lived proxied WebSockets.
+// slow-header connection exhaustion, and publicIdleTimeout closes the
+// connections that sit idle between requests. No ReadTimeout/WriteTimeout:
+// those would break long uploads, edge-held streams (#432) and long-lived
+// proxied WebSockets (#471).
func (e *Edge) publicServers(https, challenge http.Handler, tlsCfg *tls.Config, httpAddr, httpsAddr string) (httpsSrv, httpSrv *http.Server) {
- httpSrv = &http.Server{Addr: httpAddr, Handler: e.plainTracked(challenge), ReadHeaderTimeout: 10 * time.Second}
- httpsSrv = &http.Server{Addr: httpsAddr, Handler: https, TLSConfig: tlsCfg, ReadHeaderTimeout: 10 * time.Second}
+ httpSrv = &http.Server{Addr: httpAddr, Handler: e.plainTracked(challenge), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: publicIdleTimeout}
+ httpsSrv = &http.Server{Addr: httpsAddr, Handler: https, TLSConfig: tlsCfg, ReadHeaderTimeout: 10 * time.Second, IdleTimeout: publicIdleTimeout}
return httpsSrv, httpSrv
}
+// publicIdleTimeout is how long a server the edge exposes (:80, :443, the
+// dev listener, the devbox listener and the edge-local door) keeps a
+// connection with no request on it: an HTTP/1.1 keep-alive connection
+// between requests, or an HTTP/2 connection with no open stream. With
+// IdleTimeout and ReadTimeout both zero, net/http keeps such a connection,
+// its goroutine and its TLS state for as long as the client likes;
+// ReadHeaderTimeout starts only at the next request's first byte. It never
+// touches a request in flight, an edge-held stream (an open request) or a
+// hijacked connection (WebSockets, tunnels), so it is safe where
+// ReadTimeout and WriteTimeout are not.
+//
+// It must stay under the idle timeout of the region NLB in front of a
+// fixed edge: AWS's TCP default of 350 s, since the listeners in
+// infra/modules/region-host/ingress set no tcp_idle_timeout_seconds. The
+// edge then closes first, with a FIN the client sees, instead of the NLB
+// forgetting the flow and the client's next request on it getting a reset.
+// It is also above the 90 s IdleConnTimeout of the edge's own backhaul
+// client, so a box-to-box hop's pool drops a connection before the far
+// edge does.
+const publicIdleTimeout = 120 * time.Second
+
// CloseIngress is step 3's edge half: close the public listeners
// gracefully, waiting up to wait (the connection wait).
//