carlos / platform Public

console: a failed page write is not a served page

writeTemplate dropped w.Write's error and returned nil, so render recorded
the plan banner's notice for a page that never reached the browser. Before
the buffering, executing straight into the writer surfaced that error. Now
the write error is logged and returned (no second response: the headers
have gone), and a failing-writer regression pins that the notice is not
recorded. perfGet fails on a body read error instead of dropping it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 9ec1ea8e8e0cc59fa97d91f0a5a5166815e1ad4e parent ed6599a
2 files changed, +45 −3
  • internal/console/perfwiring_test.go +36 −1
  • internal/console/server.go +9 −2

CI failed — run details

diff --git a/internal/console/perfwiring_test.go b/internal/console/perfwiring_test.go
index 2d7bc5be7..7492fa0d9 100644
--- a/internal/console/perfwiring_test.go
+++ b/internal/console/perfwiring_test.go
@@ -1,6 +1,7 @@
package console
import (
+ "errors"
"io"
"net/http"
"net/http/httptest"
@@ -22,8 +23,11 @@ func perfGet(t *testing.T, c *http.Client, u string) (*http.Response, string) {
if err != nil {
t.Fatal(err)
}
- body, _ := io.ReadAll(resp.Body)
+ body, err := io.ReadAll(resp.Body)
resp.Body.Close()
+ if err != nil {
+ t.Fatalf("GET %s: reading the body: %v", u, err)
+ }
return resp, string(body)
}
@@ -147,3 +151,34 @@ func TestCLIPagesAreTimed(t *testing.T) {
t.Errorf("Server-Timing = %q, want a render span", st)
}
}
+
+// failingWriter accepts headers but refuses every body write, as a
+// connection the client has dropped does.
+type failingWriter struct{ *httptest.ResponseRecorder }
+
+var errClientGone = errors.New("client gone")
+
+func (failingWriter) Write([]byte) (int, error) { return 0, errClientGone }
+
+// A page that was never delivered must not count as served: render records
+// the plan banner's notice only after a write that succeeded.
+func TestRenderWriteErrorIsNotSuccess(t *testing.T) {
+ fx := newConsoleFixture(t)
+ acct := overRow(t, fx)
+ req := httptest.NewRequest(http.MethodGet, fx.u("/"), nil)
+ p := fx.srv.accountPage(httptest.NewRecorder(), req, "paul@keymail.dev", "csrf", acct, RoleOwner)
+ p.Title, p.Msg = "Apps", "ok"
+
+ if err := fx.srv.writeTemplate(failingWriter{httptest.NewRecorder()}, http.StatusOK, "message", p); !errors.Is(err, errClientGone) {
+ t.Fatalf("writeTemplate on a failing writer = %v, want %v", err, errClientGone)
+ }
+ fx.srv.render(failingWriter{httptest.NewRecorder()}, http.StatusOK, "message", p)
+ if st, _ := capRow(t, fx, acct, capStorage); !st.NoticeAt.IsZero() {
+ t.Fatalf("an undelivered page recorded notice: %v", st.NoticeAt)
+ }
+ // The control: the same page delivered does record it.
+ fx.srv.render(httptest.NewRecorder(), http.StatusOK, "message", p)
+ if st, _ := capRow(t, fx, acct, capStorage); st.NoticeAt.IsZero() {
+ t.Fatal("a delivered page recorded no notice")
+ }
+}
diff --git a/internal/console/server.go b/internal/console/server.go
index ec51e3a64..904814bf8 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -2254,7 +2254,8 @@ const maxPooledTemplateBuf = 1 << 20
// any header goes, so a template that fails part way answers a plain 500
// rather than half a page under a 200, and the first byte the perf
// middleware stamps comes after the template's work, which is timed as the
-// "render" span. The error is returned, already logged and answered.
+// "render" span. The error is returned already logged: a template error is
+// also answered 500, a write error (the headers already sent) is not.
func (s *Server) writeTemplate(w http.ResponseWriter, status int, name string, data any) error {
buf := templateBufs.Get().(*bytes.Buffer)
buf.Reset()
@@ -2273,7 +2274,13 @@ func (s *Server) writeTemplate(w http.ResponseWriter, status int, name string, d
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(status)
- _, _ = w.Write(buf.Bytes())
+ // The headers have gone, so a failed write can only be logged and
+ // returned: the page was not delivered, and render must not count it
+ // as served.
+ if _, err := w.Write(buf.Bytes()); err != nil {
+ log.Printf("console: render %s: %v", name, err)
+ return err
+ }
return nil
}