Skip to content

Commit 73b96c4

Browse files
committed
refactor(mcp): simplify terminate duration handling in CommandTransport
1 parent d7e56a4 commit 73b96c4

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

mcp/cmd.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
const (
1717
defaultTerminateDuration = 5 * time.Second
18-
minTerminateDuration = 1 * time.Second
1918
)
2019

2120
// A CommandTransport is a [Transport] that runs a command and communicates
@@ -24,7 +23,7 @@ type CommandTransport struct {
2423
Command *exec.Cmd
2524
// TerminateDuration controls how long Close waits after closing stdin
2625
// for the process to exit before sending SIGTERM.
27-
// If less than 1 second (including zero or negative), the default of 5s is used.
26+
// If zero or negative, the default of 5s is used.
2827
TerminateDuration time.Duration
2928
}
3029

@@ -55,11 +54,11 @@ func (t *CommandTransport) Connect(ctx context.Context) (Connection, error) {
5554
if err := t.Command.Start(); err != nil {
5655
return nil, err
5756
}
58-
terminateDuration := t.TerminateDuration
59-
if terminateDuration < minTerminateDuration {
60-
terminateDuration = defaultTerminateDuration
57+
td := t.TerminateDuration
58+
if td <= 0 {
59+
td = defaultTerminateDuration
6160
}
62-
return newIOConn(&pipeRWC{t.Command, stdout, stdin, terminateDuration}), nil
61+
return newIOConn(&pipeRWC{t.Command, stdout, stdin, td}), nil
6362
}
6463

6564
// A pipeRWC is an io.ReadWriteCloser that communicates with a subprocess over

mcp/cmd_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func TestCommandTransportTerminateDuration(t *testing.T) {
273273
defer cancel()
274274

275275
// Use a command that won't exit when stdin is closed
276-
cmd := exec.Command("sleep", "3600")
276+
cmd := exec.Command("sleep", "20")
277277
transport := &mcp.CommandTransport{
278278
Command: cmd,
279279
TerminateDuration: tt.duration,
@@ -288,10 +288,11 @@ func TestCommandTransportTerminateDuration(t *testing.T) {
288288
err = conn.Close()
289289
elapsed := time.Since(start)
290290

291-
// Close() may return "signal: terminated" when the subprocess is killed,
292-
// which is expected behavior for our test with a non-responsive subprocess
293-
if err != nil && err.Error() != "signal: terminated" {
294-
t.Fatalf("Close() failed with unexpected error: %v", err)
291+
if err != nil {
292+
var exitErr *exec.ExitError
293+
if !errors.As(err, &exitErr) {
294+
t.Fatalf("Close() failed with unexpected error: %v", err)
295+
}
295296
}
296297

297298
if elapsed > tt.wantMaxDuration {

0 commit comments

Comments
 (0)