Fix connection leaks after closing non-exhaustive EventSource.#91
Fix connection leaks after closing non-exhaustive EventSource.#91LinZong wants to merge 6 commits intolaunchdarkly:mainfrom
Conversation
|
Hello and thank you for bringing this to our attention and opening this PR. We will review when able to do so. Thank you! |
|
I am experiencing the same issue. Could you please review at your earliest convenience? |
@dlqkfzlr Maybe you can try okhttp-sse as an alternate choice. |
@LinZong I'm hesitant to use it because it's still in experimental support. Is okhttp-sse library more stable? |
@dlqkfzlr I had tried to use okhttp-sse on both client-side and server-side for about 1 year,so far so good. In fact, some famous LLM-related library like langchain4j also uses okhttp-sse to provide streaming response. |
Affected version
4.1.1
Problem
Okhttp connection won't closed properly after closing EventSource when SSE stream is not fully consumed.
Reproduce code
Reason
com.launchdarkly.eventsource.EventSource#closewill close underlyingokhttp3.Calland package-privatecom.launchdarkly.eventsource.HttpConnectStrategy.Client#httpClientif there is no specified httpClient from outside.Canceling a
okhttp3.Callwill:Condition for logging connection leak warning: connection.calls is not empty, but referring 'RealCall' was gone by GC, indicating RealCall was not detached with RealConnection properly.
The only way to initiative detach RealCall with RealConnection properly is calling
releaseConnectionNoEventsmethod, which entrance is inmessageDonemethod.Called from stream exhausted:
Called from closing Response.
But such method cannot be called either closing
Callor closing connection pool. ClosingCallis obvious, let's focus on closing conneciton pool.Possible Fixup
Close underlying
Responsein closeCurrentStream along with closingCallat reading thread.