Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.net.SocketException: socket is closed #8400

Closed
jessicakfl opened this issue May 8, 2024 · 11 comments
Closed

java.net.SocketException: socket is closed #8400

jessicakfl opened this issue May 8, 2024 · 11 comments
Labels
bug Bug in existing code needs info More information needed from reporter stackoverflow Referred to stackoverflow

Comments

@jessicakfl
Copy link

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)

@jessicakfl jessicakfl added the bug Bug in existing code label May 8, 2024
@yschimke
Copy link
Collaborator

yschimke commented May 8, 2024

Also posted in #7794 (comment)

@yschimke
Copy link
Collaborator

yschimke commented May 8, 2024

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.

@yschimke yschimke added needs info More information needed from reporter stackoverflow Referred to stackoverflow labels May 8, 2024
@jessicakfl
Copy link
Author

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

@jessicakfl
Copy link
Author

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,

@jessicakfl
Copy link
Author

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

@jessicakfl
Copy link
Author

jessicakfl commented May 9, 2024

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

@yschimke
Copy link
Collaborator

yschimke commented May 9, 2024

Thanks for the repro, I'll dig in on the weekend. I really appreciate the effort to make it

@jessicakfl
Copy link
Author

jessicakfl commented May 10, 2024

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..

@jessicakfl
Copy link
Author

jessicakfl commented May 12, 2024

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

@yschimke
Copy link
Collaborator

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.

@jessicakfl
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code needs info More information needed from reporter stackoverflow Referred to stackoverflow
Projects
None yet
Development

No branches or pull requests

2 participants