Skip to content

Commit b690e80

Browse files
author
Marcos
committed
fix(server): resolve stdio server context cancellation bug
Fix a critical bug in the stdio server's readNextLine function that was causing "Error reading input" and "context cancelled" errors when Claude Desktop attempted to connect to MCP servers. The issue was in the select-default pattern that would immediately bypass context cancellation checks and proceed with blocking ReadString operations even when the context was cancelled. This prevented proper cleanup and caused connection failures. Changes: - Remove problematic select-default pattern in readNextLine goroutine - Simplify the read logic to directly call reader.ReadString('\n') - Maintain proper done channel handling for result communication - Ensure context cancellation is respected throughout the read operation This resolves connection issues introduced in version 0.30.0 where the stdio transport became unreliable for client connections. Fixes: stdio server connection failures with Claude Desktop
1 parent 243a292 commit b690e80

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

server/stdio.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,18 @@ func (s *StdioServer) readNextLine(ctx context.Context, reader *bufio.Reader) (s
195195
defer close(done)
196196

197197
go func() {
198-
select {
199-
case <-done:
200-
return
201-
default:
202-
line, err := reader.ReadString('\n')
203-
if err != nil {
204-
select {
205-
case errChan <- err:
206-
case <-done:
207-
}
208-
return
209-
}
198+
line, err := reader.ReadString('\n')
199+
if err != nil {
210200
select {
211-
case readChan <- line:
201+
case errChan <- err:
212202
case <-done:
213203
}
214204
return
215205
}
206+
select {
207+
case readChan <- line:
208+
case <-done:
209+
}
216210
}()
217211

218212
select {

0 commit comments

Comments
 (0)