I have the following Java code:
class TestClient {
...
SocketChannel channel = SocketChannel.open()
channel.configureBlocking(false)
SelectionKey key = channel.register(clientSelector, SelectionKey.OP_CONNECT)
channel.connect(serverSocketAddress)
...
In the Selector thread I have the standard while loop and inside that:
if(key.isConnectable()) {
SocketChannel channel = (SocketChannel) key.channel();
channel.finishConnect(); <-- This is throwing NoConnectionPendingException rarely
}
Error message is: NoConnectionPendingException null
And this is spinning - see approx 100 messages like this.
Now, why would this be happening? If the connect fails, why would key.isConnectable() be true? Is this a race condition whereby the connect has failed. I cannot see any failure in the connect - no exception is thrown. Could it be a race condition where the connect is successful, then key.isConnectable is true, and somewhere between that and finishConnect() the connection has failed?
What can I do about it?