RFR: JDK-8299475: Enhance SocketException by cause where it is missing in net and nio area [v3]
Matthias Baesken
mbaesken at openjdk.org
Wed Jan 4 09:04:06 UTC 2023
On Tue, 3 Jan 2023 13:45:13 GMT, Alan Bateman <alanb at openjdk.org> wrote:
>> Matthias Baesken has updated the pull request incrementally with one additional commit since the last revision:
>>
>> re-throw SocketException, remove one enhancement because it is meant to be internal
>
> src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java line 413:
>
>> 411: throw e;
>> 412: } catch (IOException ioe) {
>> 413: throw new SocketException(ioe.getMessage(), ioe);
>
> I'm in two minds on whether this is a good idea or not because the stack trace of the cause is mostly the same as the SocketException, e.g. right now we get a clear stack trace like this:
>
> Exception in thread "main" java.net.SocketException: Broken pipe
> at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.java:413)
> at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:433)
> at java.base/sun.nio.ch.NioSocketImpl$2.write(NioSocketImpl.java:812)
> at java.base/java.net.Socket$SocketOutputStream.write(Socket.java:1192)
> at java.base/java.io.OutputStream.write(OutputStream.java:124)
>
>
> but with the change we get a cause with almost the same stack trace:
>
> Exception in thread "main" java.net.SocketException: Broken pipe
> at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.java:413)
> at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:433)
> at java.base/sun.nio.ch.NioSocketImpl$2.write(NioSocketImpl.java:812)
> at java.base/java.net.Socket$SocketOutputStream.write(Socket.java:1192)
> at java.base/java.io.OutputStream.write(OutputStream.java:124)
> Caused by: java.io.IOException: Broken pipe
> at java.base/sun.nio.ch.SocketDispatcher.write0(Native Method)
> at java.base/sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:62)
> at java.base/sun.nio.ch.NioSocketImpl.tryWrite(NioSocketImpl.java:388)
> at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.java:404)
> at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:433)
> at java.base/sun.nio.ch.NioSocketImpl$2.write(NioSocketImpl.java:812)
> at java.base/java.net.Socket$SocketOutputStream.write(Socket.java:1192)
> at java.base/java.io.OutputStream.write(OutputStream.java:124)
>
>
> The reason for the translation from IOException to SocketException is so that the SocketImpl added via JEP 353 throws the same specific exceptions as the legacy implementation. We were concerned there may be older code assuming that read/write throw SocketException. So one idea here is to avoid the confusing cause is to use the stack trace from the IOException, as in:
>
> var e = new SocketException(ioe.getMessage());
> e.setStackTrace(ioe.getStackTrace());
> throw e;
>
> which gives us a clear stack trace like we have now:
>
> Exception in thread "main" java.net.SocketException: Broken pipe
> at java.base/sun.nio.ch.SocketDispatcher.write0(Native Method)
> at java.base/sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:62)
> at java.base/sun.nio.ch.NioSocketImpl.tryWrite(NioSocketImpl.java:388)
> at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.java:404)
> at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:435)
> at java.base/sun.nio.ch.NioSocketImpl$2.write(NioSocketImpl.java:814)
> at java.base/java.net.Socket$SocketOutputStream.write(Socket.java:1192)
> at java.base/java.io.OutputStream.write(OutputStream.java:124)
Hi Alan, I adjusted this coding following your suggestion to use setStackTrace.
-------------
PR: https://git.openjdk.org/jdk/pull/11813
More information about the nio-dev
mailing list