A simple open outlet hangs very rarely

I have a pretty simple piece of code that hangs in java. Hanging VERY infrequently. Maybe in order once every 1000 executions. Running it in a loop on the device does not 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 this happens so rarely in production, it was difficult to narrow down the cause of the problem. I am in the process of programming the code now, so the next release of our product will have more information when this happens, but I thought I would ask if anyone had seen just a simple bind / connect / isConnected / close before?

Thank!

+3
source share
2 answers

Java ? , .

+3

, , ( ). - , .

, finally, , , .

+2

Source: https://habr.com/ru/post/1729622/


All Articles