kubernetes-client / java

Official Java client library for kubernetes
http://kubernetes.io/
Apache License 2.0
3.54k stars 1.88k forks source link

java.net.SocketException: Connection or outbound has been closed #3692

Open SamratDawn opened 4 days ago

SamratDawn commented 4 days ago

I am trying to create a exec request using this page as reference but sometimes when I am running the code I am getting this error-

java.net.SocketException: Connection or outbound has been closed at java.base/sun.security.ssl.SSLSocketOutputRecord.deliver(SSLSocketOutputRecord.java:291) at java.base/sun.security.ssl.SSLSocketImpl$AppOutputStream.write(SSLSocketImpl.java:1298) at okio.OutputStreamSink.write(JvmOkio.kt:56) at okio.AsyncTimeout$sink$1.write(AsyncTimeout.kt:102) at okio.RealBufferedSink.flush(RealBufferedSink.kt:268) at okhttp3.internal.ws.WebSocketWriter.writeControlFrame(WebSocketWriter.kt:142) at okhttp3.internal.ws.WebSocketWriter.writeClose(WebSocketWriter.kt:102) at okhttp3.internal.ws.RealWebSocket.writeOneFrame$okhttp(RealWebSocket.kt:533) at okhttp3.internal.ws.RealWebSocket$WriterTask.runOnce(RealWebSocket.kt:620) at okhttp3.internal.concurrent.TaskRunner.runTask(TaskRunner.kt:116) at okhttp3.internal.concurrent.TaskRunner.access$runTask(TaskRunner.kt:42) at okhttp3.internal.concurrent.TaskRunner$runnable$1.run(TaskRunner.kt:65) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:833)

I have tried extending the timeouts and separating the in, out, err streams but nothing works. I saw the issue 3385 but how do I fix the error in my code?

Version used - 21.0.0

brendandburns commented 4 days ago

As mentioned here: https://github.com/kubernetes-client/java/issues/3385#issuecomment-2096436807

You need to set the exception handler on the Exec class, you can then suppress the logging of the exception if you want to.

What is happening is that both sides of the connection are attempting to close the connection when the exec completes and there is a race. Sometimes the server side wins and the client gets this exception when trying to close the web socket, because the tcp socket is already closed.