8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed

Reviewed-by: dfuchs, chegar
This commit is contained in:
Alan Bateman 2019-11-03 14:07:43 +00:00
parent db4909bf99
commit fd077ea9ae
3 changed files with 47 additions and 13 deletions

View file

@ -332,8 +332,8 @@ class ServerSocketChannelImpl
n = Net.accept(fd, newfd, isaa);
}
} finally {
// restore socket to blocking mode
lockedConfigureBlocking(true);
// restore socket to blocking mode (if channel is open)
tryLockedConfigureBlocking(true);
}
} finally {
end(true, n > 0);
@ -376,7 +376,7 @@ class ServerSocketChannelImpl
}
/**
* Adjust the blocking mode while holding acceptLock.
* Adjust the blocking. acceptLock must already be held.
*/
private void lockedConfigureBlocking(boolean block) throws IOException {
assert acceptLock.isHeldByCurrentThread();
@ -386,6 +386,25 @@ class ServerSocketChannelImpl
}
}
/**
* Adjusts the blocking mode if the channel is open. acceptLock must already
* be held.
*
* @return {@code true} if the blocking mode was adjusted, {@code false} if
* the blocking mode was not adjusted because the channel is closed
*/
private boolean tryLockedConfigureBlocking(boolean block) throws IOException {
assert acceptLock.isHeldByCurrentThread();
synchronized (stateLock) {
if (isOpen()) {
IOUtil.configureBlocking(fd, block);
return true;
} else {
return false;
}
}
}
/**
* Closes the socket if there are no accept in progress and the channel is
* not registered with a Selector.