disabling retryOnConnectionFailure causes java.io.EOFException: \n not found: limit=0 content=…
https://github.com/spring-projects/spring-ai/blame/c52f675445cb563a9f37c54f3489e3a6e049db97/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/http/okhttp/SpringAiOpenAiHttpClient.java#L516
Every time when I make several requests to llama.cpp server, I catch exception on second/third request
java.util.concurrent.CompletionException: com.openai.errors.OpenAIIoException: Request failed
at java.base/java.util.concurrent.CompletableFuture.wrapInCompletionException(CompletableFuture.java:323) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:359) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:364) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:957) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:932) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:531) ~[na:na]
at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2221) ~[na:na]
at org.springframework.ai.openai.http.okhttp.SpringAiOpenAiHttpClient$1.onFailure(SpringAiOpenAiHttpClient.java:138) ~[spring-ai-openai-2.0.0-RC1.jar!/:2.0.0-RC1]
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:525) ~[okhttp-4.12.0.jar!/:na]
at io.micrometer.context.ContextSnapshot.lambda$wrap$0(ContextSnapshot.java:91) ~[context-propagation-1.2.1.jar!/:1.2.1]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1090) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:614) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:1474) ~[na:na]
Caused by: com.openai.errors.OpenAIIoException: Request failed
... 6 common frames omitted
Caused by: java.io.IOException: unexpected end of stream on http://llama_server:8080/...
at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:210) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:110) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:93) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.12.0.jar!/:na]
at io.micrometer.core.instrument.binder.okhttp3.OkHttpObservationInterceptor.intercept(OkHttpObservationInterceptor.java:97) ~[micrometer-core-1.16.4.jar!/:1.16.4]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517) ~[okhttp-4.12.0.jar!/:na]
... 4 common frames omitted
Caused by: java.io.EOFException: \n not found: limit=0 content=…
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:335) ~[okio-jvm-3.6.0.jar!/:na]
at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29) ~[okhttp-4.12.0.jar!/:na]
at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:180) ~[okhttp-4.12.0.jar!/:na]
... 19 common frames omitted
Thanks for reporting this.
From the stack trace, this looks related to OkHttp connection reuse against an OpenAI-compatible llama.cpp server. Spring AI currently configures the OpenAI OkHttp client with retryOnConnectionFailure(false) to avoid double retries, since the OpenAI SDK retrying client owns retry behavior.
I can help investigate this further, but I think the preferred fix needs maintainer direction first. Possible directions could be:
- make
retryOnConnectionFailureconfigurable, - keep it disabled by default but document/work around OpenAI-compatible servers that close reused connections,
- or revisit whether disabling OkHttp connection-failure retry is still the best default.
I’m happy to try a small reproducer/test once the preferred direction is clear.
Opened #6323 with the fix.
Root cause: SpringAiOpenAiHttpClient was setting OkHttp's retryOnConnectionFailure(false) — based on a misreading that this would duplicate the OpenAI SDK's RetryingHttpClient. It doesn't: OkHttp's flag handles transparent recovery from broken/stale pooled connections, while the SDK retries HTTP status codes with backoff. They operate at different layers.
With OkHttp's recovery disabled, servers that aggressively close idle keep-alive connections (like llama.cpp here) leave a half-closed connection in the pool; the next request reuses it and fails with the EOFException: \n not found: limit=0 / unexpected end of stream you saw.
The PR re-enables OkHttp's default (retryOnConnectionFailure(true)) and adds a regression test using MockWebServer with SocketPolicy.DISCONNECT_AT_END — verified to fail with the exact same error if the flag is reverted.