Adding SocketChannel toString to connection exception messages (original) (raw)

Steven Schlansker stevenschlansker at gmail.com
Fri Dec 22 00:29:18 UTC 2017


On Dec 21, 2017, at 11:11 AM, Steven Schlansker <stevenschlansker at gmail.com> wrote:

What if ConnectException included the attempted hostname / IP / port SocketAddress? java.net.ConnectException: Connection to 'foo.mycorp.com[10.x.x.x]:12345' refused Much more useful! This could also be extended to various other socket exceptions.

I started hacking around with this, first with PlainSocketImpl only. I apologize in advance for what is likely to be terrible JNI style, as I don't have much experience with it and I omitted any error handling for the POC - but I think it proves that this isn't too big of a project, as I would want to cover SocketChannelImpl and maybe UnixAsynchronousSocketChannelImpl and call it good enough for my uses.

Behold!

[me at myhost]% java pkg.SocketFail java.net.ConnectException: Connection refused (Connection refused)

[me at myhost]% ~/code/jdk10/build/macosx-x86_64-normal-server-release/jdk/bin/java pkg.SocketFail java.net.ConnectException: Connection refused (Connection refused: localhost/127.0.0.1:12345)

Am I correct that the best path towards acceptance for this kind of change is to target jdk10 and then beg for a backport to jdk9u? Thanks again for considering this improvement! WIP patch below inline.

diff --git a/src/java.base/unix/native/libnet/PlainSocketImpl.c b/src/java.base/unix/native/libnet/PlainSocketImpl.c --- a/src/java.base/unix/native/libnet/PlainSocketImpl.c +++ b/src/java.base/unix/native/libnet/PlainSocketImpl.c @@ -217,6 +217,9 @@ (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); }

+// private helper for socketConnect to describe errors +static void sockErrDescribe(JNIEnv env, char errbuf, char* msg, jobject ia, jint port); + /*

{

@@ -329,8 +334,8 @@ jlong prevNanoTime = JVM_NanoTime(env, 0);

         if (errno != EINPROGRESS) {

@@ -372,8 +377,8 @@ } /* while */

         if (connect_rv == 0) {

@@ -419,36 +424,37 @@ * a more descriptive exception text is used. */ if (connect_rv == -1 && errno == EINVAL) {

#endif #if defined(EPROTO) if (errno == EPROTO) {

#endif if (errno == ECONNREFUSED) {

+static void sockErrDescribe(JNIEnv env, char errbuf, char* msg, jobject ia, jint port) {

+} + /*



More information about the core-libs-dev mailing list