mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
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
2 changed files with 142 additions and 13 deletions
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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.
|
* Marks the beginning of a read operation that might block.
|
||||||
*
|
*
|
||||||
* @throws ClosedChannelException if the channel is closed
|
* @throws ClosedChannelException if blocking and the channel is closed
|
||||||
* @throws NotYetConnectedException if the channel is not yet connected
|
|
||||||
*/
|
*/
|
||||||
private void beginRead(boolean blocking) throws ClosedChannelException {
|
private void beginRead(boolean blocking) throws ClosedChannelException {
|
||||||
if (blocking) {
|
if (blocking) {
|
||||||
|
@ -333,12 +332,10 @@ class SocketChannelImpl
|
||||||
begin();
|
begin();
|
||||||
|
|
||||||
synchronized (stateLock) {
|
synchronized (stateLock) {
|
||||||
ensureOpenAndConnected();
|
ensureOpen();
|
||||||
// record thread so it can be signalled if needed
|
// record thread so it can be signalled if needed
|
||||||
readerThread = NativeThread.current();
|
readerThread = NativeThread.current();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ensureOpenAndConnected();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -373,6 +370,7 @@ class SocketChannelImpl
|
||||||
|
|
||||||
readLock.lock();
|
readLock.lock();
|
||||||
try {
|
try {
|
||||||
|
ensureOpenAndConnected();
|
||||||
boolean blocking = isBlocking();
|
boolean blocking = isBlocking();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
try {
|
try {
|
||||||
|
@ -415,6 +413,7 @@ class SocketChannelImpl
|
||||||
|
|
||||||
readLock.lock();
|
readLock.lock();
|
||||||
try {
|
try {
|
||||||
|
ensureOpenAndConnected();
|
||||||
boolean blocking = isBlocking();
|
boolean blocking = isBlocking();
|
||||||
long n = 0;
|
long n = 0;
|
||||||
try {
|
try {
|
||||||
|
@ -452,8 +451,7 @@ class SocketChannelImpl
|
||||||
/**
|
/**
|
||||||
* Marks the beginning of a write operation that might block.
|
* Marks the beginning of a write operation that might block.
|
||||||
*
|
*
|
||||||
* @throws ClosedChannelException if the channel is closed or output shutdown
|
* @throws ClosedChannelException if blocking and the channel is closed
|
||||||
* @throws NotYetConnectedException if the channel is not yet connected
|
|
||||||
*/
|
*/
|
||||||
private void beginWrite(boolean blocking) throws ClosedChannelException {
|
private void beginWrite(boolean blocking) throws ClosedChannelException {
|
||||||
if (blocking) {
|
if (blocking) {
|
||||||
|
@ -461,14 +459,12 @@ class SocketChannelImpl
|
||||||
begin();
|
begin();
|
||||||
|
|
||||||
synchronized (stateLock) {
|
synchronized (stateLock) {
|
||||||
ensureOpenAndConnected();
|
ensureOpen();
|
||||||
if (isOutputClosed)
|
if (isOutputClosed)
|
||||||
throw new ClosedChannelException();
|
throw new ClosedChannelException();
|
||||||
// record thread so it can be signalled if needed
|
// record thread so it can be signalled if needed
|
||||||
writerThread = NativeThread.current();
|
writerThread = NativeThread.current();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ensureOpenAndConnected();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -496,9 +492,9 @@ class SocketChannelImpl
|
||||||
@Override
|
@Override
|
||||||
public int write(ByteBuffer buf) throws IOException {
|
public int write(ByteBuffer buf) throws IOException {
|
||||||
Objects.requireNonNull(buf);
|
Objects.requireNonNull(buf);
|
||||||
|
|
||||||
writeLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
|
ensureOpenAndConnected();
|
||||||
boolean blocking = isBlocking();
|
boolean blocking = isBlocking();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
try {
|
try {
|
||||||
|
@ -529,6 +525,7 @@ class SocketChannelImpl
|
||||||
|
|
||||||
writeLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
|
ensureOpenAndConnected();
|
||||||
boolean blocking = isBlocking();
|
boolean blocking = isBlocking();
|
||||||
long n = 0;
|
long n = 0;
|
||||||
try {
|
try {
|
||||||
|
@ -557,6 +554,7 @@ class SocketChannelImpl
|
||||||
int sendOutOfBandData(byte b) throws IOException {
|
int sendOutOfBandData(byte b) throws IOException {
|
||||||
writeLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
|
ensureOpenAndConnected();
|
||||||
boolean blocking = isBlocking();
|
boolean blocking = isBlocking();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
try {
|
try {
|
||||||
|
@ -1177,6 +1175,8 @@ class SocketChannelImpl
|
||||||
|
|
||||||
readLock.lock();
|
readLock.lock();
|
||||||
try {
|
try {
|
||||||
|
ensureOpenAndConnected();
|
||||||
|
|
||||||
// check that channel is configured blocking
|
// check that channel is configured blocking
|
||||||
if (!isBlocking())
|
if (!isBlocking())
|
||||||
throw new IllegalBlockingModeException();
|
throw new IllegalBlockingModeException();
|
||||||
|
@ -1254,6 +1254,8 @@ class SocketChannelImpl
|
||||||
|
|
||||||
writeLock.lock();
|
writeLock.lock();
|
||||||
try {
|
try {
|
||||||
|
ensureOpenAndConnected();
|
||||||
|
|
||||||
// check that channel is configured blocking
|
// check that channel is configured blocking
|
||||||
if (!isBlocking())
|
if (!isBlocking())
|
||||||
throw new IllegalBlockingModeException();
|
throw new IllegalBlockingModeException();
|
||||||
|
@ -1261,8 +1263,8 @@ class SocketChannelImpl
|
||||||
// loop until all bytes have been written
|
// loop until all bytes have been written
|
||||||
int pos = off;
|
int pos = off;
|
||||||
int end = off + len;
|
int end = off + len;
|
||||||
beginWrite(true);
|
|
||||||
try {
|
try {
|
||||||
|
beginWrite(true);
|
||||||
while (pos < end && isOpen()) {
|
while (pos < end && isOpen()) {
|
||||||
int size = end - pos;
|
int size = end - pos;
|
||||||
int n = tryWrite(b, pos, size);
|
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…
Add table
Add a link
Reference in a new issue