Socket SO TIMEOUT: Difference between revisions
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | |||
* https://docs.oracle.com/javase/8/docs/api/java/net/SocketOptions.html | |||
=Internal= | =Internal= | ||
* [[Java_Networking#DatagramSocket_SO_TIMEOUT|DatagramSocket SO_TIMEOUT]] | * [[Java_Networking#DatagramSocket_SO_TIMEOUT|DatagramSocket SO_TIMEOUT]] | ||
* [[Java_Networking#Socket_SO_TIMEOUT|TCP Socket SO_TIMEOUT]] | * [[Java_Networking#Socket_SO_TIMEOUT|TCP Socket SO_TIMEOUT]] | ||
* [[Tomcat_Configuration#soTimeout|Tomcat Connector soTimeout]] | |||
=Overview= | =Overview= | ||
Line 49: | Line 54: | ||
=Subjects= | =Subjects= | ||
* [[ | * [[HttpComponents_HttpClient#Setting_Socket_SO_TIMEOUT|Setting socket SO_TIMEOUT with Apache HttpClient]] |
Latest revision as of 18:35, 10 May 2017
External
Internal
Overview
SO_TIMEOUT defines the timeout for waiting for data - the a maximum period inactivity between two consecutive data packets. It is specified in milliseconds. A timeout value of zero is interpreted as an infinite timeout.
At java.net API level, it can be set on a socket with setSoTimeout(int timeout) throws SocketException and read with getSoTimeout(). This method call enables/disables SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.
SO_TIMEOUT and Writing
SO_TIMEOUT does not help when the socket is writing. I captured the following thread dump where soTimeout() was set to 20 seconds, but the client got stuck for about 17 minutes. Stack trace below. The explanation is here: [Java Socket#HangInSocketWriteCall].
"CLD Runner 0 Thread" prio=10 tid=0x00007f5f5c3c1800 nid=0x349f runnable [0x00007f5f5480e000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.FileDispatcherImpl.write0(Native Method) at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47) at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:94) at sun.nio.ch.IOUtil.write(IOUtil.java:65) at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:466) - locked <0x00000000fd9f3a00> (a java.lang.Object) at java.nio.channels.Channels.writeFullyImpl(Channels.java:78) at java.nio.channels.Channels.writeFully(Channels.java:98) - locked <0x00000000fd9f39b8> (a java.lang.Object) at java.nio.channels.Channels.access$000(Channels.java:61) at java.nio.channels.Channels$1.write(Channels.java:174) - locked <0x00000000fd9f3b08> (a java.nio.channels.Channels$1) at java.io.BufferedOutputStream.write(BufferedOutputStream.java:122) - locked <0x00000000fd9f3af0> (a java.io.BufferedOutputStream) at java.io.FilterOutputStream.write(FilterOutputStream.java:97) at org.infinispan.client.hotrod.impl.transport.tcp.TcpTransport.writeBytes(TcpTransport.java:134) at org.infinispan.client.hotrod.impl.transport.AbstractTransport.writeArray(AbstractTransport.java:98) at org.infinispan.client.hotrod.impl.operations.AbstractKeyValueOperation.sendPutOperation(AbstractKeyValueOperation.java:44) at org.infinispan.client.hotrod.impl.operations.PutOperation.executeOperation(PutOperation.java:30) at org.infinispan.client.hotrod.impl.operations.PutOperation.executeOperation(PutOperation.java:19) at org.infinispan.client.hotrod.impl.operations.RetryOnFailureOperation.execute(RetryOnFailureOperation.java:49) at org.infinispan.client.hotrod.impl.RemoteCacheImpl.put(RemoteCacheImpl.java:237) at org.infinispan.client.hotrod.impl.RemoteCacheSupport.put(RemoteCacheSupport.java:79) at com.novaordis.cld.service.infinispan.InfinispanService.set(InfinispanService.java:62) at com.novaordis.cld.operations.Write.perform(Write.java:46) at com.novaordis.cld.SingleThreadedRunner.loopUntilStoppedOrOutOfOperations(SingleThreadedRunner.java:166) at com.novaordis.cld.SingleThreadedRunner.run(SingleThreadedRunner.java:75) at java.lang.Thread.run(Thread.java:724)