question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple EventLoopGroup cause socketException

See original GitHub issue

Hi. Recently I make a server-stress-test client with netty bootstrap.

First, I create multiple bootstrap to connect server and it causes error: java.net.SocketException: No buffer space available (maximum connections reached?): connect when the bootstrap was created bout 1,000 times in my computer. At last i search in google find the right way to make to multiple connection with bootstrap.

And i looked at the bootstrap code and found nothing to cause SocketException. Now i wonder why it causes SocketException when i create multiple bootstrap object and the correct way to create multiple client connection to server.

Here is the incorrect code(cause SocketException) and correct(maybe?) code.

Incorrect Code.

for(int i = 0; i < 5000; i++) {
  EventLoopGroup workerGroup = new NioEventLoopGroup();
  Bootstrap bootstrap = new Bootstrap();
  bootstrap
    .group(workerGroup)
    .channel(NioSocketChannel.class)
    .option(ChannelOption.SO_KEEPALIVE, true)
    .option(ChannelOption.SO_REUSEADDR, true)
    .handler(new ChannelInitializer<SocketChannel>() {
      @Override
      protected void initChannel(SocketChannel ch) throws Exception {
        ch.pipeline().addLast(new EchoHandler());
      }
    });

  try {
    ChannelFuture future = bootstrap.connect("localhost", 10000).sync();
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}

Correct(?) Code

EventLoopGroup workerGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap
  .group(workerGroup)
  .channel(NioSocketChannel.class)
  .option(ChannelOption.SO_KEEPALIVE, true)
  .option(ChannelOption.SO_REUSEADDR, true)
  .handler(new ChannelInitializer<SocketChannel>() {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
      ch.pipeline().addLast(new EchoHandler());
    }
  });

List<ChannelFuture> list = new ArrayList<>();
for(int i = 0; i < 5000; i++) {
  ChannelFuture future = bootstrap.connect("localhost", 10000).sync();
  list.add(future);
}

Error Exception

Exception in thread "main" java.lang.IllegalStateException: failed to create a child event loop
	at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:88)
	at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:58)
	at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:52)
	at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:87)
	at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:82)
	at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:63)
	at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:51)
	at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:43)
	at com.example.demo.DemoApplication.multipleBootstrap(DemoApplication.java:54)
	at com.example.demo.DemoApplication.main(DemoApplication.java:31)
Caused by: io.netty.channel.ChannelException: failed to open a new selector
	at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:175)
	at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:149)
	at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:127)
	at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:36)
	at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:84)
	... 9 more
Caused by: java.io.IOException: Unable to establish loopback connection
	at sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:94)
	at sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:61)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.nio.ch.PipeImpl.<init>(PipeImpl.java:171)
	at sun.nio.ch.SelectorProviderImpl.openPipe(SelectorProviderImpl.java:50)
	at java.nio.channels.Pipe.open(Pipe.java:155)
	at sun.nio.ch.WindowsSelectorImpl.<init>(WindowsSelectorImpl.java:127)
	at sun.nio.ch.WindowsSelectorProvider.openSelector(WindowsSelectorProvider.java:44)
	at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:173)
	... 13 more
Caused by: java.net.SocketException: No buffer space available (maximum connections reached?): connect
	at sun.nio.ch.Net.connect0(Native Method)
	at sun.nio.ch.Net.connect(Net.java:454)
	at sun.nio.ch.Net.connect(Net.java:446)
	at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
	at java.nio.channels.SocketChannel.open(SocketChannel.java:189)
	at sun.nio.ch.PipeImpl$Initializer$LoopbackConnector.run(PipeImpl.java:127)
	at sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:76)
	... 21 more

Expected behavior

Actual behavior

Steps to reproduce

Minimal yet complete reproducer code (or URL to code)

See Incorrect Code

Netty version

4.1.15.Final

JVM version (e.g. java -version)

java 1.8.0_151

OS version (e.g. uname -a)

Windows 10-64bit Enterprise 1709

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
normanmaurercommented, Jun 9, 2022

@chamil321 just call close() on the ServerChannel ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.net.SocketException in Netty - Stack Overflow
I'm using Netty-4.033 on Mac. My Server Code: EventLoopGroup masterGroup = null; EventLoopGroup workGroup = ...
Read more >
How to Handle the Socket Exception in Java - Rollbar
Some common causes for the SocketException are: Closed socket connection - The most common cause of SocketException is reading or writing from ...
Read more >
Netty data model, threading, and gotchas | by Ammar Khaku
When creating the server Channel, you must provide two EventLoopGroup objects: one for the main server Channel, and another that is used for ......
Read more >
How to Handle Java SocketException - Baeldung
The most common cause of SocketException is writing or reading data to or from a closed socket connection. Another cause of it is...
Read more >
How does java net SocketException Connection reset happen
SocketException : Connection reset errors in our logs ... .sms.http.filters.NoCacheFilter. ... There are several possible causes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found