提交 da4da995 编写于 作者: C chegar

6994079: PlainSocketImpl should close the socket if it fails

Reviewed-by: alanb
上级 95647ee1
...@@ -28,9 +28,7 @@ package java.net; ...@@ -28,9 +28,7 @@ package java.net;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.InterruptedIOException;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.ByteArrayOutputStream;
import sun.net.ConnectionResetException; import sun.net.ConnectionResetException;
import sun.net.NetHooks; import sun.net.NetHooks;
...@@ -58,7 +56,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl ...@@ -58,7 +56,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
protected int fdUseCount = 0; protected int fdUseCount = 0;
/* lock when increment/decrementing fdUseCount */ /* lock when increment/decrementing fdUseCount */
protected Object fdLock = new Object(); protected final Object fdLock = new Object();
/* indicates a close is pending on the file descriptor */ /* indicates a close is pending on the file descriptor */
protected boolean closePending = false; protected boolean closePending = false;
...@@ -68,7 +66,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl ...@@ -68,7 +66,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
private int CONNECTION_RESET_PENDING = 1; private int CONNECTION_RESET_PENDING = 1;
private int CONNECTION_RESET = 2; private int CONNECTION_RESET = 2;
private int resetState; private int resetState;
private Object resetLock = new Object(); private final Object resetLock = new Object();
/** /**
* Load net library into runtime. * Load net library into runtime.
...@@ -100,25 +98,24 @@ abstract class AbstractPlainSocketImpl extends SocketImpl ...@@ -100,25 +98,24 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
protected void connect(String host, int port) protected void connect(String host, int port)
throws UnknownHostException, IOException throws UnknownHostException, IOException
{ {
IOException pending = null; boolean connected = false;
try { try {
InetAddress address = InetAddress.getByName(host); InetAddress address = InetAddress.getByName(host);
this.port = port; this.port = port;
this.address = address; this.address = address;
try { connectToAddress(address, port, timeout);
connectToAddress(address, port, timeout); connected = true;
return; } finally {
} catch (IOException e) { if (!connected) {
pending = e; try {
close();
} catch (IOException ioe) {
/* Do nothing. If connect threw an exception then
it will be passed up the call stack */
}
} }
} catch (UnknownHostException e) {
pending = e;
} }
// everything failed
close();
throw pending;
} }
/** /**
...@@ -151,22 +148,29 @@ abstract class AbstractPlainSocketImpl extends SocketImpl ...@@ -151,22 +148,29 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
* SocketAddress subclass not supported by this socket * SocketAddress subclass not supported by this socket
* @since 1.4 * @since 1.4
*/ */
protected void connect(SocketAddress address, int timeout) throws IOException { protected void connect(SocketAddress address, int timeout)
if (address == null || !(address instanceof InetSocketAddress)) throws IOException {
throw new IllegalArgumentException("unsupported address type"); boolean connected = false;
InetSocketAddress addr = (InetSocketAddress) address;
if (addr.isUnresolved())
throw new UnknownHostException(addr.getHostName());
this.port = addr.getPort();
this.address = addr.getAddress();
try { try {
if (address == null || !(address instanceof InetSocketAddress))
throw new IllegalArgumentException("unsupported address type");
InetSocketAddress addr = (InetSocketAddress) address;
if (addr.isUnresolved())
throw new UnknownHostException(addr.getHostName());
this.port = addr.getPort();
this.address = addr.getAddress();
connectToAddress(this.address, port, timeout); connectToAddress(this.address, port, timeout);
return; connected = true;
} catch (IOException e) { } finally {
// everything failed if (!connected) {
close(); try {
throw e; close();
} catch (IOException ioe) {
/* Do nothing. If connect threw an exception then
it will be passed up the call stack */
}
}
} }
} }
...@@ -311,7 +315,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl ...@@ -311,7 +315,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
} }
} }
try { try {
FileDescriptor fd = acquireFD(); acquireFD();
try { try {
socketConnect(address, port, timeout); socketConnect(address, port, timeout);
/* socket may have been closed during poll/select */ /* socket may have been closed during poll/select */
...@@ -370,7 +374,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl ...@@ -370,7 +374,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
* @param s the connection * @param s the connection
*/ */
protected void accept(SocketImpl s) throws IOException { protected void accept(SocketImpl s) throws IOException {
FileDescriptor fd = acquireFD(); acquireFD();
try { try {
socketAccept(s); socketAccept(s);
} finally { } finally {
...@@ -562,7 +566,6 @@ abstract class AbstractPlainSocketImpl extends SocketImpl ...@@ -562,7 +566,6 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
close(); close();
} }
/* /*
* "Acquires" and returns the FileDescriptor for this impl * "Acquires" and returns the FileDescriptor for this impl
* *
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册