Files
magpie/internal/gateway/image_mcp_memory_test.go
T
yetone b2ccfe25dd gateway: a drawn image is kept on disk for its hour, and the reply it came in is let go (tnt12231 on X)
The image MCP server kept every drawing, and every video, in memory for an
hour (up to 256 MB), and swept them only when another image arrived. A
drawing also held its reply, the base64 strings parsed from it and the decoded
bytes at once. Now:
- imageMCPKeepFile writes the image to <config>/image-mcp (emptied at the
  first use in a run) and the file URL serves it from there; only the
  metadata is in memory. A timer removes it at its hour. If it can't be
  written the bytes stay in memory, as before.
- imageMCPDrawn drops the recorded reply and each b64 string as it is decoded;
  the stdio server (internal/imagemcp) clears each b64 once decoded.
- both hand the heap back (debug.FreeOSMemory) when a drawing is done: the
  Go scavenger gives it back slowly, and Task Manager showed it as
  magpie's.

The foreground/background numbers in the report (WebView2 processes a few MB
when hidden) are WebView2 suspending, not a leak.

Run: TestImageMCPKeepsDrawingsOffTheHeap (new; 6 x 12 MB drawings kept):
heap +71 MB on the old code, +0 after. internal/imagemcp and the gateway's
Image/Video/MCP/Browser/Record tests, gofmt, and vet for darwin, linux
and windows on mote. Broke it by hand: the old image_mcp.go with the new
test is red. Not run: the full suite, a real vendor's drawing, Windows.
2026-11-13 10:36:05 +08:00

63 lines
1.6 KiB
Go

package gateway
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"net/http"
"runtime"
"testing"
)
// Images the image MCP server draws are kept for an hour so a link can
// download them. Kept in memory, five 12 MB drawings were 60 MB of the
// process's heap until an hour later or the next drawing (tnt12231 on X:
// magpie's memory at 1 GB+ while drawing). They are kept on disk, and the
// reply they came in is let go.
func TestImageMCPKeepsDrawingsOffTheHeap(t *testing.T) {
s, _ := easeled(t)
t.Setenv("MAGPIE_PUBLIC_URL", "")
heap := func() uint64 {
var m runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m)
return m.HeapAlloc
}
before := heap()
body := func() []byte {
img := make([]byte, 12<<20)
rand.Read(img)
copy(img, "\x89PNG\r\n\x1a\n")
b64 := base64.StdEncoding.EncodeToString(img)
type item struct {
B64 string `json:"b64_json"`
}
b, _ := json.Marshal(map[string]any{"model": "m", "data": []item{{b64}, {b64}}})
return b
}()
var urls []string
for range 3 {
rec := &replyRecorder{header: http.Header{}}
rec.body.Write(body)
res := s.imageMCPDrawn(rec, "", "http://127.0.0.1:3425")
if res["isError"] == true {
t.Fatalf("%v", res)
}
urls = append(urls, "x")
}
after := heap() - uint64(len(body))
runtime.KeepAlive(body)
st := &s.imageStore
st.mu.Lock()
n, kept := len(st.files), st.bytes
st.mu.Unlock()
if n != 6 || kept < 6*(12<<20) {
t.Fatalf("kept %d files, %d bytes", n, kept)
}
// 72 MB of drawings are kept: the heap holds none of them
if grew := int64(after) - int64(before); grew > 16<<20 {
t.Fatalf("the heap grew by %d MB with %d MB of drawings kept", grew>>20, kept>>20)
}
}