7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
Reviewed-by: alanb, chegar
--- 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();
}
}