JDK-4202949 : cannot bind ServerSocket to same port after closing it if exec'd process runs
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-01-14
  • Updated: 2000-08-21
  • Resolved: 2000-08-21
Description

Name: gsC80088			Date: 01/13/99


This is nearly the same as bug no. 4060907, but still reproducible in JDK1.2beta4!

The attached program does the following:
1. create a server socket on port 8003
2. 'exec' Windows-Notepad
3. close the server socket
4. try to create the server socket again

On (4.) I get the following error message:
java.net.BindException: Address in use: bind

If I do not exec Notepad or if I do a waitFor(Notepad), it works fine.

The test program:
--------------------------------------------------
import java.io.*;
import java.net.*;
import java.util.*;

public class TestBind
{
    public static void main (String[] args)
    {
        ServerSocket mySocket;

        try
        {
            Properties prop = System.getProperties();
            String javaver = prop.getProperty("java.version");
            if (javaver != null)
                System.err.println("java version: "+javaver);
            else
                System.err.println("java.version: not found");    
            // create socket
            System.err.println("create serverSocket");
            mySocket = new ServerSocket(8003);

            // exec notepad
            String[] execcmd = new String[2];
            execcmd[0] = "c:\\winnt\\system32\\notepad.exe";
            execcmd[1] = "c:\\tmp\\test.html";


            // execute the command
            System.err.println("start exec");
            Process notepad = Runtime.getRuntime().exec (execcmd);
            //System.err.println("waiting for end of notepad");
            //notepad.waitFor();

            // close socket
            System.err.println("close serverSocket");
            mySocket.close();

            // open socket again
            System.err.println("open serverSocket again");
            mySocket = new ServerSocket(8003);
            
        }
        catch (IOException e)
        {
            System.err.println(e);
        }
        /*catch (InterruptedException e)
        {
            System.err.println(e);
        }*/

        System.exit(0);
    }
}
(Review ID: 39948)
======================================================================

Comments
EVALUATION The socket may dulicated in the exec'd process and therefore closing the socket one process does not actually close and free-up the underlying socket. jeff.nisewanger@Eng 1999-01-26 This issue should no longer exist in 1.3. In 1.3 when sockets are created the socket handle is set to non-inheritiable. This means that the exec'ed process does not inherit the socket and therefore can not prevent the Java process from re-binding to the same port. Prior to 1.3 this was an issue because the socket handles were inherited into the exec'ed processes. Note that on the JDC there is a comment with a new test case that fails on 1.3. On examination there is a timing issue with this test case. The timing issue relates to the scheduling of the client and the server as there is a window within which the client can connect before the server closes the socket. This is not a bug. If the client code in the test case were updated to get the input stream and attempt a read then the read would correctly fail with a "connection reset by peer" socket exception. Given that 1.3 makes the sockets non-inheritable this bug should be closed. alan.bateman@ireland 2000-08-14
14-08-2000