lightsail-network / java-stellar-sdk

The Java Stellar SDK library provides APIs to build transactions and connect to Horizon and Soroban-RPC Server.
https://javadoc.io/doc/network.lightsail/stellar-sdk
Apache License 2.0
175 stars 160 forks source link

SSEStream unpredictably throws unwanted IOException("Canceled") #649

Closed lijamie98 closed 2 weeks ago

lijamie98 commented 2 weeks ago

I encountered an unpredictable and unwanted exception thrown from the SSEStream of the Java Horizon SDK (https://github.com/lightsail-network/java-stellar-sdk).

java.io.IOException: Canceled
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:72)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at org.stellar.sdk.requests.ClientIdentificationInterceptor.intercept(ClientIdentificationInterceptor.java:22)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    ...

Here is the root cause:

When reconnectTimeout expires, restart is called eventSource.cancel() is triggered.

A new StellarEventSourceListener is created for the new event source.

The previously canceled StellarEventSourceListener (the one that we don't track) can receive the IOException (Canceled) because the call was canceled in okhttp. The origin of the exception is here.

I suggest the SSEStream to not fire the onFailure() to the listener if the exception was targeting the canceled StellarEventSourceListener.

lijamie98 commented 2 weeks ago

Thank you so much for the fast fix. :-)