java.net.SocketException: socket is closed
We are having multiple incidences in firebase regarding to this issue:

this problem occur randomly in a POST method, which could be called periodically. This issue was already raised at https://github.com/square/okio/issues/702 in our project we are using okhttp 4.10.0 FYI. Retrofit version is 2.9.0
Is there anything regarding the okhttp builder that need to be done to avoid this? Is any server side configuration needed?
Thanks.
Is it happening in a way that is causing your app to crash without a possibility to catch and handle? If its crashing internally and not reaching you app I'd be very concerned.
Trying to judge the severity of this.
Without a repro, it will be tough to investigate without more info.
Generally an IOException is not something we can reliably avoid.
It might be being retried automatically for safe GET requests.
Hi @yschimke , thanks for your response. the issue is happening in a block of code like this while calling to a post service. The app is not crashing since is inside a try / catch block
return try {
remoteSource.postData(weighBill).let { <- something is wrong at this point since should be calling okhttp/ okio which may be causing the exception
......
}
} catch (e: Exception) { <- the exception is cached here,
...
}
where remoteSource call the service:
@Headers("Content-Type: application/json")
@POST("/weighbill/{dairyId}/weighbills")
suspend fun postStartWeight(
@Path("dairyId") dairyId: Int,
@Body weighBill: WeighBill
): Response<WeighBillResponse>
My base url looks like https://mybaseurl/weighbill/20/weighbills
I'm not sure there is something to fix here. IOExceptions happen. And retrying in your app code or an interceptor might be the right thing to do when OkHttp can't safely do it for you.
@yschimke this is an issue I actually don't see in other projects where we use also retrofit - okhttp - okio, which is also widely use. Just trying to figure what could be causing it.
Sorry, there isn't enough here to go on. This is quite possible in normal conditions. Get requests may be automatically and safely retried. But you'll need to either implement in your app or a custom application intereceptor.
I encountered this issue a few months ago and conducted some online experiments. I discovered that our request protocol supports both HTTP/1.1 and HTTP/2.0. To address this, I created two instances of the OkHttpClient, each configured for a specific protocol. One instance was set for HTTP/1.1, and the other for HTTP/2.0. By separating requests based on their protocol requirements, I resolved the problem.
However, despite these measures, I was unable to pinpoint the issue within the code itself.
the issue still exist, I suspect during async call in okhttp, part of the request send after connection closed, so increasing write time out didn't help, I don't have ok environment to test out, but it is bug need to investigate and fix, call trace paste for you, I can provide my call to your api if needed, request failed -> java.net.SocketTimeoutException: timeout 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okio.SocketAsyncTimeout.newTimeoutException(JvmOkio.kt:147) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okio.AsyncTimeout.access$newTimeoutException(AsyncTimeout.kt:158) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:337) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:179) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:111) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:95) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205) 2024-05-08 08:45:47.646 16662-16705 System.out com.idigue.seashell I at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:537) 2024-05-08 08:45:47.647 16662-16705 System.out com.idigue.seashell I at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2024-05-08 08:45:47.647 16662-16705 System.out com.idigue.seashell I at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 2024-05-08 08:45:47.647 16662-16705 System.out com.idigue.seashell I at java.lang.Thread.run(Thread.java:920) 2024-05-08 08:45:47.647 16662-16705 System.out com.idigue.seashell I Caused by: java.net.SocketException: Socket closed 2024-05-08 08:45:47.647 16662-16705 System.out com.idigue.seashell I at java.net.SocketInputStream.read(SocketInputStream.java:209) 2024-05-08 08:45:47.647 16662-16705 System.out com.idigue.seashell I at java.net.SocketInputStream.read(SocketInputStream.java:144) 2024-05-08 08:45:47.647 16662-16705 System.out com.idigue.seashell I at okio.InputStreamSource.read(JvmOkio.kt:94) 2024-05-08 08:45:47.647 16662-16705 System.out com.idigue.seashell I at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:125)