JDK-8068016 : ServerSocket backlog is not limiting queue length
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 7u72
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: linux_ubuntu
  • CPU: x86_64
  • Submitted: 2014-12-19
  • Updated: 2014-12-22
  • Resolved: 2014-12-22
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_72"
Java(TM) SE Runtime Environment (build 1.7.0_72-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.72-b04, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux minkir-laptop 3.8.0-28-generic #41-Ubuntu SMP Fri Jul 26 16:26:01 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
As described in ServerSocket constructor:
http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#ServerSocket%28int,%20int%29

"The maximum queue length for incoming connection indications (a request to connect) is set to the backlog parameter. If a connection indication arrives when the queue is full, the connection is refused. "

Based on POSIX interpretation backlog is only for indication purposes and application (JVM) should implement queue limit:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html

"The backlog argument provides a hint to the implementation which the implementation shall use to limit the number of outstanding connections in the socket's listen queue."

Backlog doesn't seem to be working with simple ServerSocket implementation, number of connections are not limited as described in constructor specification

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Take client-server example sources from:
https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html

Minor modification to KKMultiServer.java to set backlog:
ServerSocket serverSocket = new ServerSocket(portNumber, 10)

Launch KKMultiServer and many KnockKnockClient's. 
Result - backlog is not limiting client to server.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
As backlog queue limit is reached, server shouldn't accept new connections (reset or ignore).
ACTUAL -
Backlog is not effective, number of client->server sockets established is not limited.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
Minor modification to introduce backlog:

        try (ServerSocket serverSocket = new ServerSocket(portNumber, 10)) { 
            while (listening) {
                    new KKMultiServerThread(serverSocket.accept()).start();
                }
            } catch (IOException e) {
            System.err.println("Could not listen on port " + portNumber);
            System.exit(-1);
        }
 

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


Comments
This issue is a duplicate of JDK-6258215. The following text, from ServerSocket constructors that accept an integer backlog arg, and from bind(..), explains that the backlog is effectively a "hint". "The backlog argument is the requested maximum number of pending connections on the socket. Its exact semantics are implementation specific. In particular, an implementation may impose a maximum length or may choose to ignore the parameter altogther. The value provided should be greater than 0. If it is less than or equal to 0, then an implementation specific default will be used."
22-12-2014