Overview
I am running JUnit tests on two classes. The WebVerticle class deploys a verticle and instantiates a DatagramSocket class by passing the verticle's reference. The DatagramSocket class then uses the verticle to deploy a datagram socket. When I run the unit tests for these classes, I can see that the tests have passed, but there is an exception with the error message: "Failed to submit a listener notification task. Event loop shut down?". The Finalizer is unable to close a datagram socket, due to an event loop being shut down prematurely, and no listeners can be notified.
Problem
The full error trace:
Aug 09, 2017 10:24:28 AM io.netty.util.concurrent.DefaultPromise safeExecute
SEVERE: Failed to submit a listener notification task. Event loop shut down?
java.util.concurrent.RejectedExecutionException: event executor terminated
at io.netty.util.concurrent.SingleThreadEventExecutor.reject(SingleThreadEventExecutor.java:821)
at io.netty.util.concurrent.SingleThreadEventExecutor.offerTask(SingleThreadEventExecutor.java:327)
at io.netty.util.concurrent.SingleThreadEventExecutor.addTask(SingleThreadEventExecutor.java:320)
at io.netty.util.concurrent.SingleThreadEventExecutor.execute(SingleThreadEventExecutor.java:746)
at io.netty.util.concurrent.DefaultPromise.safeExecute(DefaultPromise.java:760)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:428)
at io.netty.util.concurrent.DefaultPromise.setFailure(DefaultPromise.java:113)
at io.netty.channel.DefaultChannelPromise.setFailure(DefaultChannelPromise.java:87)
at io.netty.channel.AbstractChannelHandlerContext.safeExecute(AbstractChannelHandlerContext.java:1011)
at io.netty.channel.AbstractChannelHandlerContext.close(AbstractChannelHandlerContext.java:611)
at io.netty.channel.AbstractChannelHandlerContext.close(AbstractChannelHandlerContext.java:466)
at io.netty.channel.DefaultChannelPipeline.close(DefaultChannelPipeline.java:964)
at io.netty.channel.AbstractChannel.close(AbstractChannel.java:234)
at io.vertx.core.net.impl.ConnectionBase.close(ConnectionBase.java:121)
at io.vertx.core.datagram.impl.DatagramSocketImpl.finalize(DatagramSocketImpl.java:341)
at java.lang.System$2.invokeFinalize(System.java:1270)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:98)
at java.lang.ref.Finalizer.access$100(Finalizer.java:34)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:210)
Symptoms + Analysis
The above error messages always occurs right after:
Running testCleanUp.DataSocketTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.484 sec - in testCleanUp.DataSocketTest
Running testCleanUp.WebVerticleTest
Running each test on its own shows no problems. The problem is only reproducible in this order: DataSocketDeployer, WebVerticle.
Initially, I thought that the socket was not being closed in time for the second test to run. The theory was that the reference of the data socket survived for sometime, even after the vertx.close() was initialised, causing some race conditions to occur.
Ensuring that the datagram socket was closed, there were no datagrams being sent, and making the thread sleep for a fairly long time in between the tests did not solve the issue.
Code
I have put together a reproducer - Please see the attached code.
If anyone could help, would be much appreciated!