WSS Connection Timeout Randomly

Hi,

I have integrated Infura for ETH deposits and Withdrawal functionality in a centralized exchange project. All functionalities are working fine, except the subscription, which is required to track incoming ETH deposits of the users.

Below is code snippet:

WebSocketService socketService = new WebSocketService("wss://rinkeby.infura.io/ws/v3/<myPID>", true);

public void start() throws ConnectException, OnErrorNotImplementedException {

    log.info("Started >>>>>>> ");
    socketService.connect();

    Web3j web3jSocket = Web3j.build(socketService);

    Disposable subscription=web3jSocket.transactionFlowable()
            .subscribe(tx ->
            {
                log.info("Tx Hash > " +tx.getHash());
                saveTxHash(tx.getHash());

            }, throwable -> {
                log.info("Message >>>> "+throwable.getMessage());
                    throwable.printStackTrace();
});

Problem is that, it randomly throws connection timeout exception. I am currently on a high-speed lease line so I am sure connectivity issue isn’t from my end.

I have been struggling from three straight nights on this part. Without this, exchange launch is impossible.

Below is the exception :

2127f43ec31e0eeab1b1cd8b6a061ac071d
2020-05-31 17:36:50.686 INFO 14044 — [ool-14-thread-2] c.c.e.c.e.services.SubscriptionService : Tx Hash > 0x9f13976bc3ec920084b75562f04ff3961f1e9ff9cecd6579d0e738fe17f32697
2020-05-31 17:36:51.996 INFO 14044 — [ool-14-thread-2] c.c.e.c.e.services.SubscriptionService : Tx Hash > 0x06314762d33a287b97446d8b8ffe22324a8ac88e8ca9734d33a835d891bf525a
2020-05-31 17:36:52.519 INFO 14044 — [ool-14-thread-2] c.c.e.c.e.services.SubscriptionService : Tx Hash > 0xe276b29e8b8fcfad3e12bf7f6ee351b0bd9e7c20d815552925118efd2593500c
2020-05-31 17:36:54.161 INFO 14044 — [ool-14-thread-2] c.c.e.c.e.services.SubscriptionService : Tx Hash > 0x6cdb62a39847f7c3ea7a4614a95eb13ca28fdfcd70d3107bc1e7a0c220f9991d
2020-05-31 17:36:54.658 INFO 14044 — [ool-14-thread-2] c.c.e.c.e.services.SubscriptionService : Tx Hash > 0x47b7ee687a21294a2b46c896e3480708abb915518bfa6d3cd3340ef1d9efa043
2020-05-31 17:37:05.129 INFO 14044 — [ool-14-thread-2] c.c.e.c.e.services.SubscriptionService : Message >>>> timeout
java.net.SocketTimeoutException: timeout
at okio.Okio$4.newTimeoutException(Okio.java:230)
at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:211)
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:75)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
at okhttp3.RealCall.execute(RealCall.java:69)
at org.web3j.protocol.http.HttpService.performIO(HttpService.java:160)
at org.web3j.protocol.Service.send(Service.java:42)
at org.web3j.protocol.core.Request.send(Request.java:81)
at com.coinrecoil.exchange.coin.ethereum.services.SubscriptionService.getTransactionByHash(SubscriptionService.java:100)
at com.coinrecoil.exchange.coin.ethereum.services.SubscriptionService.saveTxHash(SubscriptionService.java:87)
at com.coinrecoil.exchange.coin.ethereum.services.SubscriptionService.lambda$start$0(SubscriptionService.java:74)
at io.reactivex.internal.subscribers.LambdaSubscriber.onNext(LambdaSubscriber.java:65)
at io.reactivex.internal.operators.flowable.FlowableFlattenIterable$FlattenIterableSubscriber.drain(FlowableFlattenIterable.java:312)
at io.reactivex.internal.operators.flowable.FlowableFlattenIterable$FlattenIterableSubscriber.onNext(FlowableFlattenIterable.java:174)
at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.tryEmitScalar(FlowableFlatMap.java:234)
at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.onNext(FlowableFlatMap.java:152)
at io.reactivex.internal.operators.flowable.FlowableCreate$BufferAsyncEmitter.drain(FlowableCreate.java:547)
at io.reactivex.internal.operators.flowable.FlowableCreate$BufferAsyncEmitter.onNext(FlowableCreate.java:471)
at org.web3j.protocol.core.filters.BlockFilter.process(BlockFilter.java:42)
at org.web3j.protocol.core.filters.Filter.pollFilter(Filter.java:143)
at org.web3j.protocol.core.filters.Filter.lambda$run$0(Filter.java:92)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.read(SocketInputStream.java:204)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at okio.Okio$2.read(Okio.java:139)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
… 42 more

Can someone help ?

Thanks.

Hello @Kunal_Thakker, I’ve done some research into this and I’ll try to help out. After you build the web3j service, can you try

web3j.blockObservable(false).subscribe(block -> {
    System.out.println("NEW BLOCK -> " + block.getBlock().getNumber().intValue());
});

to see if you are able to get each new block as it comes in? Also, how long are you able to run your code without an exception? A minute? 10 minutes? Less?

Hi,
Thanks for the reply.

I have digged a little deeper and found one more clue. Whether it is blockflowable or transactionflowable, exception issue is there. But I do not get any exception if I run the service using IDE, (IntelliJ)

But if I compile it to jar using mvn clean install, and mvn clean package, and then run it, it throws random exception at random times. Sometimes I get connection time out, sometimes websocketnotconnected exception.

I have posted the issue with Web3J community as well, and also searched on Google. there are many people facing this issue when they have infura and Web3J combined.

Okay hmm. 2 things, can you add in reconnect logic so that if you get a time out or websocketnotconnected it automatically reconnects?

And can you try using HttpService instead of WebsocketService?

Pardon my ignorance, can you guide how to reconnect and subsribe ? I tried researching a lot about OnError() but there isn’t enough documentation on how to use it.

For HTTPService, here’s the exception:

Caused by: java.lang.UnsupportedOperationException: Service HttpService does not support subscriptions

Besides, I received the reply from Web3J team for my issue:

@kunalbarchha yes I have seen related issue before - we concluded it was to do with Infura issue. I am not sure how to solve it because I cannot replicate.”

Hello. Have you fixed this bug?

Hi @Danyliuk_Mykola, and welcome to the Infura community! We have recently updated our WSS service to notify our users when a connection drops. The best solution is to have a reconnection set into your code so if the connection does drop, it will reconnect automatically.

Hello, could you possibly point to documentation or example of how to do that with a Web3j subsciprion? I have been googling for days and can’t seem to figure it out.