Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

middleware: a streaming request's runtimes let its body go once its request hooks are done (#3383)

yetoneb0d0092be52f
diff --git a/internal/middleware/middleware_test.go b/internal/middleware/middleware_test.go
index 23c9f395f..6051b2e37 100644
--- a/internal/middleware/middleware_test.go
+++ b/internal/middleware/middleware_test.go
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
+ "runtime"
"slices"
"strings"
"sync"
@@ -486,3 +487,43 @@ type discard struct{}
func (discard) Header() http.Header { return http.Header{"Content-Type": {"text/event-stream"}} }
func (discard) Write(p []byte) (int, error) { return len(p), nil }
func (discard) WriteHeader(int) {}
+
+// A request whose reply is streaming holds its runtimes, and they hold
+// neither its body as the hooks were given it nor what they made of it:
+// a long context's body was kept alive until the reply ended, ~10 MB of a
+// 7 MB body a request, and with many agents' requests streaming at once,
+// hundreds of MB (#3383). What a hook put in ctx.state stays.
+func TestStreamingRequestHoldsNoBody(t *testing.T) {
+ use(t, file(t, `export function onRequest(body, ctx) { ctx.state.n = body.messages.length; }
+export function onProvider(body, ctx) { ctx.state.p = ctx.provider.id; }
+export function onEvent(ev, ctx) { ev.n = ctx.state.n; return ev; }`))
+ body := []byte(`{"model":"m1","messages":[` + strings.Repeat(`{"role":"user","content":"`+strings.Repeat("x", 1000)+`"},`, 8000) + `{"role":"user","content":"y"}]}`)
+ heap := func() int64 {
+ runtime.GC()
+ var m runtime.MemStats
+ runtime.ReadMemStats(&m)
+ return int64(m.HeapAlloc)
+ }
+ before := heap()
+ runs := make([]*Run, 8)
+ for i := range runs {
+ runs[i] = Begin(info())
+ runs[i].Request(body)
+ runs[i].Tried(body, gwmw.To{ID: "p1", Model: "m1"})
+ }
+ held := heap() - before
+ for _, r := range runs {
+ rec := httptest.NewRecorder()
+ w, done := r.Wrap(rec)
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Write([]byte("data: {\"a\":1}\n\n"))
+ done()
+ if !strings.Contains(rec.Body.String(), `"n":8001`) {
+ t.Errorf("ctx.state was lost: %s", rec.Body)
+ }
+ r.End()
+ }
+ if held > int64(len(body)) {
+ t.Fatalf("8 streaming requests hold %d MB, their body is %d MB", held>>20, len(body)>>20)
+ }
+}
diff --git a/internal/middleware/run.go b/internal/middleware/run.go
index 2c0a0ffec..e923a09d4 100644
--- a/internal/middleware/run.go
+++ b/internal/middleware/run.go
@@ -72,6 +72,20 @@ func (r *Run) End() {
}
}
+// letGo lets the runtimes drop the request's body as its hooks were
+// given it, and what they made of it: a runtime is the request's until its
+// reply has streamed, minutes on a long one, and kept a long context's
+// body alive all that time, ~10 MB a 7 MB body (#3383). ctx, and what
+// ctx.state holds, stay. Not after each onEvent: an event is small, and
+// its hooks run a thousand times a reply.
+func (r *Run) letGo() {
+ for _, in := range r.insts {
+ if in != nil {
+ in.rt.ReleaseCallData()
+ }
+ }
+}
+
// acquire is middleware i's runtime for this request and its ctx: the
// request's Info, the plugin's options, a state of its own and reject.
func (r *Run) acquire(i int) (*inst, moejs.Value, error) {
@@ -130,6 +144,7 @@ func (r *Run) Request(body []byte) ([]byte, *Reject) {
if r == nil {
return body, nil
}
+ defer r.letGo()
for i, m := range r.set.list {
if !m.hasReq {
continue
@@ -159,6 +174,7 @@ func (r *Run) Tried(body []byte, to gwmw.To) []byte {
if r == nil {
return body
}
+ defer r.letGo()
b, _ := json.Marshal(to)
for i, m := range r.set.list {
if !m.hasProv {