Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8212798

Enhance BindException message with address information

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      Having a java server process which creates a multitude of TCP ServerSocket, it happens sometimes that some other OS process occupies the desired TCP port. The resulting exception is as follows:

      JDK 8 stack trace:
      java.net.BindException: Address already in use: JVM_Bind
      at java.net.DualStackPlainSocketImpl.bind0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
      at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
      at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
      at java.net.ServerSocket.bind(ServerSocket.java:375)
      at java.net.ServerSocket.<init>(ServerSocket.java:237)
      at java.net.ServerSocket.<init>(ServerSocket.java:128)
      at BindTest.main(BindTest.java:8)

      JDK 11 stack trace:
      java.net.BindException: Address already in use: NET_Bind
      at java.base/java.net.PlainSocketImpl.bind0(Native Method)
      at java.base/java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:132)
      at java.base/java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:436)
      at java.base/java.net.ServerSocket.bind(ServerSocket.java:381)
      at java.base/java.net.ServerSocket.<init>(ServerSocket.java:243)
      at java.base/java.net.ServerSocket.<init>(ServerSocket.java:135)
      at BindTest.main(BindTest.java:8)

      JDK 13 stack trace:
      Exception in thread "main" java.net.BindException: Address already in use
      at java.base/sun.nio.ch.Net.bind0(Native Method)
      at java.base/sun.nio.ch.Net.bind(Net.java:491)
      at java.base/sun.nio.ch.Net.bind(Net.java:480)
      at java.base/sun.nio.ch.NioSocketImpl.bind(NioSocketImpl.java:643)
      at java.base/java.net.ServerSocket.bind(ServerSocket.java:396)
      at java.base/java.net.ServerSocket.<init>(ServerSocket.java:282)
      at java.base/java.net.ServerSocket.<init>(ServerSocket.java:173)
      at BindTest.main(BindTest.java:8)

      Having this stacktrace, it is very difficult to understand which port causes the bind failure.
      A message that adds address/port info like "Address already in use: NET_Bind <address:port>" should be better.

      Here is a simple repro code that creates two ServerSocket on same port locally - the same happens if the port is occuped by another OS process:

      import java.net.ServerSocket;
      public class BindTest {
      public static void main(String[] args) {
      try {
      try (ServerSocket s1 = new ServerSocket(8080)) {
      try (ServerSocket s2 = new ServerSocket(8080)) {
      }
      }
      } catch (Exception ex) {
      ex.printStackTrace();
      }
      }
      }



            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: