8199329: Remove code that attempts to read bytes after connection reset reported

Reviewed-by: redestad, clanger, chegar
This commit is contained in:
Alan Bateman 2018-03-15 11:02:22 +00:00
parent 3a7f72200c
commit fc927f60c3
4 changed files with 17 additions and 85 deletions

@ -45,8 +45,7 @@ import sun.net.ResourceManager;
*
* @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 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
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 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
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 @@ abstract class AbstractPlainSocketImpl extends SocketImpl
}
}
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;
}
/*

@ -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 @@ import sun.net.ConnectionResetException;
* @author Jonathan Payne
* @author Arthur van Hoff
*/
class SocketInputStream extends FileInputStream
{
class SocketInputStream extends FileInputStream {
static {
init();
}
@ -163,8 +162,6 @@ class SocketInputStream extends FileInputStream
+ " off == " + off + " buffer length == " + b.length);
}
boolean gotReset = false;
// acquire file descriptor and do the read
FileDescriptor fd = impl.acquireFD();
try {
@ -173,29 +170,11 @@ class SocketInputStream extends FileInputStream
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.
@ -203,9 +182,6 @@ class SocketInputStream extends FileInputStream
if (impl.isClosedOrPending()) {
throw new SocketException("Socket closed");
}
if (impl.isConnectionResetPending()) {
impl.setConnectionReset();
}
if (impl.isConnectionReset()) {
throw new SocketException("Connection reset");
}

@ -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 @@ import java.nio.channels.FileChannel;
* @author Jonathan Payne
* @author Arthur van Hoff
*/
class SocketOutputStream extends FileOutputStream
{
class SocketOutputStream extends FileOutputStream {
static {
init();
}
@ -111,7 +110,7 @@ class SocketOutputStream extends FileOutputStream
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()) {

@ -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 @@ import sun.security.action.GetPropertyAction;
* @author Chris Hegarty
*/
class PlainSocketImpl extends AbstractPlainSocketImpl
{
class PlainSocketImpl extends AbstractPlainSocketImpl {
private AbstractPlainSocketImpl impl;
/* java.net.preferIPv4Stack */
@ -254,22 +253,14 @@ class PlainSocketImpl extends AbstractPlainSocketImpl
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();
}