From 818662352c565b1205c01be0e3ee0aab3e2ea05f Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 23 Jun 2025 16:50:39 +0100 Subject: [PATCH] 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. --- Library/Homebrew/mcp_server.rb | 2 ++ Library/Homebrew/test/mcp_server_spec.rb | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/Library/Homebrew/mcp_server.rb b/Library/Homebrew/mcp_server.rb index 2ce1c89e11..5e0e440a56 100644 --- a/Library/Homebrew/mcp_server.rb +++ b/Library/Homebrew/mcp_server.rb @@ -155,6 +155,8 @@ module Homebrew input = if ping_switch? { jsonrpc: JSON_RPC_VERSION, id: 1, method: "ping" }.to_json else + break if @stdin.eof? + @stdin.gets end next if input.nil? || input.strip.empty? diff --git a/Library/Homebrew/test/mcp_server_spec.rb b/Library/Homebrew/test/mcp_server_spec.rb index 98f31469cf..96c303fe5e 100644 --- a/Library/Homebrew/test/mcp_server_spec.rb +++ b/Library/Homebrew/test/mcp_server_spec.rb @@ -228,6 +228,8 @@ RSpec.describe Homebrew::McpServer do end it "exits on Interrupt" do + stdin.puts + stdin.rewind allow(stdin).to receive(:gets).and_raise(Interrupt) expect do server.run @@ -237,6 +239,8 @@ RSpec.describe Homebrew::McpServer do end it "exits on error" do + stdin.puts + stdin.rewind allow(stdin).to receive(:gets).and_raise(StandardError, "fail") expect do server.run