JDK-8246707 : (sc) SocketChannel.read/write throws AsynchronousCloseException on closed channel
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 11,14
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-06-05
  • Updated: 2024-11-20
  • Resolved: 2020-08-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 16
11.0.11Fixed 13.0.7Fixed 16 b11Fixed
Related Reports
Relates :  
Relates :  
Description
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class NIOTest {
    public static void main(String[] args) throws Exception {
        ServerSocketChannel server = ServerSocketChannel.open();
        server.bind(null);
        SocketAddress saddr = server.getLocalAddress();
        SocketChannel client = SocketChannel.open(saddr);
        System.out.println(client);
        client.close();
        ByteBuffer buf = ByteBuffer.wrap("Hello world".getBytes());
        client.write(buf);
    }
}

throws AsynchronousCloseException instead of ChannelClosedException
Comments
Fix request (13u) Requesting backport to 13u for parity with 11u. The patch applies cleanly. Tested with tier1; new test fails without the patch, passes with it.
12-03-2021

Fix request (11u) Backport this patch to throw ChannelClosedException when calling SocketChannel.read/write on a closed channel. This patch doesn't apply cleanly but is fairly trivial. The reason is that thelatest jdk11u-dev has no ` blockingRead ` and `blockingWriteFully` function,because they were added in jdk13 through JDK-8222774. Tested with tier1, tier2. No regression in tests. https://mail.openjdk.java.net/pipermail/jdk-updates-dev/2021-February/004842.html
23-02-2021

URL: https://hg.openjdk.java.net/jdk/jdk/rev/c8c6030e4d1f User: pconcannon Date: 2020-08-12 11:34:18 +0000
12-08-2020

The SocketChannel implementation was significantly refactored in JDK 11 to improve the performance non-blocking read/write operations. A side effect is that AsynchronousCloseException is thrown instead of ChannelClosedException when the read/write methods are invoked on a closed channel. AsynchronousCloseException is a ChannelClosedException so not wrong but it is confusing and not the original intention. I've attempted a patch to this bug with the changes to SocketChannelImpl. The other channel implementations were refactored too but they don't have this issue.
07-06-2020