8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
Summary: join the dispatcher thread in the ServerImpl.stop method to ensure Dispatcher is finished prior to exiting stop().
Reviewed-by: chegar
--- a/jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java Thu Nov 06 13:29:36 2014 -0800
+++ b/jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java Sat Nov 15 18:26:29 2014 +0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2014, 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
@@ -82,6 +82,7 @@
private Timer timer, timer1;
private Logger logger;
+ private Thread dispatcherThread;
ServerImpl (
HttpServer wrapper, String protocol, InetSocketAddress addr, int backlog
@@ -141,9 +142,9 @@
if (executor == null) {
executor = new DefaultExecutor();
}
- Thread t = new Thread (dispatcher);
+ dispatcherThread = new Thread (dispatcher);
started = true;
- t.start();
+ dispatcherThread.start();
}
public void setExecutor (Executor executor) {
@@ -205,6 +206,12 @@
if (timer1Enabled) {
timer1.cancel();
}
+ try {
+ dispatcherThread.join();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ logger.log(Level.FINER, "ServerImpl.stop: ", e);
+ }
}
Dispatcher dispatcher;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/com/sun/net/httpserver/SimpleHttpServerTest.java Sat Nov 15 18:26:29 2014 +0000
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2014 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 8015692
+ * @summary Test HttpServer instantiation, start, and stop repeated in a loop
+ * Testing for Bind exception on Windows
+ */
+
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+
+import com.sun.net.httpserver.HttpServer;
+
+
+public class SimpleHttpServerTest {
+
+ public static void main(String[] args) throws Exception {
+
+ System.out.println(System.getProperty("java.version"));
+ InetSocketAddress serverAddr = new InetSocketAddress(0);
+ HttpServer server = HttpServer.create(serverAddr, 0);
+ final int serverPort = server.getAddress().getPort();
+ server.start();
+ server.stop(0);
+ serverAddr = new InetSocketAddress(serverPort);
+ int exceptionCount = 0;
+ System.out.println("Using serverPort == " + serverPort);
+ for (int i = 0; i < 100; i++) {
+ try {
+ server = HttpServer.create(serverAddr, 0);
+ server.start();
+ server.stop(0);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ exceptionCount++;
+ }
+ }
+ if (exceptionCount > 0) {
+ throw new RuntimeException("Test Failed");
+ }
+ }
+}