carlos / platform Public

edge: a drain's Connection: close survives the gzip header sync

conns.track sets Connection: close on the real writer, but gzipWriter's
syncHeader replaced that writer's headers wholesale at WriteHeader, so a
proxied tenant response to a client that accepts gzip (Go clients and
browsers) lost it during the ingress drain's step 1 and §25's drain. The
sync now keeps a Connection header already on the real writer unless the
private map sets its own; compression is otherwise unchanged. Found by
the root-runner run (W6 final review F9).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 47e12e8a9daa7ca1c8dd10912bb1a013e8f40142 parent e70dba3
2 files changed, +80 −0
  • internal/edge/compress.go +10 −0
  • internal/edge/compress_test.go +70 −0
diff --git a/internal/edge/compress.go b/internal/edge/compress.go
index e460c0401..cff893786 100644
--- a/internal/edge/compress.go
+++ b/internal/edge/compress.go
@@ -208,11 +208,21 @@ func (g *gzipWriter) Header() http.Header { return g.hdr }
// Wholesale, not a merge: a handler that DELETED a header between blocks (the
// shape the purge-on-103 test exercises) must see that deletion reach the
// client, which a merge would silently undo.
+//
+// The one exception is Connection, which the edge itself sets on the real
+// writer before the handler runs (conns.track: the ingress drain's and
+// §25's Connection: close) and which no handler above ever saw in hdr. It
+// survives unless the private map sets its own; wiping it would serve a
+// drain's responses on a kept-alive connection.
func (g *gzipWriter) syncHeader() {
down := g.ResponseWriter.Header()
+ conn, keep := down["Connection"]
for k := range down {
delete(down, k)
}
+ if _, own := g.hdr["Connection"]; keep && !own {
+ down["Connection"] = conn
+ }
for k, v := range g.hdr {
down[k] = v
}
diff --git a/internal/edge/compress_test.go b/internal/edge/compress_test.go
index 1213f231c..f2bd15d41 100644
--- a/internal/edge/compress_test.go
+++ b/internal/edge/compress_test.go
@@ -805,3 +805,73 @@ func TestHeadKeepsItsContentLength(t *testing.T) {
t.Errorf("HEAD Content-Length = %q, want 4096", got)
}
}
+
+// A drain's Connection: close, set on the real writer by conns.track,
+// survives the gzip writer's header sync on a proxied tenant response the
+// client accepts gzip for (W6 final review F9; Go clients and browsers ask
+// for gzip by default). Compression itself is unchanged: the body still
+// arrives gzipped. Both drains share the switch: the ingress drain's step 1
+// and §25's draining flag (DrainConnections sets it with its barrier).
+func TestDrainConnectionCloseSurvivesGzip(t *testing.T) {
+ for _, drain := range []string{"ingress", "connection-drain"} {
+ t.Run(drain, func(t *testing.T) {
+ dir := t.TempDir()
+ sock := filepath.Join(dir, "app.sock")
+ ln, err := net.Listen("unix", sock)
+ if err != nil {
+ t.Fatal(err)
+ }
+ body := strings.Repeat("lifecycle tenant ", 200)
+ tenant := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/plain")
+ io.WriteString(w, body)
+ })}
+ go tenant.Serve(ln)
+ t.Cleanup(func() { tenant.Close() })
+ reg, err := registry.Open(filepath.Join(dir, "registry.db"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { reg.Close() })
+ if err := reg.Upsert(registry.Route{Host: "app.example.com", Socket: sock}); err != nil {
+ t.Fatal(err)
+ }
+ e, err := New(reg, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ srv := httptest.NewServer(e)
+ defer srv.Close()
+ client := &http.Client{Transport: &http.Transport{}} // asks for gzip by itself
+ defer client.CloseIdleConnections()
+ get := func() *http.Response {
+ t.Helper()
+ req, _ := http.NewRequest("GET", srv.URL+"/", nil)
+ req.Host = "app.example.com"
+ resp, err := client.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ b, err := io.ReadAll(resp.Body)
+ resp.Body.Close()
+ if err != nil || string(b) != body || !resp.Uncompressed {
+ t.Fatalf("tenant: %d bytes, err %v, gzipped %v", len(b), err, resp.Uncompressed)
+ }
+ return resp
+ }
+ if get().Close {
+ t.Fatal("Connection: close before the drain")
+ }
+ if drain == "ingress" {
+ if err := e.BeginIngressDrain(); err != nil {
+ t.Fatal(err)
+ }
+ } else {
+ e.conns.draining.Store(true)
+ }
+ if !get().Close {
+ t.Fatal("a gzipped response during the drain lost Connection: close")
+ }
+ })
+ }
+}