Skip to content

Commit

Permalink
Fix(KonaFiber): Support available() and Connection Reset of InputStre…
Browse files Browse the repository at this point in the history
…am of VTSocketImpl

--story=873966885

Contributed-by:emoryzheng

    modified:   jdk/src/share/classes/sun/nio/ch/VTSocketImpl.java
    new file:   jdk/test/java/net/Socket/ConnectionReset.java
  • Loading branch information
miao-zheng committed Aug 9, 2022
1 parent 989a2f8 commit db4a50f
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 29 deletions.
59 changes: 30 additions & 29 deletions jdk/src/share/classes/sun/nio/ch/VTSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class VTSocketImpl extends SocketImpl {
protected SocketChannelImpl sc = null;
protected ServerSocketChannelImpl ssc = null;

/* indicates connection reset state */
private boolean connectionReset;

public VTSocketImpl(boolean isServerSocket, boolean isSocksSocket) {
this.isServerSocket = isServerSocket;
this.isSocksSocket = isSocksSocket;
Expand Down Expand Up @@ -230,8 +233,6 @@ public InputStream run() throws IOException {
private byte[] bs = null;
private byte[] b1 = null;

private ByteBuffer readAhead = null;

@Override
public int read() throws IOException {
if (b1 == null) {
Expand Down Expand Up @@ -268,6 +269,13 @@ private int read(ByteBuffer bb) throws IOException {
return read0(bb);
} catch (ClosedChannelException x) {
throw new SocketException("Socket closed");
} catch (IOException e) {
if (e.getLocalizedMessage().startsWith("Connection reset by peer")) {
connectionReset = true;
throw new SocketException("Connection reset");
} else {
throw e;
}
} finally {
readLock.unlock();
}
Expand All @@ -276,18 +284,9 @@ private int read(ByteBuffer bb) throws IOException {
private int read0(ByteBuffer bb)
throws IOException {
int n;
if (readAhead != null && readAhead.hasRemaining()) {
if (bb.remaining() >= readAhead.remaining()) {
n = readAhead.remaining();
bb.put(readAhead);
} else {
n = bb.remaining();
for (int i = 0; i < n; i++) {
bb.put(readAhead.get());
}
}
return n;
}

if (connectionReset)
throw new SocketException("Connection reset");

if ((n = ch.read(bb)) != 0) {
return n;
Expand All @@ -309,17 +308,7 @@ private int read0(ByteBuffer bb)

@Override
public int available() throws IOException {
if (readAhead == null) {
readAhead = ByteBuffer.allocate(4096);
} else if (readAhead.hasRemaining()) {
return readAhead.remaining();
}

readAhead.clear();
ch.read(readAhead);
readAhead.flip();

return readAhead.remaining();
return VTSocketImpl.this.available();
}

@Override
Expand All @@ -336,6 +325,22 @@ public void close() throws IOException {
return socketInputStream;
}

@Override
protected int available() throws IOException {
stateLock.lock();
try {
if (!getSocketChannelImpl().isOpen())
throw new ClosedChannelException();
if (isInputClosed) {
return 0;
} else {
return Net.available(getSocketChannelImpl().getFD());
}
} finally {
stateLock.unlock();
}
}

@Override
public OutputStream getOutputStream() throws IOException {
try {
Expand Down Expand Up @@ -498,10 +503,6 @@ protected void sendUrgentData (int data) throws IOException {
throw new IOException("Socket buffer full");
}

protected int available() throws IOException {
return 0;
}

public void setSocketChannel(SocketChannel target) {
sc = (SocketChannelImpl)target;
}
Expand Down
218 changes: 218 additions & 0 deletions jdk/test/java/net/Socket/ConnectionReset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
* Copyright (c) 2019, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/**
* @test
* @requires os.family != "solaris"
* @run testng ConnectionReset
* @summary Test behavior of read and available when a connection is reset
*/

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.testng.annotations.Test;
import static org.testng.Assert.*;

@Test
public class ConnectionReset {

static final int REPEAT_COUNT = 5;

/**
* Tests available before read when there are no bytes to read
*/
public void testAvailableBeforeRead1() throws IOException {
System.out.println("testAvailableBeforeRead1");
withResetConnection(null, s -> {
InputStream in = s.getInputStream();
for (int i=0; i<REPEAT_COUNT; i++) {
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
assertTrue(bytesAvailable == 0);
try {
int bytesRead = in.read();
if (bytesRead == -1) {
System.out.println("read => EOF");
} else {
System.out.println("read => 1 byte");
}
assertTrue(false);
} catch (IOException ioe) {
System.out.format("read => %s (expected)%n", ioe);
}
}
});
}

/**
* Tests available before read when there are bytes to read
*/
public void testAvailableBeforeRead2() throws IOException {
System.out.println("testAvailableBeforeRead2");
byte[] data = { 1, 2, 3 };
withResetConnection(data, s -> {
InputStream in = s.getInputStream();
int remaining = data.length;
for (int i=0; i<REPEAT_COUNT; i++) {
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
assertTrue(bytesAvailable <= remaining);
try {
int bytesRead = in.read();
if (bytesRead == -1) {
System.out.println("read => EOF");
assertTrue(false);
} else {
System.out.println("read => 1 byte");
assertTrue(remaining > 0);
remaining--;
}
} catch (IOException ioe) {
System.out.format("read => %s%n", ioe);
remaining = 0;
}
}
});
}

/**
* Tests read before available when there are no bytes to read
*/
public void testReadBeforeAvailable1() throws IOException {
System.out.println("testReadBeforeAvailable1");
withResetConnection(null, s -> {
InputStream in = s.getInputStream();
for (int i=0; i<REPEAT_COUNT; i++) {
try {
int bytesRead = in.read();
if (bytesRead == -1) {
System.out.println("read => EOF");
} else {
System.out.println("read => 1 byte");
}
assertTrue(false);
} catch (IOException ioe) {
System.out.format("read => %s (expected)%n", ioe);
}
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
assertTrue(bytesAvailable == 0);
}
});
}

/**
* Tests read before available when there are bytes to read
*/
public void testReadBeforeAvailable2() throws IOException {
System.out.println("testReadBeforeAvailable2");
byte[] data = { 1, 2, 3 };
withResetConnection(data, s -> {
InputStream in = s.getInputStream();
int remaining = data.length;
for (int i=0; i<REPEAT_COUNT; i++) {
try {
int bytesRead = in.read();
if (bytesRead == -1) {
System.out.println("read => EOF");
assertTrue(false);
} else {
System.out.println("read => 1 byte");
assertTrue(remaining > 0);
remaining--;
}
} catch (IOException ioe) {
System.out.format("read => %s%n", ioe);
remaining = 0;
}
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
assertTrue(bytesAvailable <= remaining);
}
});
}

/**
* Tests available and read on a socket closed after connection reset
*/
public void testAfterClose() throws IOException {
System.out.println("testAfterClose");
withResetConnection(null, s -> {
InputStream in = s.getInputStream();
try {
in.read();
assertTrue(false);
} catch (IOException ioe) {
// expected
}
s.close();
try {
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
assertTrue(false);
} catch (IOException ioe) {
System.out.format("available => %s (expected)%n", ioe);
}
try {
int n = in.read();
System.out.format("read => %d%n", n);
assertTrue(false);
} catch (IOException ioe) {
System.out.format("read => %s (expected)%n", ioe);
}
});
}

interface ThrowingConsumer<T> {
void accept(T t) throws IOException;
}

/**
* Invokes a consumer with a Socket connected to a peer that has closed the
* connection with a "connection reset". The peer sends the given data bytes
* before closing (when data is not null).
*/
static void withResetConnection(byte[] data, ThrowingConsumer<Socket> consumer)
throws IOException
{
InetAddress loopback = InetAddress.getLoopbackAddress();
try (ServerSocket listener = new ServerSocket()) {
listener.bind(new InetSocketAddress(loopback, 0));
try (Socket socket = new Socket()) {
socket.connect(listener.getLocalSocketAddress());
try (Socket peer = listener.accept()) {
if (data != null) {
peer.getOutputStream().write(data);
}
peer.setSoLinger(true, 0);
}
consumer.accept(socket);
}
}
}
}

0 comments on commit db4a50f

Please sign in to comment.