Simple open socket hanging very infrequently
I have a rather simple piece of code that is hanging in java. The hang is VERY infrequently. Maybe on the order of once every 1000 executions. Running it in a loop to a device doesn't seem to reproduce the problem.
long timeout = 10000;
long endTime = System.currentTimeMillis() + timeout + 5000;
Socket pingSocket = null;
String host = "host";
String port = "22";
do {
try {
pingSocket = new Socket();
pingSocket.bind(null);
pingSocket.connect(new InetSocketAddress(host, port), 5000);
if (pingSocket.isConnected()) {
pingSocket.close();
开发者_StackOverflow社区 return true;
}
pingSocket.close();
}
catch (UnknownHostException e) {
throw e;
}
catch (IOException e) {
// All other errors are subclassed from IOException, and i want
// to ignore till after my spin period.
}
try {
Thread.sleep(SPIN_SLEEP_DELAY);
}
catch (InterruptedException e) {
return false;
}
} while (System.currentTimeMillis() <= endTime);
Since it happens so infrequently in production it's been hard to narrow down what caused the problem. I'm currently in the process of instrumenting the code so that next release of our product will have more information when this happens, but I thought I would ask if anyone has seen just a simple bind/connect/isConnected/close hang before?
Thanks!
Have you generated a Java thread dump during the hang? This will tell you where in your code the hang is occurring.
I've read that if a socket fails to connect then the calling code still has to close it before continuing (can't find the reference now). Otherwise resources are still consumed and future attempts to open sockets may hang.
So move the socket close to a finally block to ensure that your socket is closed even if it fails to connect.
精彩评论