Hi Anjali,
I saw this with older Java runtimes too. I think that this is somehow an error in the SSLEngine or NIO selection key processing, because there are no “holes” in the key registration, the case, that there is a “key is writable” selection but no socket (SocketEntry
) to actually write the data to is “logical” impossible.
The following code change in TLSTM.ServerThread.writeData
tries to detect the misbehaviour and remove the key selection to avoid the busy loop. The inserted else begins with the comment “This should never happen...
”:
@Override
protected TcpAddress writeData(SelectionKey sk, TcpAddress incomingAddress) {
SocketEntry entry = (SocketEntry) sk.attachment();
try {
SocketChannel sc = (SocketChannel) sk.channel();
incomingAddress = createIncomingAddress(sc.socket());
if ((entry != null) && (!entry.hasMessage())) {
synchronized (pending) {
pending.remove(entry);
entry.removeRegistration(selector, SelectionKey.OP_WRITE);
}
}
if (entry != null) {
writeMessage(entry, sc);
}
else { // This should never happen, but if it happens we need to cancel the key to avoid a busy loop!
sk.cancel();
logger.warn("Key was writable for incoming address "+incomingAddress+" but SocketEntry is null, key is canceled");
}
}
catch (IOException iox) {
logger.warn(iox);
// make sure channel is closed properly:
closeChannel(sk.channel());
removeSocketEntry(incomingAddress);
TransportStateEvent e =
new TransportStateEvent(TLSTM.this, incomingAddress,
TransportStateEvent.STATE_DISCONNECTED_REMOTELY, iox);
fireConnectionStateChanged(e);
}
return incomingAddress;
}
Please try this fix and let me know if the issue is solved then. The debug log just before the code is entering the busy loop would be of interest too. Maybe it provides a hint about the TLS session state when this issue happens. So far, I could not reproduce this with latest Java versions.
Which Java 8 version are you using exactly?
Best regards
Frank