--- old/src/windows/classes/sun/nio/ch/PipeImpl.java 2012-12-10 14:10:44.960252469 -0800 +++ new/src/windows/classes/sun/nio/ch/PipeImpl.java 2012-12-10 14:10:44.804252474 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2012, 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 @@ -81,37 +81,60 @@ SocketChannel sc1 = null; SocketChannel sc2 = null; + // loopback address + InetAddress lb = InetAddress.getByName("127.0.0.1"); + assert(lb.isLoopbackAddress()); + InetSocketAddress sa = null; + boolean interrupted = false; + try { - // loopback address - InetAddress lb = InetAddress.getByName("127.0.0.1"); - assert(lb.isLoopbackAddress()); - - // bind ServerSocketChannel to a port on the loopback address - ssc = ServerSocketChannel.open(); - ssc.socket().bind(new InetSocketAddress(lb, 0)); - - // Establish connection (assumes connections are eagerly - // accepted) - InetSocketAddress sa - = new InetSocketAddress(lb, ssc.socket().getLocalPort()); - sc1 = SocketChannel.open(sa); - - ByteBuffer bb = ByteBuffer.allocate(8); - long secret = rnd.nextLong(); - bb.putLong(secret).flip(); - sc1.write(bb); - - // Get a connection and verify it is legitimate - for (;;) { - sc2 = ssc.accept(); - bb.clear(); - sc2.read(bb); - bb.rewind(); - if (bb.getLong() == secret) - break; - sc2.close(); + for(;;) { + // bind ServerSocketChannel to a port on the loopback + // address + if (ssc == null || !ssc.isOpen()) { + ssc = ServerSocketChannel.open(); + ssc.socket().bind(new InetSocketAddress(lb, 0)); + sa = new InetSocketAddress(lb, + ssc.socket().getLocalPort()); + } + assert(sa != null); + + // Establish connection (assumes connections are eagerly + // accepted) + try { + sc1 = SocketChannel.open(sa); + } catch (ClosedByInterruptException cie) { + interrupted = Thread.interrupted(); + } + + if (sc1 != null && sc1.isConnected()) { + ByteBuffer bb = ByteBuffer.allocate(8); + long secret = rnd.nextLong(); + bb.putLong(secret).flip(); + sc1.write(bb); + + // Get a connection and verify it is legitimate + try { + sc2 = ssc.accept(); + } catch (ClosedByInterruptException cie) { + interrupted = Thread.interrupted(); + } + + if (sc2 != null && sc2.isConnected()) { + bb.clear(); + sc2.read(bb); + bb.rewind(); + if (bb.getLong() == secret) + break; + sc2.close(); + } + sc1.close(); + } } + if (interrupted) + Thread.currentThread().interrupt(); + // Create source and sink channels source = new SourceChannelImpl(sp, sc1); sink = new SinkChannelImpl(sp, sc2); --- /dev/null 2012-12-10 09:45:43.884722293 -0800 +++ new/test/java/nio/channels/Selector/SelectorInterrupt.java 2012-12-10 14:10:45.256252461 -0800 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2012, 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. + */ + +/* @test + * @bug 8002306 + * @summary Ensure that a Selector can open even if the current thread is interrupted. + * @author Dan Xu + * @library .. + * @build SelectorInterrupt + * @run main/othervm SelectorInterrupt + */ + +import java.io.IOException; +import java.nio.channels.Selector; + + +public class SelectorInterrupt { + + public static void main(String... args) throws IOException { + Thread.currentThread().interrupt(); + Selector.open(); + } +}