Repository navigation
Stabilize flaky test: McpCompletionTests.testCompletionErrorOnMissingContext #902
Copy link
Copy link
Open
Labels
P3Nice to haves, rare edge casesNice to haves, rare edge casesneeds reproWe need a way to reproduce the problemWe need a way to reproduce the problemtestsAddresses tests onlyAddresses tests only
Description
McpCompletionTests.testCompletionErrorOnMissingContext is sometimes failing and recovers after simple build restart.
**Error:** Session not found: <sessionId>
Likely causes:
The server removes the session when SSE connection closes, but the client still has pending requests in flight. This is a race condition between:
- SSE connection closing → server removes session
- Client sending next request → "Session not found"
2026-02-09 23:31:09 [HttpClient-1-Worker-0] WARN i.m.c.t.HttpClientSseClientTransport - SSE stream observed an error
java.io.IOException: chunked transfer encoding, state: READING_LENGTH
at java.net.http/jdk.internal.net.http.common.Utils.wrapWithExtraDetail(Utils.java:375)
at java.net.http/jdk.internal.net.http.Http1Response$BodyReader.onReadError(Http1Response.java:660)
at java.net.http/jdk.internal.net.http.Http1AsyncReceiver.checkForErrors(Http1AsyncReceiver.java:302)
at java.net.http/jdk.internal.net.http.Http1AsyncReceiver.flush(Http1AsyncReceiver.java:268)
at java.net.http/jdk.internal.net.http.common.SequentialScheduler$LockingRestartableTask.run(SequentialScheduler.java:205)
at java.net.http/jdk.internal.net.http.common.SequentialScheduler$CompleteRestartableTask.run(SequentialScheduler.java:149)
at java.net.http/jdk.internal.net.http.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:230)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.io.EOFException: EOF reached while reading
at java.net.http/jdk.internal.net.http.Http1AsyncReceiver$Http1TubeSubscriber.onComplete(Http1AsyncReceiver.java:597)
at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$ReadSubscription.signalCompletion(SocketTube.java:648)
at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$InternalReadSubscription.read(SocketTube.java:853)
at java.net.http/jdk.internal.net.http.SocketTube$SocketFlowTask.run(SocketTube.java:181)
at java.net.http/jdk.internal.net.http.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:230)
at java.net.http/jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:303)
at java.net.http/jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:256)
at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$InternalReadSubscription.signalReadable(SocketTube.java:782)
at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$ReadEvent.signalEvent(SocketTube.java:965)
at java.net.http/jdk.internal.net.http.SocketTube$SocketFlowEvent.handle(SocketTube.java:253)
at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.handleEvent(HttpClientImpl.java:1402)
at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.lambda$run$3(HttpClientImpl.java:1347)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.run(HttpClientImpl.java:1347)
2026-02-09 23:31:09 [ForkJoinPool.commonPool-worker-1] ERROR i.m.c.t.HttpClientSseClientTransport - Error sending message: Sending message failed with a non-OK HTTP code: 404 - {"cause":null,"jsonRpcError":null,"localizedMessage":"Session not found: 7a9438d6-5965-456a-99af-26523f108d68","message":"Session not found: 7a9438d6-5965-456a-99af-26523f108d68","stackTrace":
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P3Nice to haves, rare edge casesNice to haves, rare edge casesneeds reproWe need a way to reproduce the problemWe need a way to reproduce the problemtestsAddresses tests onlyAddresses tests only
Activity
fix: modelcontextprotocol#902 stabilize testCompletionErrorOnMissingC…
rsmihir389 commentedon Apr 4, 2026
Hey — we have been looking at exactly this kind of failure pattern a lot lately. Race conditions between SSE teardown and in-flight client requests like this usually cost teams more than just the failed test — every rerun burns CI minutes while the underlying race stays unfixed.
A few things that tend to help with this specific pattern:
We built a tool that detects flaky tests like this and shows the dollar cost in CI time: kleore.com. Here is what a report looks like on a real repo: sample report.
60-second install as a GitHub App, free tier shows the cost. Might be useful for catching other intermittent failures in this repo before they pile up.
however-yir commentedon Apr 10, 2026
I investigated
McpCompletionTests.testCompletionErrorOnMissingContextflakiness.Likely cause:
Proposed stabilization:
await/latch/future completion)If maintainers agree, I can submit a focused PR with only test reliability changes.
Kehrlann commentedon Apr 10, 2026
@however-yir Before we submit a fix, I'd like to reliably reproduce the issue.
If this is a race condition, it should be possible to add an artificial delay somewhere to make the test fail consistently. We wouldn't commit that delay, just demonstrate the issue. Then we can think of a fix.
ftr there is some more discussions happening here in #903
ashakirin commentedon Apr 10, 2026
@however-yir I currently working to reproduce it reliably in separate test, will share the branch - fill free to contribute
fix: modelcontextprotocol#902 stabilize testCompletionErrorOnMissingC…
chore: modelcontextprotocol#902 added header and author
ashakirin commentedon Apr 13, 2026
@however-yir, @Kehrlann: I added McpCompletionFlakyReproductionTest, likely reproducing the flaky situation happens on CI/CD into fix branch. Could you please have a look?
Artificial connection break in lines 85-90 causes:
Would you like to take a look?
Kehrlann commentedon Apr 14, 2026
Spoke with @ashakirin today. It's unclear which test actually flakes mot often.
Let's start tracking the flakes in this particular issue until we see a pattern emerging.
Example: https://github.com/modelcontextprotocol/java-sdk/actions/runs/24340206510/job/71066978434#step:4:50240
Stacktrace:
Kehrlann commentedon Apr 30, 2026
Got the same error on
testCompletionErrorOnMissingContextas the original: https://github.com/modelcontextprotocol/java-sdk/actions/runs/25169961716/job/73786301955?pr=943ashakirin commentedon May 1, 2026
Error is slightly different:
Old one:
11:10:55.445 [ForkJoinPool.commonPool-worker-2] ERROR reactor.core.publisher.Operators -- Operator called default onErrorDropped reactor.core.Exceptions$ErrorCallbackNotImplemented: java.io.IOException: HTTP/1.1 header parser received no bytes Caused by: java.io.IOException: HTTP/1.1 header parser received no bytes ... Caused by: java.io.EOFException: EOF reached while reading ... ERROR io.modelcontextprotocol.server.transport.HttpServletStreamableServerTransportProvider -- Error handling message: Stream unavailable for session d54ef607-967f-4c42-b930-3842d36eaa87New one:
14:06:50.247 [boundedElastic-1] ERROR io.modelcontextprotocol.server.transport.HttpServletSseServerTransportProvider -- Failed to send message to session c2006b4a-f36f-413e-9bc0-41bea9d96f93: Client disconnected 14:06:50.248 [main] DEBUG io.modelcontextprotocol.client.LifecycleInitializer -- Joining previous initialization 14:06:50.250 [HttpClient-23-Worker-0] WARN io.modelcontextprotocol.client.transport.HttpClientSseClientTransport -- SSE stream observed an error java.io.IOException: chunked transfer encoding, state: READING_LENGTH ... Caused by: java.io.EOFException: EOF reached while reading ... "message":"Session not found: c2006b4a-f36f-413e-9bc0-41bea9d96f93","suppressed":[],"localizedMessage":"Session not found: c2006b4a-f36f-413e-9bc0-41bea9d96f93"But likely caused by the same reason.
@Kehrlann: do you think it worth to investigate deeply?
Kehrlann commentedon May 4, 2026
It would be nice to have, but it's low priority on my own list.
Kehrlann commentedon May 4, 2026
Different flake in
HttpClientStreamableHttpVersionNegotiationIntegrationTests.usesLatestVersionashakirin commentedon May 22, 2026
@Kehrlann
Looked to that once more time.
Quite sure, that the problem is broken long term SSE connection (as far as it is long, so more chances to get it broken).
What happens:
I see three possible ways to act:
a) explicitly in code (like in my initial PR)
b) using surefire's rerunFailingTestsCount
c) custom JUnit extension
a) Transport-level reconnect
b) Client-level reconnect:
WDYT?
Kehrlann commentedon Jun 16, 2026
We have much less errors since #995 was merged, which fixed some client-side error handling. Let's see if we have more flakes going forward.
Kehrlann commentedon Jun 23, 2026
New flake for
HttpServletStreamableIntegrationTests.testRootsSuccessSource: https://github.com/modelcontextprotocol/java-sdk/actions/runs/28031127085/job/82971782971
Edit: another instance - https://github.com/modelcontextprotocol/java-sdk/actions/runs/28098407134/job/83193642245
Stack trace:
Kehrlann commentedon Jun 24, 2026
Flake on the usual
McpCompletionTests.testCompletionErrorOnMissingContext:https://github.com/modelcontextprotocol/java-sdk/actions/runs/28091914820/job/83171754206
But this time it's a timeout: