Skip to content

Commit 09c3103

Browse files
committed
Fix the flaky thread count assertion in the listen keepalive test
## Motivation and Context "listen keepalive is not started when the interval is nil" fails intermittently on CI. The most recent run failed on Ruby 3.2 alone while 3.1 and head passed, and a re-run of the same commit went green. The assertion samples a process-wide count: ```ruby before = Thread.list.size open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true }) assert_equal before, Thread.list.size, "a nil interval must not spawn a keepalive thread" ``` `Thread.list` covers the whole process, so the count moves for reasons this test has no interest in. The observed failure reported 54 against 51: three threads had *gone away* between the samples, not appeared. This file starts threads in sixteen places, and the SSE stream and reaper threads they leave behind finish whenever they finish, so whether the count holds still depends on the test order and on the scheduler rather than on the behavior under test. The test itself is asking the right question. `setup` builds its transport with `listen_keepalive_interval: nil` precisely so opening a listen stream spawns no timer, and the point is to hold that guarantee. Only the measurement is wrong, so this compares the set of threads instead: a thread that disappears no longer registers, and a thread that appears still does. Minitest runs these tests serially and `open_listen_stream` drives the SSE body inline rather than on a thread, so a keepalive timer is the only thread that can appear across the two samples. No library code changes. ## How Has This Been Tested? The rewritten assertion was checked for sensitivity before being kept: a temporary probe pointed `open_listen_stream` at a transport built with `listen_keepalive_interval: 15` and asserted the set difference was *not* empty. It passed, confirming the assertion still catches a spawned keepalive thread rather than merely never failing. The probe was removed afterwards. The failure did not reproduce locally beforehand, across six seeds of the file on its own and six full-suite runs, which matches a timing-sensitive flake that needs a slower machine. ## Breaking Changes None.
1 parent 6b1704f commit 09c3103

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

‎test/mcp/server/transports/streamable_http_transport_test.rb‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6307,10 +6307,13 @@ def string
63076307
end
63086308

63096309
test "listen keepalive is not started when the interval is nil" do
6310-
before = Thread.list.size
6310+
# The set of threads, not their count: `Thread.list` is process-wide, so a thread another test left running
6311+
# that finishes between the two samples moves the count in the direction this assertion does not care about.
6312+
# Only a thread that appears is evidence of a keepalive timer.
6313+
before = Thread.list
63116314
open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true })
63126315

6313-
assert_equal before, Thread.list.size, "a nil interval must not spawn a keepalive thread"
6316+
assert_empty(Thread.list - before, "a nil interval must not spawn a keepalive thread")
63146317
end
63156318

63166319
test "listen_keepalive_interval rejects a non-positive value" do

0 commit comments

Comments
 (0)