Socket SO TIMEOUT: Difference between revisions
(Created page with "=Internal= * DatagramSocket SO_TIMEOUT * TCP Socket SO_TIMEOUT =Overview= SO_TIMEOUT def...") |
|||
Line 8: | Line 8: | ||
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. | 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 | At <tt>java.net</tt> API level, it can be set on a socket with <tt>setSoTimeout(int timeout) throws SocketException</tt> and read with <tt>getSoTimeout()</tt>. This method call enables/disables SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a <tt>read()</tt> call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a <tt>java.net.SocketTimeoutException</tt> 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 and Writing= |
Revision as of 16:54, 20 March 2017
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)