What does web service do when client has socket timeout exceeded?

It should throw a broken pipe exception. Below shows an strace of a server socket when the client has timed out after not receiving a response in the time period it specified (setTimeout() method)…

13125 12:19:13.666786 <... poll resumed> ) = 1 ([{fd=7, revents=POLLIN}])
13125 12:19:13.666835 accept(7, {sa_family=AF_INET6, sin6_port=htons(21367), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 8
13125 12:19:13.666870 fcntl(8, F_GETFL) = 0x2 (flags O_RDWR)
13125 12:19:13.666890 fcntl(8, F_SETFL, O_RDWR) = 0
13125 12:19:13.667242 write(1, "got socket", 10) = 10
13125 12:19:13.667500 write(1, "\n", 1) = 1
13125 12:19:13.668865 recvfrom(8, "s", 1, 0, NULL, NULL) = 1
13125 12:19:13.694517 recvfrom(8, "\n", 1, 0, NULL, NULL) = 1
13125 12:19:15.695164 sendto(8, "sending back STEVE", 18, 0, NULL, 0) = 18
13125 12:19:15.695303 sendto(8, "\n", 1, 0, NULL, 0) = -1 EPIPE (Broken pipe)
13125 12:19:15.695332 --- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=13124, si_uid=8084694} ---
13125 12:19:15.695347 rt_sigreturn()    = -1 EPIPE (Broken pipe)
13125 12:19:15.696405 dup2(6, 8)        = 8
13125 12:19:15.696446 close(8)          = 0
13125 12:19:15.696493 poll([{fd=7, events=POLLIN|POLLERR}], 1, 4294967295 

A similar call from the client has the following system call trace output…

10767 11:52:50.806171 connect(7, {sa_family=AF_INET6, sin6_port=htons(5000), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = 0
10767 11:52:50.806691 getsockname(7,  
10767 11:52:50.806912 <... getsockname resumed> {sa_family=AF_INET6, sin6_port=htons(20311), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 0
10767 11:52:50.807171 write(1, "0", 1 
10767 11:52:50.807965 write(1, "\n", 1) = 1
10767 11:52:50.808115 write(1, "1000", 4) = 4
10767 11:52:50.808180 write(1, "\n", 1) = 1
10767 11:52:50.838359 write(1, "Fri Aug 04 11:52:50 EDT 2017", 28) = 28
10767 11:52:50.838496 write(1, "\n", 1) = 1
10767 11:52:50.838600 sendto(7, "steve", 5, 0, NULL, 0) = 5
10767 11:52:50.838729 sendto(7, "\n", 1, 0, NULL, 0) = 1
10767 11:52:50.838857 poll([{fd=7, events=POLLIN|POLLERR}], 1, 1000 
10767 11:52:51.839950 <... poll resumed> ) = 0 (Timeout)
10767 11:52:51.841524 write(2, "java.net.SocketTimeoutException: Read timed out", 47) = 47

It should be noted that for the server, even though EPIPE is clearly returned by the OS, java (1.8.0.101) will not necessarily raise any exception on this event. This is bad, as logging clients that have timed out or gone away for any reason can be invaluable when troubleshooting. I didn’t dig into the JDK source code, but it would be interesting to see why they ignore this signal.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.