Merge "AbstractPlainSocketImpl#close shouldn't set fd to null"
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;