java.net.SocketException: socket is closed
this problem occur randomly in a POST method, which could be called periodically.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)
Also posted in https://github.com/square/okhttp/issues/7794#issuecomment-2100993925
If you can reproduce it against a private server, then you could post code for a repro, and privately message the actual host so we can investigate. Without that, this isn't actionable, and not clear it's not a problem with your client or server.
I am looking at the problem right now, definitely reproducible, will create GitHub separate repo to show the call, my postman call to the same server do not have the error
have created a github repo and reproduced the error, https://github.com/jessicakfl/purplerain stack trace here,
Installing profile for com.idigue.purplerain 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I request failed -> java.net.SocketTimeoutException: timeout 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okio.SocketAsyncTimeout.newTimeoutException(JvmOkio.kt:147) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okio.AsyncTimeout.access$newTimeoutException(AsyncTimeout.kt:158) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:337) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:179) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:111) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:95) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:537) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at java.lang.Thread.run(Thread.java:920) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I Caused by: java.net.SocketException: Socket closed 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at java.net.SocketInputStream.read(SocketInputStream.java:209) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at java.net.SocketInputStream.read(SocketInputStream.java:144) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okio.InputStreamSource.read(JvmOkio.kt:94) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:125) 2024-05-08 16:35:16.114 19514-19541 System.out com.idigue.purplerain I ... 20 more
the error not happen every time, the first time succeeded, log here,
2024-05-08 16:19:25.414 19204-19204 System.out com.idigue.purplerain I kotlin.Unit 2024-05-08 16:19:26.494 19204-19241 ProfileInstaller com.idigue.purplerain D Installing profile for com.idigue.purplerain 2024-05-08 16:19:28.217 19204-19234 System.out com.idigue.purplerain I insert user called ->
seems like a threading issue,
the treading error when write called at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109), other than print out more debug log, please provide apis about the intercepter setup to bypass the error
i'am using implementation("com.squareup.okhttp3:okhttp-android:5.0.0-alpha.11"), okio.RealBufferedSource in the package seems wrong version, at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427) 2024-05-08 19:08:50.759 19817-21076 System.out com.idigue.seashell I at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320)
can't find at all
Thanks for the repro, I'll dig in on the weekend. I really appreciate the effort to make it
I realized ok is complicated and have extensive test coverage, would help with better documentation, might still not easy to reproduce on your end, will find out..
think I found the problems when looking at ok trace, still have questions though, 1, in implementation("com.squareup.okhttp3:okhttp-android:5.0.0-alpha.11"),okio.RealBufferedSource has wrong version in the package 2, ok throw timeout error and failed the write request when server take the request without response, have changed server side return response 200, then no more errors, not sure why failed the whole request and not consistent result in threadings, wonder if it's intentional for ok
That's great.
It sounds like OkHttp is doing the right thing, but sounds like you expect OkHttp to handle servers that don't return a response and just close the socket.
Some feedback on the repro, you might want to consider higher level libraries that build on OkHttp like retrofit or Ktor. they should simplify some of your code compared to the OkHttp callbacks.
That's great.
It sounds like OkHttp is doing the right thing, but sounds like you expect OkHttp to handle servers that don't return a response and just close the socket.
Some feedback on the repro, you might want to consider higher level libraries that build on OkHttp like retrofit or Ktor. they should simplify some of your code compared to the OkHttp callbacks.
it is write request, server throw no error, postman calls success though, watch dog from time to time timeout the same socket call not very stable product, wonder if you had a look at my repo, hope it's correct recommended ok api callbacks
I have the same problem, the problem occurs when sending large images.