BindException : "Address already in use" - when restarting server socket after stop

467 views
Skip to first unread message

Priyanka Perera

unread,
Nov 12, 2020, 2:59:26 AM11/12/20
to ne...@googlegroups.com
Hi all,

Below scenario when repeated in a loop produced  "Address already in use' exception.
  • Start server
  • Sync start
  • Stop
  • Sync stop
Is it not guaranteed to release the server port even when the server socket is deactivated ?

netty 4.1.41.Final on openjdk-11.0.3

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

public class NettySequencingServer {
private final int port;

public NettySequencingServer(int port) {
this.port = port;
}

public static void main(String[] args) throws Exception {
int port = 5000;
NettySequencingServer nettySequencingServer = new NettySequencingServer(port);

for (int i = 0; i < 10000; i++) {
nettySequencingServer.startStop();
}
}

public void startStop() throws Exception {
final Decoder decoder = new Decoder();
EventLoopGroup group = new NioEventLoopGroup(1);
ServerBootstrap b = new ServerBootstrap();

b.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>(){
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(decoder);
}
});

try {
ChannelFuture startFuture = b.bind();
Channel channel = startFuture.channel();
// Start Server
startFuture.sync();
if (!startFuture.isSuccess()) {
System.out.println("Start failed");
}

// Stop Server
ChannelFuture stopFuture = channel.disconnect().sync();

if (!stopFuture.isSuccess()) {
System.out.println("Stop failed");
}
} catch (Exception e) {
System.out.println(e.getMessage());
for (StackTraceElement ele : e.getStackTrace()) {
System.out.println(ele.toString());
}
}
}
}

Address already in use
java.base/sun.nio.ch.Net.bind0(Native Method)
java.base/sun.nio.ch.Net.bind(Net.java:461)
java.base/sun.nio.ch.Net.bind(Net.java:453)
java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:132)
io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:551)
io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1346)
io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:503)
io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:488)
io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:985)
io.netty.channel.AbstractChannel.bind(AbstractChannel.java:247)
io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:344)
io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:510)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:518)
io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1044)
io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
java.base/java.lang.Thread.run(Thread.java:834)

Julian Feinauer

unread,
Nov 12, 2020, 3:39:37 AM11/12/20
to ne...@googlegroups.com

Hi,

 

if I remember correctly you also have to free the EventLoopGroup to release all ressources completely.

Could you try that?

 

Best

Julian

--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/CAJt14FNODZsszdGWcKUz%2BmGc6izcTdFFVQy2HQNDrPJRjnaj3g%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages