Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
fix: subscriberQueue.pop - release popped lines, not just their slot
Fernando Marinod44580d485b2
diff --git a/internal/node/stdio_bridge.go b/internal/node/stdio_bridge.goindex f3d096ad..097b7acc 100644--- a/internal/node/stdio_bridge.go+++ b/internal/node/stdio_bridge.go@@ -288,7 +288,18 @@ func (q *subscriberQueue) pop(ctx context.Context) (line string, ok bool, err erq.mu.Lock()if len(q.buf) > 0 {line = q.buf[0]+ // Re-slicing alone leaves the popped element's string header+ // live in the backing array, keeping its bytes reachable for+ // as long as the array itself is - which, for a long-lived+ // session, is every line ever queued. Clear the slot before+ // advancing, and drop the backing array entirely once drained,+ // rather than let it sit at its peak size for the queue's+ // remaining lifetime.+ q.buf[0] = ""q.buf = q.buf[1:]+ if len(q.buf) == 0 {+ q.buf = nil+ }q.mu.Unlock()return line, true, nil}diff --git a/internal/node/stdio_bridge_test.go b/internal/node/stdio_bridge_test.goindex 9d2035b1..fe8240da 100644--- a/internal/node/stdio_bridge_test.go+++ b/internal/node/stdio_bridge_test.go@@ -163,3 +163,42 @@ func TestStdioBridge_SendWritesToStdin(t *testing.T) {t.Fatalf("Send: stdin got %q, want %q", got, want)}}++// TestSubscriberQueue_PopReleasesBackingArray guards against a real memory+// retention bug flagged in review: re-slicing alone (q.buf = q.buf[1:])+// leaves the popped element's string header live in the backing array,+// keeping its bytes reachable for as long as the array itself is - which,+// for a long-lived session, is every line ever queued. pop must clear the+// popped slot and drop the backing array entirely once drained.+func TestSubscriberQueue_PopReleasesBackingArray(t *testing.T) {+ q := newSubscriberQueue()+ q.push("a")+ q.push("b")++ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)+ defer cancel()++ // A second reference into the same backing array, captured before any+ // pop: re-slicing q.buf alone wouldn't touch this array's contents, so+ // backing[0] would still read "a" after popping it if pop didn't+ // explicitly clear the slot.+ q.mu.Lock()+ backing := q.buf[:2:2]+ q.mu.Unlock()++ if got, ok, err := q.pop(ctx); err != nil || !ok || got != "a" {+ t.Fatalf("pop 1: got %q, ok=%v, err=%v", got, ok, err)+ }+ if backing[0] != "" {+ t.Fatalf("pop: backing array slot still holds %q after being popped, want \"\" (string not released)", backing[0])+ }++ if got, ok, err := q.pop(ctx); err != nil || !ok || got != "b" {+ t.Fatalf("pop 2: got %q, ok=%v, err=%v", got, ok, err)+ }+ q.mu.Lock()+ defer q.mu.Unlock()+ if q.buf != nil {+ t.Fatalf("pop: buf = %#v after full drain, want nil (backing array not released)", q.buf)+ }+}