Mercurial > hg > openjdk > jdk8u > jdk
changeset 14397:389dd8689b82
6878250: (so) IllegalBlockingModeException thrown when reading from a closed SocketChannel's InputStream
Reviewed-by: alanb, andrew
Contributed-by: songyaofei2@huawei.com
author | dongbohe |
---|---|
date | Tue, 02 Feb 2021 09:45:39 +0800 |
parents | 7c8bbbfe6acb |
children | c5e2385ff43d |
files | src/share/classes/sun/nio/ch/SocketAdaptor.java test/java/nio/channels/SocketChannel/AdaptSocketReadAfterClose.java |
diffstat | 2 files changed, 67 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/sun/nio/ch/SocketAdaptor.java Fri Jun 06 14:58:41 2014 +0400 +++ b/src/share/classes/sun/nio/ch/SocketAdaptor.java Tue Feb 02 09:45:39 2021 +0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2021, 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 @@ -198,6 +198,9 @@ throws IOException { synchronized (sc.blockingLock()) { + if (!sc.isOpen()) { + throw new ClosedChannelException(); + } if (!sc.isBlocking()) throw new IllegalBlockingModeException();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/nio/channels/SocketChannel/AdaptSocketReadAfterClose.java Tue Feb 02 09:45:39 2021 +0800 @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021, Huawei Technologies Co., Ltd. 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 java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; + +import org.testng.annotations.Test; +import org.testng.Assert; + +/* + * @test + * @bug 8260875 + * @summary Reading a closed SocketChannel should throw a ClosedChannelException + * @run testng AdaptSocketReadAfterClose + */ + +public class AdaptSocketReadAfterClose { + + @Test + public void testReadAfterClose() throws InterruptedException, IOException { + ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); + SocketAddress saddr = ssc.getLocalAddress(); + + SocketChannel channel = SocketChannel.open(saddr); + channel.configureBlocking(false); + Socket s = channel.socket(); + InputStream is = s.getInputStream(); + channel.close(); + + byte[] buf = new byte[10]; + Throwable ex = Assert.expectThrows(ClosedChannelException.class, () -> is.read(buf)); + Assert.assertEquals(ex.getClass(), ClosedChannelException.class); + ssc.close(); + } +}