Gradle: MessageIOException: Could not write message [EndOfStream] to 127.0.0.1 (Firewall)?
Asked Answered
D

2

18

I wrote a simple test project, which opens port 9123 for some time and exit:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Date;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class TimeServer
{
    private static final int PORT = 9123;
    public static void main( String[] args ) throws IOException
    {
        IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
        acceptor.setHandler( new TimeServerHandler() );
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        acceptor.bind( new InetSocketAddress(PORT) );
    }

    private static class TimeServerHandler extends IoHandlerAdapter {
        @Override
        public void exceptionCaught(IoSession session, Throwable cause ) throws Exception
        {
            cause.printStackTrace();
        }
        @Override
        public void messageReceived( IoSession session, Object message ) throws Exception
        {
            String str = message.toString();
            if( str.trim().equalsIgnoreCase("quit") ) {
                session.close();
                return;
            }
            Date date = new Date();
            session.write( date.toString() );
            System.out.println("Message written...");
        }
        @Override
        public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
        {
            System.out.println( "IDLE " + session.getIdleCount( status ));
        }
    }
}

...

import java.io.IOException;

public class TimeServerTest {


   @Test
   public void runningTimeServerForTime() throws IOException, InterruptedException {

      int period = 15000;

      System.out.println("Running time server for " + period + "ms");

      TimeServer.main(new String[] {});

      Thread.sleep(period);

      System.out.println("Done, exiting");

      System.exit(0);

   }
}

This test runs Ok under IntelliJ and when Windows firewall is Off.

When windows firewall is On, it fails with exception

>gradle test
:compileJava
Note: PATH\TimeServer.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
Unexpected exception thrown.
org.gradle.messaging.remote.internal.MessageIOException: Could not write message [EndOfStream] to '/127.0.0.1:58895'.
        at org.gradle.messaging.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:111)
        at org.gradle.messaging.remote.internal.hub.MessageHub$ConnectionDispatch.run(MessageHub.java:284)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
        at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: An existing connection was forcibly closed by the remote host
        at sun.nio.ch.SocketDispatcher.write0(Native Method)
        at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
        at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
        at sun.nio.ch.IOUtil.write(IOUtil.java:51)
        at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
        at org.gradle.messaging.remote.internal.inet.SocketConnection$SocketOutputStream.flush(SocketConnection.java:236)
        at org.gradle.messaging.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:109)
        ... 6 more

BUILD SUCCESSFUL

What is happening?

Why the referred port 58895 is very different, than opened port 9123?

How to make this test run w/o disabling firewall?

Which program to add to whitelist of windows firewall for this test runs ok?

Dorella answered 9/12, 2016 at 17:32 Comment(2)
From the stacktrace there is no link to your test visible. If you don't start your TimeServer. Will it also fail?Application
Windows 10 mobile hospot is enabled?Desrosiers
S
16

It may be related to how gradle handles System.exit() calls, see GRADLE-2759.

As a rule of thumb, it is not a good idea to have a System.exit() call in tests as it may mess with the test framework (what if everything is launched from the same JVM? You would abruptely shutdown it). You should add a way to shutdown your server gracefully.

Note that the Network error does not seem to have anything to do with your network code or your server: you can see from the package name in the stack trace (org.gradle.messaging.remote.internal) that it comes from Gradle. This is why the port is different and unrelated to the one you've specified.

No idea why this would pass with Windows Firewall off though.

Serow answered 18/12, 2016 at 16:34 Comment(1)
In my spring boot application, I have nowhere explicitly written System exit. Still, I am getting the same error with a different port.Velocipede
I
0

For me this was because of a typo in a specified IP address and port that I was trying to connect out to. Once I fixed the typo the client connected to the server and the error (with the unknown port 54345 or 61927) went away.

Inextinguishable answered 7/3, 2023 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.