0

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?

Shivani S
  • 17
  • 3
  • 1
    I can think of a couple of possibilities. (1) The channel was the result of `accept()` and you got mixed up. (2) You called `finishConnect()` twice somehow. Note that under the hood, `isConnectable()` and `isWritable()` map to the same bit. – user207421 May 12 '23 at 00:23

0 Answers0