AbstractPlainSocketImpl#close shouldn't set fd to null
pre-enso code expects fd to never be null. close() call should
only invalidate the fd object (sets to -1).
AbstractPlainSocketImpl#close is already invalidating the fd,
so I've updated the code to not alter the fd in close call
+ Updated every (fd != null) condition need to check if the
fd.valid()
Bug: 26470377
Change-Id: I69a2b9a9a5d7483952b3efe177bcd92e8f895bd1
diff --git a/luni/src/test/java/libcore/java/net/SocketTest.java b/luni/src/test/java/libcore/java/net/SocketTest.java
index 57facc5..f77faa9 100644
--- a/luni/src/test/java/libcore/java/net/SocketTest.java
+++ b/luni/src/test/java/libcore/java/net/SocketTest.java
@@ -390,7 +390,7 @@
}
- // b/25805791
+ // b/25805791 + b/26470377
public void testFileDescriptorStaysSame() throws Exception {
// SocketImplementation FileDescriptor object shouldn't change after calling
// bind (and many other methods).
@@ -408,8 +408,12 @@
assertSame(fd1, fd2);
int fd2Val = fd2.getInt$();
assertTrue(fd1Val != fd2Val); // The actual fd should be different
- }
+ s.close();
+ FileDescriptor fd3 = s.getFileDescriptor$();
+ assertSame(fd1, fd3);
+ assertFalse(fd3.valid());
+ }
static class MockServer {
private ExecutorService executor;
diff --git a/ojluni/src/main/java/java/net/AbstractPlainSocketImpl.java b/ojluni/src/main/java/java/net/AbstractPlainSocketImpl.java
index 920531a..746c19e 100755
--- a/ojluni/src/main/java/java/net/AbstractPlainSocketImpl.java
+++ b/ojluni/src/main/java/java/net/AbstractPlainSocketImpl.java
@@ -88,7 +88,6 @@
socketCreate(false);
} catch (IOException ioe) {
ResourceManager.afterUdpClose();
- fd = null;
throw ioe;
}
} else {
@@ -486,7 +485,7 @@
*/
protected void close() throws IOException {
synchronized(fdLock) {
- if (fd != null) {
+ if (fd != null && fd.valid()) {
if (!stream) {
ResourceManager.afterUdpClose();
}
@@ -495,17 +494,15 @@
}
closePending = true;
socketClose();
- fd = null;
return;
}
}
}
void reset() throws IOException {
- if (fd != null) {
+ if (fd != null && fd.valid()) {
socketClose();
}
- fd = null;
super.reset();
}
@@ -514,7 +511,7 @@
* Shutdown read-half of the socket connection;
*/
protected void shutdownInput() throws IOException {
- if (fd != null) {
+ if (fd != null && fd.valid()) {
socketShutdown(SHUT_RD);
if (socketInputStream != null) {
socketInputStream.setEOF(true);
@@ -527,7 +524,7 @@
* Shutdown write-half of the socket connection;
*/
protected void shutdownOutput() throws IOException {
- if (fd != null) {
+ if (fd != null && fd.valid()) {
socketShutdown(SHUT_WR);
shut_wr = true;
}
@@ -538,7 +535,7 @@
}
protected void sendUrgentData (int data) throws IOException {
- if (fd == null) {
+ if (fd == null || !fd.valid()) {
throw new IOException("Socket Closed");
}
socketSendUrgentData (data);
@@ -602,7 +599,7 @@
* close is in progress.
*/
synchronized (fdLock) {
- if (closePending || (fd == null)) {
+ if (closePending || (fd == null) || !fd.valid()) {
return true;
} else {
return false;