changeset 49680:92cca24c8807

8199329: Remove code that attempts to read bytes after connection reset reported Reviewed-by: redestad, clanger, chegar
author alanb
date Thu, 15 Mar 2018 11:02:22 +0000
parents 15a0e60c8b97
children 7443b946694a
files src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java src/java.base/share/classes/java/net/SocketInputStream.java src/java.base/share/classes/java/net/SocketOutputStream.java src/java.base/windows/classes/java/net/PlainSocketImpl.java
diffstat 4 files changed, 17 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java	Thu Mar 15 10:47:58 2018 +0000
+++ b/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java	Thu Mar 15 11:02:22 2018 +0000
@@ -45,8 +45,7 @@
  *
  * @author  Steven B. Byrne
  */
-abstract class AbstractPlainSocketImpl extends SocketImpl
-{
+abstract class AbstractPlainSocketImpl extends SocketImpl {
     /* instance variable for SO_TIMEOUT */
     int timeout;   // timeout in millisec
     // traffic class
@@ -68,11 +67,7 @@
     protected boolean closePending = false;
 
     /* indicates connection reset state */
-    private int CONNECTION_NOT_RESET = 0;
-    private int CONNECTION_RESET_PENDING = 1;
-    private int CONNECTION_RESET = 2;
-    private int resetState;
-    private final Object resetLock = new Object();
+    private volatile boolean connectionReset;
 
    /* whether this Socket is a stream (TCP) socket or not (UDP)
     */
@@ -541,18 +536,8 @@
         int n = 0;
         try {
             n = socketAvailable();
-            if (n == 0 && isConnectionResetPending()) {
-                setConnectionReset();
-            }
         } catch (ConnectionResetException exc1) {
-            setConnectionResetPending();
-            try {
-                n = socketAvailable();
-                if (n == 0) {
-                    setConnectionReset();
-                }
-            } catch (ConnectionResetException exc2) {
-            }
+            setConnectionReset();
         }
         return n;
     }
@@ -680,31 +665,12 @@
         }
     }
 
-    public boolean isConnectionReset() {
-        synchronized (resetLock) {
-            return (resetState == CONNECTION_RESET);
-        }
+    boolean isConnectionReset() {
+        return connectionReset;
     }
 
-    public boolean isConnectionResetPending() {
-        synchronized (resetLock) {
-            return (resetState == CONNECTION_RESET_PENDING);
-        }
-    }
-
-    public void setConnectionReset() {
-        synchronized (resetLock) {
-            resetState = CONNECTION_RESET;
-        }
-    }
-
-    public void setConnectionResetPending() {
-        synchronized (resetLock) {
-            if (resetState == CONNECTION_NOT_RESET) {
-                resetState = CONNECTION_RESET_PENDING;
-            }
-        }
-
+    void setConnectionReset() {
+        connectionReset = true;
     }
 
     /*
--- a/src/java.base/share/classes/java/net/SocketInputStream.java	Thu Mar 15 10:47:58 2018 +0000
+++ b/src/java.base/share/classes/java/net/SocketInputStream.java	Thu Mar 15 11:02:22 2018 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -40,8 +40,7 @@
  * @author      Jonathan Payne
  * @author      Arthur van Hoff
  */
-class SocketInputStream extends FileInputStream
-{
+class SocketInputStream extends FileInputStream {
     static {
         init();
     }
@@ -163,8 +162,6 @@
                     + " off == " + off + " buffer length == " + b.length);
         }
 
-        boolean gotReset = false;
-
         // acquire file descriptor and do the read
         FileDescriptor fd = impl.acquireFD();
         try {
@@ -173,39 +170,18 @@
                 return n;
             }
         } catch (ConnectionResetException rstExc) {
-            gotReset = true;
+            impl.setConnectionReset();
         } finally {
             impl.releaseFD();
         }
 
         /*
-         * We receive a "connection reset" but there may be bytes still
-         * buffered on the socket
-         */
-        if (gotReset) {
-            impl.setConnectionResetPending();
-            impl.acquireFD();
-            try {
-                n = socketRead(fd, b, off, length, timeout);
-                if (n > 0) {
-                    return n;
-                }
-            } catch (ConnectionResetException rstExc) {
-            } finally {
-                impl.releaseFD();
-            }
-        }
-
-        /*
          * If we get here we are at EOF, the socket has been closed,
          * or the connection has been reset.
          */
         if (impl.isClosedOrPending()) {
             throw new SocketException("Socket closed");
         }
-        if (impl.isConnectionResetPending()) {
-            impl.setConnectionReset();
-        }
         if (impl.isConnectionReset()) {
             throw new SocketException("Connection reset");
         }
--- a/src/java.base/share/classes/java/net/SocketOutputStream.java	Thu Mar 15 10:47:58 2018 +0000
+++ b/src/java.base/share/classes/java/net/SocketOutputStream.java	Thu Mar 15 11:02:22 2018 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,8 +38,7 @@
  * @author      Jonathan Payne
  * @author      Arthur van Hoff
  */
-class SocketOutputStream extends FileOutputStream
-{
+class SocketOutputStream extends FileOutputStream {
     static {
         init();
     }
@@ -111,7 +110,7 @@
             socketWrite0(fd, b, off, len);
         } catch (SocketException se) {
             if (se instanceof sun.net.ConnectionResetException) {
-                impl.setConnectionResetPending();
+                impl.setConnectionReset();
                 se = new SocketException("Connection reset");
             }
             if (impl.isClosedOrPending()) {
--- a/src/java.base/windows/classes/java/net/PlainSocketImpl.java	Thu Mar 15 10:47:58 2018 +0000
+++ b/src/java.base/windows/classes/java/net/PlainSocketImpl.java	Thu Mar 15 11:02:22 2018 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -43,8 +43,7 @@
  * @author Chris Hegarty
  */
 
-class PlainSocketImpl extends AbstractPlainSocketImpl
-{
+class PlainSocketImpl extends AbstractPlainSocketImpl {
     private AbstractPlainSocketImpl impl;
 
     /* java.net.preferIPv4Stack */
@@ -254,22 +253,14 @@
         impl.releaseFD();
     }
 
-    public boolean isConnectionReset() {
+    boolean isConnectionReset() {
         return impl.isConnectionReset();
     }
 
-    public boolean isConnectionResetPending() {
-        return impl.isConnectionResetPending();
-    }
-
-    public void setConnectionReset() {
+    void setConnectionReset() {
         impl.setConnectionReset();
     }
 
-    public void setConnectionResetPending() {
-        impl.setConnectionResetPending();
-    }
-
     public boolean isClosedOrPending() {
         return impl.isClosedOrPending();
     }