Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to ignore only "connection reset by peer" IOExceptions

I am getting really annoyed with loads of IOExceptions from socket read calls due to network problems. Normally it simply means someone killed the child process or the network went down badly (VPN connection dropped etc).

My server cannot do anything but I really dont want to see all these errors in the log files. Is there any way in java to ignore these exceptions?

I know at the Windows msdn level its a WSAENETRESET error so why do all errors get handled into a generic IOException and not an ConnectionResetException.

Having a connection reset by peer is something which is very generic and standard.

I cant find out if the exception message will appear localised on different Windows OS locales either.

like image 773
Neil Wightman Avatar asked Oct 18 '25 18:10

Neil Wightman


1 Answers

This is not a solution in itself, it is just to prove that npinti's answer, which does sound reasonable, turned out to be inaccurate when I tried to confirm it, and is hence misleading developers to build solutions that don't work as expected on non-English systems:

Contrary to what one might expect, IOException.getMessage() returns the same localized string as IOException.getLocalizedMessage().

When running this code

try {
    client.read(byteBuf);
} catch (IOException ioe) {
    System.out.println("IOException.getMessage(): \"" + ioe.getMessage() + "\"");
    System.out.println("IOException.getLocalizedMessage(): \"" + ioe.getLocalizedMessage() + "\"");
    throw ioe;
}

on a Japanese Windows, the output is this:

IOException.getMessage(): "既存の接続はリモート ホストに強制的に切断されました。"
IOException.getLocalizedMessage(): "既存の接続はリモート ホストに強制的に切断されました。"
Exception in thread "main" java.io.IOException: 既存の接続はリモート ホストに強制的に切断されました。
        at java.base/sun.nio.ch.SocketDispatcher.read0(Native Method)
        at java.base/sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
        at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:276)
        at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:245)
        at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:223)
        at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:353)
        at Test.main(Test.java:21)

On an English Linux it is this:

IOException.getMessage(): "Connection reset by peer"
IOException.getLocalizedMessage(): "Connection reset by peer"
Exception in thread "main" java.io.IOException: Connection reset by peer
        at java.base/sun.nio.ch.FileDispatcherImpl.read0(Native Method)
        at java.base/sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
        at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:276)
        at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:245)
        at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:223)
        at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:353)
        at Test.main(Test.java:21)

Windows Java version:

openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment Temurin-11.0.12+7 (build 11.0.12+7)
OpenJDK 64-Bit Server VM Temurin-11.0.12+7 (build 11.0.12+7, mixed mode)

Linux Java version:

openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-Ubuntu-0ubuntu3)
OpenJDK 64-Bit Server VM (build 11.0.12+7-Ubuntu-0ubuntu3, mixed mode, sharing)
like image 111
Evgeniy Berezovsky Avatar answered Oct 20 '25 09:10

Evgeniy Berezovsky