8246707: (sc) SocketChannel.read/write throws AsynchronousCloseException on closed channel
This fix addresses an issue where an AsynchronousCloseException was being thrown instead of a ChannelClosedException when SocketChannel.write() is called on a closed SocketChannel. Reviewed-by: alanb, chegar, dfuchs
This commit is contained in:
parent
c540da3c4c
commit
831f23ee86
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -324,8 +324,7 @@ class SocketChannelImpl
|
||||
/**
|
||||
* Marks the beginning of a read operation that might block.
|
||||
*
|
||||
* @throws ClosedChannelException if the channel is closed
|
||||
* @throws NotYetConnectedException if the channel is not yet connected
|
||||
* @throws ClosedChannelException if blocking and the channel is closed
|
||||
*/
|
||||
private void beginRead(boolean blocking) throws ClosedChannelException {
|
||||
if (blocking) {
|
||||
@ -333,12 +332,10 @@ class SocketChannelImpl
|
||||
begin();
|
||||
|
||||
synchronized (stateLock) {
|
||||
ensureOpenAndConnected();
|
||||
ensureOpen();
|
||||
// record thread so it can be signalled if needed
|
||||
readerThread = NativeThread.current();
|
||||
}
|
||||
} else {
|
||||
ensureOpenAndConnected();
|
||||
}
|
||||
}
|
||||
|
||||
@ -373,6 +370,7 @@ class SocketChannelImpl
|
||||
|
||||
readLock.lock();
|
||||
try {
|
||||
ensureOpenAndConnected();
|
||||
boolean blocking = isBlocking();
|
||||
int n = 0;
|
||||
try {
|
||||
@ -415,6 +413,7 @@ class SocketChannelImpl
|
||||
|
||||
readLock.lock();
|
||||
try {
|
||||
ensureOpenAndConnected();
|
||||
boolean blocking = isBlocking();
|
||||
long n = 0;
|
||||
try {
|
||||
@ -452,8 +451,7 @@ class SocketChannelImpl
|
||||
/**
|
||||
* Marks the beginning of a write operation that might block.
|
||||
*
|
||||
* @throws ClosedChannelException if the channel is closed or output shutdown
|
||||
* @throws NotYetConnectedException if the channel is not yet connected
|
||||
* @throws ClosedChannelException if blocking and the channel is closed
|
||||
*/
|
||||
private void beginWrite(boolean blocking) throws ClosedChannelException {
|
||||
if (blocking) {
|
||||
@ -461,14 +459,12 @@ class SocketChannelImpl
|
||||
begin();
|
||||
|
||||
synchronized (stateLock) {
|
||||
ensureOpenAndConnected();
|
||||
ensureOpen();
|
||||
if (isOutputClosed)
|
||||
throw new ClosedChannelException();
|
||||
// record thread so it can be signalled if needed
|
||||
writerThread = NativeThread.current();
|
||||
}
|
||||
} else {
|
||||
ensureOpenAndConnected();
|
||||
}
|
||||
}
|
||||
|
||||
@ -496,9 +492,9 @@ class SocketChannelImpl
|
||||
@Override
|
||||
public int write(ByteBuffer buf) throws IOException {
|
||||
Objects.requireNonNull(buf);
|
||||
|
||||
writeLock.lock();
|
||||
try {
|
||||
ensureOpenAndConnected();
|
||||
boolean blocking = isBlocking();
|
||||
int n = 0;
|
||||
try {
|
||||
@ -529,6 +525,7 @@ class SocketChannelImpl
|
||||
|
||||
writeLock.lock();
|
||||
try {
|
||||
ensureOpenAndConnected();
|
||||
boolean blocking = isBlocking();
|
||||
long n = 0;
|
||||
try {
|
||||
@ -557,6 +554,7 @@ class SocketChannelImpl
|
||||
int sendOutOfBandData(byte b) throws IOException {
|
||||
writeLock.lock();
|
||||
try {
|
||||
ensureOpenAndConnected();
|
||||
boolean blocking = isBlocking();
|
||||
int n = 0;
|
||||
try {
|
||||
@ -1177,6 +1175,8 @@ class SocketChannelImpl
|
||||
|
||||
readLock.lock();
|
||||
try {
|
||||
ensureOpenAndConnected();
|
||||
|
||||
// check that channel is configured blocking
|
||||
if (!isBlocking())
|
||||
throw new IllegalBlockingModeException();
|
||||
@ -1254,6 +1254,8 @@ class SocketChannelImpl
|
||||
|
||||
writeLock.lock();
|
||||
try {
|
||||
ensureOpenAndConnected();
|
||||
|
||||
// check that channel is configured blocking
|
||||
if (!isBlocking())
|
||||
throw new IllegalBlockingModeException();
|
||||
@ -1261,8 +1263,8 @@ class SocketChannelImpl
|
||||
// loop until all bytes have been written
|
||||
int pos = off;
|
||||
int end = off + len;
|
||||
beginWrite(true);
|
||||
try {
|
||||
beginWrite(true);
|
||||
while (pos < end && isOpen()) {
|
||||
int size = end - pos;
|
||||
int n = tryWrite(b, pos, size);
|
||||
|
@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import org.testng.annotations.AfterTest;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8246707
|
||||
* @library /test/lib
|
||||
* @summary Reading or Writing to a closed SocketChannel should throw a ClosedChannelException
|
||||
* @run testng/othervm ReadWriteAfterClose
|
||||
*/
|
||||
|
||||
public class ReadWriteAfterClose {
|
||||
|
||||
private ServerSocketChannel listener;
|
||||
private SocketAddress saddr;
|
||||
private static final int bufCapacity = 4;
|
||||
private static final int bufArraySize = 4;
|
||||
private static final Class<ClosedChannelException> CCE = ClosedChannelException.class;
|
||||
|
||||
@BeforeTest
|
||||
public void setUp() throws IOException {
|
||||
listener = ServerSocketChannel.open();
|
||||
listener.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
|
||||
saddr = listener.getLocalAddress();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteAfterClose1() throws IOException {
|
||||
SocketChannel sc = SocketChannel.open(saddr);
|
||||
sc.close();
|
||||
ByteBuffer bufWrite = ByteBuffer.allocate(bufCapacity);
|
||||
Throwable ex = expectThrows(CCE, () -> sc.write(bufWrite));
|
||||
assertEquals(ex.getClass(), CCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteAfterClose2() throws IOException {
|
||||
SocketChannel sc = SocketChannel.open(saddr);
|
||||
sc.close();
|
||||
ByteBuffer[] bufArrayWrite = allocateBufArray();
|
||||
Throwable ex = expectThrows(CCE, () -> sc.write(bufArrayWrite));
|
||||
assertEquals(ex.getClass(), CCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteAfterClose3() throws IOException {
|
||||
SocketChannel sc = SocketChannel.open(saddr);
|
||||
sc.close();
|
||||
ByteBuffer[] bufArrayWrite = allocateBufArray();
|
||||
Throwable ex = expectThrows(CCE, () -> sc.write(bufArrayWrite, 0, bufArraySize));
|
||||
assertEquals(ex.getClass(), CCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadAfterClose1() throws IOException {
|
||||
SocketChannel sc = SocketChannel.open(saddr);
|
||||
sc.close();
|
||||
ByteBuffer dst = ByteBuffer.allocate(bufCapacity);
|
||||
Throwable ex = expectThrows(CCE, () -> sc.read(dst));
|
||||
assertEquals(ex.getClass(), CCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadAfterClose2() throws IOException {
|
||||
SocketChannel sc = SocketChannel.open(saddr);
|
||||
sc.close();
|
||||
ByteBuffer[] dstArray = allocateBufArray();
|
||||
Throwable ex = expectThrows(CCE, () -> sc.read(dstArray));
|
||||
assertEquals(ex.getClass(), CCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadAfterClose3() throws IOException {
|
||||
SocketChannel sc = SocketChannel.open(saddr);
|
||||
sc.close();
|
||||
ByteBuffer[] dstArray = allocateBufArray();
|
||||
Throwable ex = expectThrows(CCE, () -> sc.read(dstArray, 0, bufArraySize));
|
||||
assertEquals(ex.getClass(), CCE);
|
||||
}
|
||||
|
||||
public ByteBuffer[] allocateBufArray() {
|
||||
ByteBuffer[] bufArr = new ByteBuffer[bufArraySize];
|
||||
for (int i = 0; i < bufArraySize; i++)
|
||||
bufArr[i] = ByteBuffer.allocate(bufCapacity);
|
||||
return bufArr;
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
public void tearDown() throws IOException {
|
||||
listener.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user