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

InetAddress.getLocalHost() fails, throws NPE, hangs forever, with bad hostname

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.4.1
    • core-libs
    • x86
    • linux

      Name: nt126004 Date: 04/21/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)


      FULL OS VERSION :
      Linux bogus 2.4.18-3 #1 Thu Apr 18 07:37:53 EDT 2002 i686 unknown


      EXTRA RELEVANT SYSTEM CONFIGURATION :
      hostname is (mis)configured so that hostname (`uname -n`) cannot be resolved
      (machine is configured for DNS resolution)

      A DESCRIPTION OF THE PROBLEM :
      While similar to bug 4645267, this is *not* fixed by whatever fixed that bug.

      When the local hostname (returned by uname -n) is not resolvable to an IP address,
      InetAddress.getLocalHost() throws an UnknownHostException the first time (okay),
      throws a NullPointerException the second time (bad, but tolerable),
      and hangs indefinitely the third time (really not acceptable)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile attached source code, run with a valid hostname, see expected results
      change hostname to bogus, run again, see two exceptions and hung thread


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Java 1.4.0_01 gives the following (not ideal, but acceptable):

      # /mnt/j2sdk1.4.0_01/bin/java IAdd
       getLocalHost mako.sysdetect.com/172.16.1.44
       getLocalHost mako.sysdetect.com/172.16.1.44
       getLocalHost mako.sysdetect.com/172.16.1.44
      # hostname bogus
      # /mnt/j2sdk1.4.0_01/bin/java IAdd
       getLocalHost /0.0.0.0
       getLocalHost /0.0.0.0
       getLocalHost /0.0.0.0

      Java 1.4.1_01 gives the following:

      # /mnt/j2sdk1.4.1_01/bin/java IAdd
       getLocalHost mako.sysdetect.com/172.16.1.44
       getLocalHost mako.sysdetect.com/172.16.1.44
       getLocalHost mako.sysdetect.com/172.16.1.44
      # hostname bogus
      # /mnt/j2sdk1.4.1_01/bin/java IAdd

      besides exceptions thrown on the first two calls (noted below),
      the third call to InetAddress.getLocalHost() hangs forever


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.net.UnknownHostException: bogus: bogus
              at java.net.InetAddress.getLocalHost(InetAddress.java:1128)
              at IAdd.main(IAdd.java:5)
      java.lang.NullPointerException
              at java.net.InetAddress.cacheAddress(InetAddress.java:648)
              at java.net.InetAddress.cacheAddress(InetAddress.java:633)
              at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1016)
              at java.net.InetAddress.getLocalHost(InetAddress.java:1125)
              at IAdd.main(IAdd.java:11)

      attaching to the hung java process with jdb shows the following:
      Initializing jdb ...
      > threads
      Group system:
        (java.lang.ref.Reference$ReferenceHandler)0x144 Reference Handler cond. waiting
        (java.lang.ref.Finalizer$FinalizerThread)0x145 Finalizer cond. waiting
        (java.lang.Thread)0x146 Signal Dispatcher running
      Group main:
        (java.lang.Thread)0x1 main cond. waiting
        (java.util.logging.LogManager$Cleaner)0x148 Thread-0 unknown

      main[1] suspend
      All threads suspended.
      main[1] where 0x1
        [1] java.lang.Object.wait (native method)
        [2] java.lang.Object.wait (Object.java:426)
        [3] java.net.InetAddress.checkLookupTable (InetAddress.java:1,046)
        [4] java.net.InetAddress.getAddressFromNameService (InetAddress.java:985)
        [5] java.net.InetAddress.getLocalHost (InetAddress.java:1,125)
        [6] IAdd.main (IAdd.java:17)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.net.*;
      public class IAdd {
        public static void main(String[] args) {
          try {
            System.out.println(" getLocalHost " +
                               InetAddress.getLocalHost());
          } catch(Exception e) {
            e.printStackTrace();
          }
          try {
            System.out.println(" getLocalHost " +
                               InetAddress.getLocalHost());
          } catch(Exception e) {
            e.printStackTrace();
          }
          try {
            System.out.println(" getLocalHost " +
                               InetAddress.getLocalHost());
          } catch(Exception e) {
            e.printStackTrace();
          }
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The best workaround is to make sure the hostname is resolvable; if necessary
      by switching to file-based resolution and putting an entry in /etc/hosts.

      However, our application may be deployed in customer sites on demo machines
      that are not well-administered, and no qualified sysadmin is available.

      So we just have our own local method that centralizes all access to the
      InetAddress.getLocalHost() - this method catches the NPE and returns an
      entry of "localhost" to try and prevent future hangs, which could occur
      if there was any attempt to resolve the bogus hostname, not just calls to
      InetAddress.getLocalHost()
      (Review ID: 182634)
      ======================================================================

            yuwangsunw Yujiang Wang (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: