Problem with half-closure related to connection-reset in Java 11

Norman Maurer norman.maurer at googlemail.com
Fri Jun 1 15:33:50 UTC 2018


Hi Chris,


> On 1. Jun 2018, at 17:28, Chris Hegarty <chris.hegarty at oracle.com> wrote:
> 
> Hi Norman,
> 
> On 30/05/18 09:16, Norman Maurer wrote:
>> ...
>> I added a reproducer which not uses any netty classes to the PR that for now ignores test-failures caused by this when running on Java11+:
>> https://github.com/netty/netty/pull/7984#issuecomment-393071386
> 
> Would it be possible for you to post the reproducer, plain text is fine, on the net-dev mailing list. That way we can be sure that any changes that are made in this area resolve the particular issue you observe. I think we understand the root cause, but it would be good to be sure.
> 
> -Chris.

Sure thing (its exactly the same code as in the pr):

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

public class Reproducer {

    public static void main(String... args) throws Throwable {
        CountDownLatch readLatch = new CountDownLatch(1);
        CountDownLatch writeLatch = new CountDownLatch(1);
        AtomicReference<Throwable> error = new AtomicReference<>();
        byte[] bytes = new byte[512];

        try (ServerSocket server = new ServerSocket(0)) {
            Thread thread = new Thread(() -> {
                try (Socket client = new Socket()){
                    client.connect(server.getLocalSocketAddress());

                    int readCount = 0;

                    try (InputStream in = client.getInputStream()) {
                        in.read();

                        readCount++;
                        readLatch.countDown();
                        writeLatch.await();


                        OutputStream out = client.getOutputStream();
                        try {
                            for (;;) {
                                // Just write until it fails.
                                out.write(1);
                            }
                        } catch (SocketException expected) {
                        }

                        try {
                            for (;;) {
                                // Read until it fails.
                                in.read();
                                readCount += 1;
                            }
                        } catch (SocketException expected) {
                        }
                    }

                    if (readCount != bytes.length) {
                        throw new AssertionError("Was not able to read remaining data after write error," +
                                " read " + readCount + "/" + bytes.length + ".");
                    }
                } catch (Throwable cause) {
                    error.set(cause);
                }
            });

            thread.start();

            Socket accepted = server.accept();

            // Use SO_LINGER of 0 to ensure we trigger a connection-reset when close the accepted socket.
            accepted.setSoLinger(true, 0);
            accepted.getOutputStream().write(bytes);

            // Wait until we read the first byte. After this there should still be 511 bytes left.
            readLatch.await();

            // Close the accepted channel and signal to the other thread that it should try to write now.
            accepted.close();
            writeLatch.countDown();

            thread.join();

            // Rethrow any error that was produced by the other thread.
            Throwable cause = error.get();
            if (cause != null) {
                throw cause;
            }
        }
    }
}

That said I think Alan has a reproducer for it which basically does exactly the same on the issue:

https://bugs.openjdk.java.net/browse/JDK-8203937?jql=text%20~%20%22connection%20reset%22 <https://bugs.openjdk.java.net/browse/JDK-8203937?jql=text%20~%20%22connection%20reset%22>

Thanks
Norman


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/net-dev/attachments/20180601/e96b4837/attachment-0001.html>


More information about the net-dev mailing list