carlos / platform Public

console: attribute PDF stalls to the side that stopped

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev cc5a9641b0c876a2c54216dad095d631fd487cf7 parent 56171c4
2 files changed, +138 −27
  • internal/console/apipdf.go +48 −27
  • internal/console/apipdf_test.go +90 −0

CI failed — run details

diff --git a/internal/console/apipdf.go b/internal/console/apipdf.go
index b6376defb..89b3755fa 100644
--- a/internal/console/apipdf.go
+++ b/internal/console/apipdf.go
@@ -253,17 +253,14 @@ type pdfRequestBody struct {
watchdog *pdfIdleWatchdog
}
-func (b *pdfRequestBody) Read(p []byte) (int, error) {
- b.watchdog.clientIO(1)
- defer b.watchdog.clientIO(-1)
- if err := b.watchdog.readDeadline(b.rc, time.Now().Add(b.watchdog.idle)); err != nil {
+func (b *pdfRequestBody) Read(p []byte) (n int, err error) {
+ b.watchdog.clientStart()
+ defer func() { b.watchdog.clientDone(err) }()
+ if err = b.watchdog.readDeadline(b.rc, time.Now().Add(b.watchdog.idle)); err != nil {
b.aborted.Store(true)
return 0, err
}
- n, err := b.ReadCloser.Read(p)
- if n > 0 {
- b.watchdog.progress()
- }
+ n, err = b.ReadCloser.Read(p)
if err == io.EOF {
_ = b.watchdog.readDeadline(b.rc, time.Time{})
} else if err != nil {
@@ -325,22 +322,19 @@ func (w *pdfResponseWriter) WriteHeader(status int) {
}
w.ResponseWriter.WriteHeader(status)
}
-func (w *pdfResponseWriter) Write(p []byte) (int, error) {
+func (w *pdfResponseWriter) Write(p []byte) (n int, err error) {
if w.watchdog != nil {
- w.watchdog.clientIO(1)
- defer w.watchdog.clientIO(-1)
- if err := w.writeDeadline(); err != nil {
+ w.watchdog.clientStart()
+ defer func() { w.watchdog.clientDone(err) }()
+ if err = w.writeDeadline(); err != nil {
return 0, err
}
}
if w.status == 0 {
w.WriteHeader(http.StatusOK)
}
- n, err := w.ResponseWriter.Write(p)
+ n, err = w.ResponseWriter.Write(p)
w.bytes += int64(n)
- if n > 0 && w.watchdog != nil {
- w.watchdog.progress()
- }
return n, err
}
@@ -352,22 +346,23 @@ func (w *pdfResponseWriter) writeDeadline() error {
return err
}
-func (w *pdfResponseWriter) FlushError() error {
- w.watchdog.clientIO(1)
- defer w.watchdog.clientIO(-1)
- if err := w.writeDeadline(); err != nil {
+func (w *pdfResponseWriter) FlushError() (err error) {
+ w.watchdog.clientStart()
+ defer func() { w.watchdog.clientDone(err) }()
+ if err = w.writeDeadline(); err != nil {
return err
}
- err := w.rc.Flush()
+ err = w.rc.Flush()
if errors.Is(err, http.ErrNotSupported) {
return nil
}
return err
}
-// The timer spans engine writes, response headers and response reads. Client
-// reads and writes have socket deadlines too; tracking them attributes a stall
-// to the client even if the watchdog fires before the socket timeout does.
+// The engine owns the timer between client I/O calls. While a client read,
+// write or flush is pending, the timer bounds that call instead. Completing
+// client I/O records its timeout before returning ownership to the engine,
+// which gets a fresh interval: time waiting for the client is not engine idle.
type pdfIdleWatchdog struct {
mu sync.Mutex
idle time.Duration
@@ -422,16 +417,42 @@ func (w *pdfIdleWatchdog) readDeadline(rc *http.ResponseController, deadline tim
func (w *pdfIdleWatchdog) progress() {
w.mu.Lock()
defer w.mu.Unlock()
- if !w.stopped && w.expired == "" {
+ if !w.stopped && w.expired == "" && w.clients == 0 {
w.deadline = time.Now().Add(w.idle)
w.timer.Reset(w.idle)
}
}
-func (w *pdfIdleWatchdog) clientIO(delta int) {
+func (w *pdfIdleWatchdog) clientStart() {
w.mu.Lock()
defer w.mu.Unlock()
- w.clients += delta
+ w.clients++
+ if !w.stopped && w.expired == "" && w.clients == 1 {
+ w.deadline = time.Now().Add(w.idle)
+ w.timer.Reset(w.idle)
+ }
+}
+
+func (w *pdfIdleWatchdog) clientDone(err error) {
+ w.mu.Lock()
+ defer w.mu.Unlock()
+ w.clients--
+ if w.stopped || w.expired != "" {
+ return
+ }
+ // A socket deadline can beat the timer callback. Keep its attribution
+ // even after the client call returns and Transport starts cleaning up.
+ var network net.Error
+ if errors.As(err, &network) && network.Timeout() {
+ w.expired = pdfAborted
+ w.timer.Stop()
+ w.cancel()
+ return
+ }
+ if w.clients == 0 {
+ w.deadline = time.Now().Add(w.idle)
+ w.timer.Reset(w.idle)
+ }
}
func (w *pdfIdleWatchdog) outcome() pdfOutcome {
diff --git a/internal/console/apipdf_test.go b/internal/console/apipdf_test.go
index 1d21bcda1..020ef097d 100644
--- a/internal/console/apipdf_test.go
+++ b/internal/console/apipdf_test.go
@@ -14,9 +14,11 @@ import (
"net/http/httptest"
"net/http/httptrace"
"net/textproto"
+ "os"
"strings"
"sync/atomic"
"testing"
+ "testing/synctest"
"time"
"github.com/carlosframework/platform/internal/svcrole"
@@ -819,3 +821,91 @@ func TestPDFDoorExpiredUploadDoesNotResume(t *testing.T) {
t.Fatalf("expired upload resumed: %d bytes, %v", n, err)
}
}
+
+type pdfIdleTestReader func([]byte) (int, error)
+
+func (read pdfIdleTestReader) Read(p []byte) (int, error) { return read(p) }
+
+type pdfIdleTestWriter struct {
+ *httptest.ResponseRecorder
+ write func([]byte) (int, error)
+}
+
+func (w *pdfIdleTestWriter) Write(p []byte) (int, error) { return w.write(p) }
+func (w *pdfIdleTestWriter) FlushError() error {
+ _, err := w.write(nil)
+ return err
+}
+
+func TestPDFDoorIdleClientTimeoutOrder(t *testing.T) {
+ for _, direction := range []string{"upload", "download", "flush"} {
+ for _, watchdogFirst := range []bool{false, true} {
+ t.Run(fmt.Sprintf("%s/watchdog-first=%t", direction, watchdogFirst), func(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ watchdog := newPDFIdleWatchdog(time.Second, func() {})
+ defer watchdog.stop()
+ // Dispatch the callback ourselves to cover both orderings,
+ // including a socket timeout that returns before the callback.
+ watchdog.timer.Stop()
+ stall := func([]byte) (int, error) {
+ watchdog.timer.Stop()
+ time.Sleep(time.Second)
+ if watchdogFirst {
+ watchdog.expire()
+ }
+ return 0, os.ErrDeadlineExceeded
+ }
+ writer := &pdfIdleTestWriter{ResponseRecorder: httptest.NewRecorder(), write: stall}
+ rc := http.NewResponseController(writer)
+ var err error
+ if direction == "upload" {
+ body := &pdfRequestBody{ReadCloser: io.NopCloser(pdfIdleTestReader(stall)), rc: rc, watchdog: watchdog}
+ _, err = body.Read(make([]byte, 1))
+ } else {
+ out := &pdfResponseWriter{ResponseWriter: writer, rc: rc, watchdog: watchdog}
+ if direction == "download" {
+ _, err = out.Write([]byte("x"))
+ } else {
+ err = out.FlushError()
+ }
+ }
+ if !errors.Is(err, os.ErrDeadlineExceeded) {
+ t.Fatalf("client I/O = %v, want timeout", err)
+ }
+ watchdog.expire()
+ if got := watchdog.outcome(); got != pdfAborted {
+ t.Fatalf("client stall = %q, want aborted", got)
+ }
+ })
+ })
+ }
+ }
+}
+
+func TestPDFDoorIdleEngineAfterClientEOF(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ watchdog := newPDFIdleWatchdog(time.Second, func() {})
+ defer watchdog.stop()
+ body := &pdfRequestBody{
+ ReadCloser: io.NopCloser(pdfIdleTestReader(func([]byte) (int, error) {
+ time.Sleep(750 * time.Millisecond)
+ return 0, io.EOF
+ })),
+ rc: http.NewResponseController(httptest.NewRecorder()),
+ watchdog: watchdog,
+ }
+ if _, err := body.Read(make([]byte, 1)); err != io.EOF {
+ t.Fatal(err)
+ }
+ time.Sleep(250 * time.Millisecond)
+ synctest.Wait()
+ if got := watchdog.outcome(); got != "" {
+ t.Fatalf("time waiting for the client charged to the engine: %q", got)
+ }
+ time.Sleep(750 * time.Millisecond)
+ synctest.Wait()
+ if got := watchdog.outcome(); got != pdfFailed {
+ t.Fatalf("engine stall after upload = %q, want failed", got)
+ }
+ })
+}