7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
authormichaelm
Fri, 09 Sep 2011 14:04:44 +0100
changeset 10437 dfca69ed7f87
parent 10435 e9df9d264894
child 10438 9607eb55d49f
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception Reviewed-by: alanb, chegar
jdk/src/share/classes/java/net/DatagramSocket.java
jdk/src/share/classes/java/net/MulticastSocket.java
jdk/src/share/classes/java/net/Socket.java
--- a/jdk/src/share/classes/java/net/DatagramSocket.java	Thu Sep 08 09:04:28 2011 +0800
+++ b/jdk/src/share/classes/java/net/DatagramSocket.java	Fri Sep 09 14:04:44 2011 +0100
@@ -174,9 +174,7 @@
      * @see SecurityManager#checkListen
      */
     public DatagramSocket() throws SocketException {
-        // create a datagram socket.
-        createImpl();
-        bind(new InetSocketAddress(0));
+        this(new InetSocketAddress(0));
     }
 
     /**
@@ -221,7 +219,12 @@
         // create a datagram socket.
         createImpl();
         if (bindaddr != null) {
-            bind(bindaddr);
+            try {
+                bind(bindaddr);
+            } finally {
+                if (!isBound())
+                    close();
+            }
         }
     }
 
--- a/jdk/src/share/classes/java/net/MulticastSocket.java	Thu Sep 08 09:04:28 2011 +0800
+++ b/jdk/src/share/classes/java/net/MulticastSocket.java	Fri Sep 09 14:04:44 2011 +0100
@@ -162,7 +162,12 @@
         setReuseAddress(true);
 
         if (bindaddr != null) {
-            bind(bindaddr);
+            try {
+                bind(bindaddr);
+            } finally {
+                if (!isBound())
+                    close();
+            }
         }
     }
 
--- a/jdk/src/share/classes/java/net/Socket.java	Thu Sep 08 09:04:28 2011 +0800
+++ b/jdk/src/share/classes/java/net/Socket.java	Fri Sep 09 14:04:44 2011 +0100
@@ -425,6 +425,10 @@
         } catch (IOException e) {
             close();
             throw e;
+        } finally {
+            // if bind() or connect threw a runtime exception
+            if ((localAddr != null && !bound) || (address != null && !connected))
+                close();
         }
     }