-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio.go
More file actions
268 lines (236 loc) · 7.54 KB
/
Copy pathstdio.go
File metadata and controls
268 lines (236 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package backend
import (
"context"
"fmt"
"log/slog"
"os"
"os/exec"
"sync"
"time"
"github.com/hoophq/mcpproxy/config"
)
// defaultShutdownGrace bounds each step of the shutdown escalation.
const defaultShutdownGrace = 3 * time.Second
// politeGrace caps how long Close waits for the child to notice stdin EOF
// before it starts sending signals.
const politeGrace = 250 * time.Millisecond
// StdioBackend runs an MCP server as a child process and speaks
// newline-delimited JSON-RPC over its stdin/stdout. It logs the child's
// stderr at debug level, since MCP servers use that stream for human-readable
// diagnostics rather than protocol traffic.
//
// Start puts the child in its own process group so Close reaps the whole
// tree. The common launcher (`npx some-mcp-server`) forks a node grandchild
// that would otherwise survive and keep holding the pipes.
type StdioBackend struct {
// ShutdownGrace bounds each step of Close's escalation (stdin EOF,
// SIGTERM, then SIGKILL). Zero means defaultShutdownGrace.
ShutdownGrace time.Duration
name string
cfg config.Backend
log *slog.Logger
conn *lineConn
done chan struct{}
cmd *exec.Cmd
stdout *os.File // parent read end of the child's stdout
stderr *os.File // parent read end of the child's stderr
mu sync.Mutex
started bool // Start was called
spawned bool // a child exists and a reaper goroutine is running
closing bool // Close initiated: a nonzero exit status is expected
err error
closeOnce sync.Once
closeErr error
}
// NewStdio builds an unstarted stdio backend. cfg.Command must be non-empty;
// cfg.Env is layered over the parent environment.
func NewStdio(name string, cfg config.Backend, log *slog.Logger) *StdioBackend {
if log == nil {
log = slog.Default()
}
return &StdioBackend{
name: name,
cfg: cfg,
log: log,
conn: newLineConn(name, log),
done: make(chan struct{}),
}
}
func (b *StdioBackend) Name() string { return b.name }
// Start spawns the child. Cancelling ctx terminates the whole process group.
func (b *StdioBackend) Start(ctx context.Context) error {
b.mu.Lock()
if b.started {
b.mu.Unlock()
return fmt.Errorf("stdio backend %s: already started", b.name)
}
b.started = true
b.mu.Unlock()
if len(b.cfg.Command) == 0 {
return fmt.Errorf("stdio backend %s: empty command", b.name)
}
cmd := exec.CommandContext(ctx, b.cfg.Command[0], b.cfg.Command[1:]...)
cmd.Env = mergeEnv(os.Environ(), b.cfg.Env)
setProcGroup(cmd)
// The default CommandContext cancel kills only the direct child; take
// the whole group down instead so grandchildren cannot linger.
cmd.Cancel = func() error { return signalGroup(cmd, sigTerm) }
// Own the pipes as plain os.Files rather than using cmd.StdoutPipe:
// exec's Wait holds until those pipes hit EOF, which deadlocks whenever
// a grandchild inherits stdout and outlives the child. With our own
// files, reap can Wait first and then unblock the readers by closing
// the read ends itself.
stdinR, stdinW, err := os.Pipe()
if err != nil {
return fmt.Errorf("stdio backend %s: stdin pipe: %w", b.name, err)
}
stdoutR, stdoutW, err := os.Pipe()
if err != nil {
closeAll(stdinR, stdinW)
return fmt.Errorf("stdio backend %s: stdout pipe: %w", b.name, err)
}
stderrR, stderrW, err := os.Pipe()
if err != nil {
closeAll(stdinR, stdinW, stdoutR, stdoutW)
return fmt.Errorf("stdio backend %s: stderr pipe: %w", b.name, err)
}
cmd.Stdin, cmd.Stdout, cmd.Stderr = stdinR, stdoutW, stderrW
if err := cmd.Start(); err != nil {
closeAll(stdinR, stdinW, stdoutR, stdoutW, stderrR, stderrW)
return fmt.Errorf("stdio backend %s: starting %q: %w", b.name, b.cfg.Command[0], err)
}
// The child owns its ends now; holding them in the parent would keep
// the pipes alive past child exit and defeat EOF detection.
closeAll(stdinR, stdoutW, stderrW)
b.mu.Lock()
b.cmd = cmd
b.spawned = true
b.mu.Unlock()
b.stdout, b.stderr = stdoutR, stderrR
b.conn.attach(stdinW)
var readers sync.WaitGroup
readers.Add(2)
go func() { defer readers.Done(); b.conn.pump(stdoutR) }()
go func() { defer readers.Done(); b.conn.logLines(stderrR) }()
go b.reap(cmd, &readers)
return nil
}
// reap waits for the child, releases the readers, then closes Recv and Done
// in that order so no send races the close.
func (b *StdioBackend) reap(cmd *exec.Cmd, readers *sync.WaitGroup) {
waitErr := cmd.Wait()
// The child is gone; any grandchild still holding the write ends is no
// longer our problem, so force the readers to EOF.
closeAll(b.stdout, b.stderr)
readers.Wait()
close(b.conn.recv)
b.mu.Lock()
// A nonzero status during Close comes from our own SIGTERM or
// SIGKILL, so it is not a fault worth reporting.
if !b.closing && waitErr != nil {
b.err = fmt.Errorf("stdio backend %s exited: %w", b.name, waitErr)
}
b.mu.Unlock()
close(b.done)
}
// Send writes one JSON-RPC message to the child's stdin.
func (b *StdioBackend) Send(_ context.Context, msg []byte) error {
select {
case <-b.done:
return ErrClosed
default:
}
if err := b.conn.send(msg); err != nil {
if err == ErrClosed {
return err
}
return fmt.Errorf("stdio backend %s: write: %w", b.name, err)
}
return nil
}
func (b *StdioBackend) Recv() <-chan []byte { return b.conn.recv }
func (b *StdioBackend) Done() <-chan struct{} { return b.done }
func (b *StdioBackend) Err() error {
b.mu.Lock()
defer b.mu.Unlock()
return b.err
}
// Close escalates until the child is gone: EOF on stdin (the polite exit
// signal for stdio MCP servers), SIGTERM to the process group, then SIGKILL.
// Signals target the group rather than the pid, so an `npx` launcher's node
// grandchild dies with it. Close blocks until reap finishes and tolerates
// repeated calls.
func (b *StdioBackend) Close() error {
b.closeOnce.Do(func() { b.closeErr = b.shutdown() })
return b.closeErr
}
func (b *StdioBackend) shutdown() error {
b.mu.Lock()
spawned := b.spawned
b.closing = true
b.mu.Unlock()
if !spawned {
// No child was ever created. Honour the Backend contract anyway
// so callers blocked on Recv or Done are released.
close(b.conn.stop)
close(b.conn.recv)
close(b.done)
return nil
}
// Release a pump blocked on a consumer that stopped draining, so the
// reaper can finish even if nobody reads Recv again.
defer close(b.conn.stop)
grace := b.ShutdownGrace
if grace <= 0 {
grace = defaultShutdownGrace
}
b.conn.closeWrite()
// A server that honours stdin EOF exits at once, so cap this step
// well short of the full grace before escalating.
if b.waitExit(min(grace, politeGrace)) {
return nil
}
if err := signalGroup(b.cmd, sigTerm); err != nil {
b.log.Debug("stdio backend term", "backend", b.name, "err", err)
}
if b.waitExit(grace) {
return nil
}
if err := signalGroup(b.cmd, sigKill); err != nil {
b.log.Debug("stdio backend kill", "backend", b.name, "err", err)
}
if b.waitExit(grace) {
return nil
}
return fmt.Errorf("stdio backend %s: child did not exit after SIGKILL", b.name)
}
func (b *StdioBackend) waitExit(d time.Duration) bool {
t := time.NewTimer(d)
defer t.Stop()
select {
case <-b.done:
return true
case <-t.C:
return false
}
}
// mergeEnv layers overrides onto base ("KEY=VALUE" entries). exec resolves
// duplicates to the last entry, so appending is enough.
func mergeEnv(base []string, overrides map[string]string) []string {
if len(overrides) == 0 {
return base
}
out := make([]string, len(base), len(base)+len(overrides))
copy(out, base)
for k, v := range overrides {
out = append(out, k+"="+v)
}
return out
}
func closeAll(files ...*os.File) {
for _, f := range files {
if f != nil {
_ = f.Close()
}
}
}