Skip to content

Agent Mesh

fix: stdio_bridge - never drop a subscriber's line on a slow consumer

Fernando Marino17da7f6b185f
diff --git a/internal/node/bridge_transport.go b/internal/node/bridge_transport.go
index ad190353..6053e581 100644
--- a/internal/node/bridge_transport.go
+++ b/internal/node/bridge_transport.go
@@ -23,12 +23,12 @@ import (
)
// bridgeTransport adapts a StdioBridge to the mcp.Transport interface.
-// Reads pull lines from a Subscribe channel; writes go through Send.
+// Reads pull lines from a Subscribe queue; writes go through Send.
// Multiple bridgeTransports can coexist on one bridge; JSON-RPC IDs
// distinguish their responses, as with HTTP/SSE subscribers.
type bridgeTransport struct {
bridge *StdioBridge
- ch <-chan string
+ queue *subscriberQueue
unsub func()
}
@@ -37,22 +37,21 @@ func newBridgeTransport(b *StdioBridge) *bridgeTransport {
}
func (t *bridgeTransport) Connect(ctx context.Context) (mcp.Connection, error) {
- ch, unsub := t.bridge.Subscribe()
- t.ch = ch
+ queue, unsub := t.bridge.Subscribe()
+ t.queue = queue
t.unsub = unsub
return t, nil
}
func (t *bridgeTransport) Read(ctx context.Context) (jsonrpc.Message, error) {
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case line, ok := <-t.ch:
- if !ok {
- return nil, io.EOF
- }
- return jsonrpc.DecodeMessage([]byte(line))
+ line, ok, err := t.queue.pop(ctx)
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ return nil, io.EOF
}
+ return jsonrpc.DecodeMessage([]byte(line))
}
func (t *bridgeTransport) Write(ctx context.Context, msg jsonrpc.Message) error {
diff --git a/internal/node/stdio_bridge.go b/internal/node/stdio_bridge.go
index dd93ed58..f3d096ad 100644
--- a/internal/node/stdio_bridge.go
+++ b/internal/node/stdio_bridge.go
@@ -16,6 +16,7 @@ package node
import (
"bufio"
+ "context"
"encoding/json"
"fmt"
"io"
@@ -33,11 +34,13 @@ type StdioBridge struct {
stdout io.ReadCloser
mu sync.Mutex
clients map[chan string]bool
+ queues map[*subscriberQueue]bool
calls map[string]chan string
}
func (b *StdioBridge) Start() {
b.clients = make(map[chan string]bool)
+ b.queues = make(map[*subscriberQueue]bool)
b.calls = make(map[string]chan string)
go func() {
scanner := bufio.NewScanner(b.stdout)
@@ -66,6 +69,9 @@ func (b *StdioBridge) Start() {
default:
}
}
+ for q := range b.queues {
+ q.push(line)
+ }
b.mu.Unlock()
}
@@ -74,6 +80,10 @@ func (b *StdioBridge) Start() {
close(ch)
delete(b.clients, ch)
}
+ for q := range b.queues {
+ q.closeQueue()
+ }
+ b.queues = make(map[*subscriberQueue]bool)
for _, ch := range b.calls {
close(ch)
}
@@ -200,27 +210,99 @@ func (b *StdioBridge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
-// Subscribe registers a new subscriber channel for stdout lines and returns
-// it along with an idempotent unsubscribe function. Buffered (cap 10); drops
-// on slow consumers match the SSE behaviour in ServeHTTP.
-func (b *StdioBridge) Subscribe() (<-chan string, func()) {
- ch := make(chan string, 10)
+// Subscribe registers a new subscriber queue for stdout lines and returns it
+// along with an idempotent unsubscribe function.
+//
+// Unlike the plain chan string clients map above (bounded cap 10, drops on a
+// slow consumer - fine for the ServeHTTP SSE feed, where a browser tab
+// missing a line just misses a live update), Subscribe's only caller is
+// bridgeTransport, i.e. an MCP request/response session: every line matters,
+// because the one dropped could be the exact reply a Read() call is blocked
+// on, with nothing left to ever wake it. bridgeTransport used to share the
+// same bounded, drop-on-full channel as SSE; a periodic health probe and a
+// live tools/call session subscribing to the same backend concurrently was
+// enough to lose a message that way, surfacing as a client-side EOF even
+// though the backend had answered correctly. Every subscriber here instead
+// gets its own unbounded, order-preserving queue.
+func (b *StdioBridge) Subscribe() (*subscriberQueue, func()) {
+ q := newSubscriberQueue()
b.mu.Lock()
- b.clients[ch] = true
+ b.queues[q] = true
b.mu.Unlock()
var once sync.Once
unsub := func() {
once.Do(func() {
b.mu.Lock()
- if b.clients[ch] {
- delete(b.clients, ch)
- close(ch)
- }
+ delete(b.queues, q)
b.mu.Unlock()
+ q.closeQueue()
})
}
- return ch, unsub
+ return q, unsub
+}
+
+// subscriberQueue is an unbounded, order-preserving mailbox for one
+// bridgeTransport session's stdout lines.
+//
+// push is called from the bridge's single stdout-scanning goroutine (with
+// StdioBridge.mu held) and must never block or drop a line. pop is called by
+// the session's own reader; it blocks until a line is queued, the bridge
+// closes the queue, or ctx is done.
+type subscriberQueue struct {
+ mu sync.Mutex
+ buf []string
+ closed bool
+ notify chan struct{} // capacity 1: a "there may be new work" flag, coalesced
+}
+
+func newSubscriberQueue() *subscriberQueue {
+ return &subscriberQueue{notify: make(chan struct{}, 1)}
+}
+
+func (q *subscriberQueue) push(line string) {
+ q.mu.Lock()
+ q.buf = append(q.buf, line)
+ q.mu.Unlock()
+ q.wake()
+}
+
+func (q *subscriberQueue) closeQueue() {
+ q.mu.Lock()
+ q.closed = true
+ q.mu.Unlock()
+ q.wake()
+}
+
+func (q *subscriberQueue) wake() {
+ select {
+ case q.notify <- struct{}{}:
+ default:
+ }
+}
+
+// pop returns the next queued line in FIFO order. ok is false only once the
+// queue has been closed and fully drained.
+func (q *subscriberQueue) pop(ctx context.Context) (line string, ok bool, err error) {
+ for {
+ q.mu.Lock()
+ if len(q.buf) > 0 {
+ line = q.buf[0]
+ q.buf = q.buf[1:]
+ q.mu.Unlock()
+ return line, true, nil
+ }
+ closed := q.closed
+ q.mu.Unlock()
+ if closed {
+ return "", false, nil
+ }
+ select {
+ case <-ctx.Done():
+ return "", false, ctx.Err()
+ case <-q.notify:
+ }
+ }
}
// Send writes data to the child's stdin, appending a newline.
diff --git a/internal/node/stdio_bridge_test.go b/internal/node/stdio_bridge_test.go
index f219c59d..9d2035b1 100644
--- a/internal/node/stdio_bridge_test.go
+++ b/internal/node/stdio_bridge_test.go
@@ -16,7 +16,10 @@ package node
import (
"bytes"
+ "context"
+ "fmt"
"io"
+ "sync"
"testing"
"time"
)
@@ -42,23 +45,99 @@ func TestStdioBridge_SubscribeReceivesLines(t *testing.T) {
b, stdoutWriter, _ := newPipeBridge()
defer func() { _ = stdoutWriter.Close() }()
- ch, unsub := b.Subscribe()
+ q, unsub := b.Subscribe()
defer unsub()
go func() {
_, _ = stdoutWriter.Write([]byte("hello\nworld\n"))
}()
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
want := []string{"hello", "world"}
for _, w := range want {
- select {
- case got := <-ch:
- if got != w {
- t.Fatalf("Subscribe: got %q, want %q", got, w)
- }
- case <-time.After(2 * time.Second):
- t.Fatalf("Subscribe: timed out waiting for %q", w)
+ got, ok, err := q.pop(ctx)
+ if err != nil {
+ t.Fatalf("Subscribe: pop: %v", err)
}
+ if !ok {
+ t.Fatalf("Subscribe: queue closed before %q arrived", w)
+ }
+ if got != w {
+ t.Fatalf("Subscribe: got %q, want %q", got, w)
+ }
+ }
+}
+
+// TestStdioBridge_SubscribeDoesNotDropWhileConsumerIsBusy reproduces the
+// original bug: a burst of lines arrives while the subscriber isn't yet
+// blocked on a receive (e.g. a concurrent health-check probe still holding
+// StdioBridge.mu-adjacent work, or simply a goroutine that hasn't reached
+// its Read() call yet). The old bounded, drop-on-full channel lost lines in
+// exactly this window; the queue must never drop them.
+func TestStdioBridge_SubscribeDoesNotDropWhileConsumerIsBusy(t *testing.T) {
+ b, stdoutWriter, _ := newPipeBridge()
+ defer func() { _ = stdoutWriter.Close() }()
+
+ q, unsub := b.Subscribe()
+ defer unsub()
+
+ const n = 50
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ for i := 0; i < n; i++ {
+ _, _ = fmt.Fprintf(stdoutWriter, "line-%d\n", i)
+ }
+ }()
+ wg.Wait() // all n lines are written (and, on a slow reader, queued) before we ever call pop
+
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ for i := 0; i < n; i++ {
+ want := fmt.Sprintf("line-%d", i)
+ got, ok, err := q.pop(ctx)
+ if err != nil {
+ t.Fatalf("pop(%d): %v", i, err)
+ }
+ if !ok {
+ t.Fatalf("pop(%d): queue closed early, want %q", i, want)
+ }
+ if got != want {
+ t.Fatalf("pop(%d): got %q, want %q (a line was dropped or reordered)", i, got, want)
+ }
+ }
+}
+
+// TestStdioBridge_SubscribeCloseUnblocksPop ensures a pop() blocked waiting
+// for the next line returns promptly (ok=false) once the bridge's stdout
+// closes, rather than hanging forever.
+func TestStdioBridge_SubscribeCloseUnblocksPop(t *testing.T) {
+ b, stdoutWriter, _ := newPipeBridge()
+
+ q, unsub := b.Subscribe()
+ defer unsub()
+
+ done := make(chan struct{})
+ go func() {
+ defer close(done)
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ _, ok, err := q.pop(ctx)
+ if err != nil {
+ t.Errorf("pop: %v", err)
+ }
+ if ok {
+ t.Errorf("pop: got ok=true after close, want false")
+ }
+ }()
+
+ _ = stdoutWriter.Close()
+ select {
+ case <-done:
+ case <-time.After(2 * time.Second):
+ t.Fatal("pop did not unblock after stdout closed")
}
}