Repository navigation
Make the fixture-server readiness check detect refused connections - #160
Merged
Merged
Conversation
TestServerManager#wait_for_port (and port_open?) probed the fixture servers with Socket.tcp(host, port, connect_timeout: 1). On Darwin with Ruby <= 4.0.6, Socket.tcp returns a socket for a refused connection because the retry connect(2) answers EISCONN and the library does not check SO_ERROR (ruby-core Bug #22223, fixed in Ruby 4.0.7). The readiness check therefore returned immediately after spawn, and the first spec to touch a server raced its boot: "Connection refused" on the streamable transport and "SSE transport closed" for every example in the SSE files, depending on which file ran first. Use TCPSocket.new(host, port, connect_timeout: 1), whose C connect path checks SO_ERROR and raises ECONNREFUSED reliably, so start_server blocks until every fixture port is actually accepting connections.
jordan-brough
marked this pull request as ready for review
September 22, 2026 13:42
jordan-brough
marked this pull request as draft
September 22, 2026 13:42
jordan-brough
marked this pull request as ready for review
September 22, 2026 13:42
Owner
|
Sweet! Thanks for this @jordan-brough. Merging this now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What you'll see
Run one of the SSE spec files on its own on a Mac, for example:
Every example fails with
SSE transport closed. The streamable spec file fails one example withConnection refused. The full suite passes, because earlier spec files take long enough for the fixture servers to finish booting. So this is easy to miss.Why it happens
TestServerManagerstarts the fixture servers and then asks "is the port open yet?" before letting the tests run. That check usesSocket.tcp(host, port, connect_timeout: 1).On macOS with Ruby up to 4.0.6, that call answers "yes" even when nothing is listening on the port. This is a Ruby bug: https://bugs.ruby-lang.org/issues/22223. The retried
connect(2)reportsEISCONNand Ruby did not checkSO_ERROR, so a refused connection looked like a successful one.As a result, the readiness check returns right away and the tests start before the servers are up. The first spec to talk to a server gets a refused connection.
The fix
Use
TCPSocket.new(host, port, connect_timeout: 1)for the probe instead. Its connect path checksSO_ERRORand raisesECONNREFUSEDcorrectly, sostart_serverblocks until every fixture port is really accepting connections.One file, a few lines:
spec/support/test_server_manager.rb(wait_for_portandport_open?), plus a short comment explaining why.What changes in behavior
Server startup now waits until the ports are really listening, which takes about half a second. The isolated runs pass. The full suite is unchanged.
Measured on macOS with Ruby 4.0.6:
start_serverreturns afternative/transports/sse_spec.rbaloneadapters/mcp_sdk_sse_spec.rbaloneadapters/mcp_sdk_streamable_spec.rbaloneRuby 4.0.7 fixes the underlying
Socket.tcpbug, but the probe should be correct on older Rubies too.