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

Concurrent Java 6 and Java 7 processes can open ServerSockets on the same port

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 7
    • core-libs
    • generic, x86
    • windows_2008, windows_7

      SYNOPSIS
      --------
      Concurrent Java 6 and Java 7 processes can open ServerSockets on the same port

      OPERATING SYSTEM
      ----------------
      Windows 7 specifically
      Does not appear using XP.

      FULL JDK VERSION
      ----------------
      Java 7 combined with any other JDK version.
      Tested with 1.7.0_01 and 1.6.0_30.

      PROBLEM DESCRIPTION
      -------------------
      On Windows 7 two java processes can open sockets on the same port. This is only possible if one of the processes is Java 7 and the other is another version of Java.

      If this is tried with Java 6 in both processes, or Java 7 in both processes, the second attempt to open a socket will always fail with a Bind exception, which is the expected behaviour.

      This problem only occurs on Windows 7.

      Also,if only IPV4 addresses are used in both the process by specifying these two options:

          -Djava.net.preferIPv4Stack=true
          -Djava.net.preferIPv6Address=false

      a BindException is thrown as expected.

      REPRODUCTION INSTRUCTIONS
      -------------------------
      The testcase below demonstrates the errant behaviour. If the testcase is invoked with any parameter it will open a ServerSocket on port 1979 and then wait. If it is invoked with no parameters it will just try and open a ServerSocket on port 1979, then close the socket.

      1. Using Java 6:

            java BPWinWSocket wait

         This will open a ServerSocket on port 1979 and then wait with the
         socket open.

      2. In a separate command window, using Java 7:

            java BPWinSocket

      Observed result:
      The second attempt to open the socket apparently succeeds - no Exception is thrown.

      Expected result:
      The second attempt to open the ServerSocket should fail with an BindException.

      TESTCASE SOURCE
      ---------------
      import java.io.IOException;
      import java.net.*;

      public class BPWinSocket {
          private int port = 1979;
          private ServerSocket s1 = null;

          public BPWinSocket(boolean wait) {
              open(s1, 1);
              if (wait) {
                  synchronized (this) {
                      try {
                          System.err.println("====> " + "Waiting ...");
                          this.wait();
                      } catch (InterruptedException e) {
                      }
                  }
              }
              printStatus();
              close();
          }

          public static void main(String[] args) {
              if (args.length == 0) {
                  new BPWinSocket(false);
              } else {
                  new BPWinSocket(true);
              }
          }

          private void open(ServerSocket sock, int num) {
              try {
                  sock = new ServerSocket(port);
                  System.err.println("====> " + "ServerSocket " +sock + " created on port "
                                     + port);
              } catch (IOException e) {
                  System.err.println("====> " + "ServerSocket s" + num + " failed on port "
                                     + port);
                  e.printStackTrace();
              }
          }

          private void close() {
              if (s1 != null) {
                  try {
                      s1.close();
                  } catch (IOException e) {
                  }
              }
          }

          /**
           * Just to make sure nothing odd happens with the sockets like premature GC
           */
          private void printStatus() {
              if (s1 != null) {
                  System.err.println("====> " + s1.toString());
              }
          }
      }

            Unassigned Unassigned
            dkorbel David Korbel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: