[#2104] Make sure we only act on the SelectionKey if it is valid
Merge remote-tracking branch: upstream/master