Closed bashopman closed 5 years ago
Seems related to #5542 .
Is it possible to provide a timeline to get this fixed? The answer on Stack Overflow suggested it could be done on short notice. Thanks.
This project is maintained by volunteer effort. If you need a fix on some timeframe it's best to pursue one yourself and contribute a PR.
Try shutting down the dispatcher's executor service?
I'm sorry. Didn't mean to be rude. I guess I misunderstood the answer on Stack Overflow. I'll try to create a fix and provide a pull request taking swankjesse's suggestion into account.
Using the before mentioned versions of OkHttp and OkHttp-sse and swankjesses suggestion regarding shutting down the executor service, I was able to properly shut down the application by calling:
eventSource.cancel();
client.dispatcher().executorService().shutdown();
I considered suggesting eventSource.cancel()
to shut down the executor server internally, but that might cause unexpected side effects when a client is used that is also used for non-sse activities.
So, the only suggestion I could still give, is to add this to the user documentation for the okhttp-sse module.
Good luck with your nice library, I really enjoy using it.
You saved my life 🙏. Thanks
This bug report results from this question on Stack Overflow.
Problem:
I'm trying to create a client that can receive events from a server using SSE (server-sent events) and I'm using OkHttp3 to do that, combined with the OkHttp3-sse library (com.squareup.okhttp3:okhttp:4.1.0 & com.squareup.okhttp3:okhttp-sse:4.1.0). I am able to connect to the server and receive the events just fine, but at some point in time, I want to disconnect the client and shutdown my application. That is were the problem is. The connection gets closed, but the application is not shutting down, probably due to the
ConnectionPool
not closing.Attempted:
As there is hardly any documentation on the use of the OkHttp3-sse library, I have tried to reverse engineer from the code what to do. I have tried to:
RealEventSource.cancel()
which is disconnecting, but is hanging on a threadOkHttpClient.dispatcher().cancelAll()
which is also disconnecting, but remains hanging on a threadResponse.close()
which is passed toEventSourceListener.onOpen()
Finding:
As an alternative, I have looked at https://github.com/heremaps/oksse which has the
RealServerSentEvent.close()
call which is doing what I expect. It closes the connection and stops all threads allowing the application to shutdown completely. I have looked at the implementation of theRealServerSentEvent
class to see how the implementation differs fromRealEventSource
. I think the difference is in the conditions of the read loop:RealEventSource
line https://github.com/square/okhttp/blob/master/okhttp-sse/src/main/java/okhttp3/internal/sse/RealEventSource.kt#L75:compares to
RealServerSentEvent
line https://github.com/heremaps/oksse/blob/master/src/main/java/com/here/oksse/RealServerSentEvent.java#L94:The
OkSSE
implementation includes the condition if thecall.isCanceled()
in the loop, where as theOkHttp3-sse
does not. I suspect this is causingOkHttp3-sse
not to exit, but I might be mistaken.Test:
Code:
Resulting log:
Note that the connection is cancelled at 5:42:40PM, while the application exits at 5:43:40PM after some timeout.