mcp_server: fix EOF handling.

We need to handle when `stdin` is closed but there's no interrupt
signal.

Without this, the server will be stuck an in infinite busy loop.
This commit is contained in:
Mike McQuaid 2025-06-23 16:50:39 +01:00
parent c19f08b3e8
commit 818662352c
No known key found for this signature in database
2 changed files with 6 additions and 0 deletions

View File

@ -155,6 +155,8 @@ module Homebrew
input = if ping_switch? input = if ping_switch?
{ jsonrpc: JSON_RPC_VERSION, id: 1, method: "ping" }.to_json { jsonrpc: JSON_RPC_VERSION, id: 1, method: "ping" }.to_json
else else
break if @stdin.eof?
@stdin.gets @stdin.gets
end end
next if input.nil? || input.strip.empty? next if input.nil? || input.strip.empty?

View File

@ -228,6 +228,8 @@ RSpec.describe Homebrew::McpServer do
end end
it "exits on Interrupt" do it "exits on Interrupt" do
stdin.puts
stdin.rewind
allow(stdin).to receive(:gets).and_raise(Interrupt) allow(stdin).to receive(:gets).and_raise(Interrupt)
expect do expect do
server.run server.run
@ -237,6 +239,8 @@ RSpec.describe Homebrew::McpServer do
end end
it "exits on error" do it "exits on error" do
stdin.puts
stdin.rewind
allow(stdin).to receive(:gets).and_raise(StandardError, "fail") allow(stdin).to receive(:gets).and_raise(StandardError, "fail")
expect do expect do
server.run server.run