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();
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!