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

Impossible to create a socket backlog greater than 200 on Windows 8+

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • None
    • core-libs
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10, OpenJDK 17

      A DESCRIPTION OF THE PROBLEM :
      Microsoft Windows 8+ has a socket backlog limit of 200. To override this limit, you need to pass a negative value, e.g., -500, to Win32 API listen(). However, from Java this is impossible, as non-positive backlog values are always overridden to 50.

      This can be see here in OpenJDK17: sun.nio.ch.ServerSocketChannelImpl.bind(): Net.listen(fd, backlog < 1 ? 50 : backlog);

      And here: java.net.ServerSocket.bind(java.net.SocketAddress, int): if (backlog < 1) backlog = 50;

      Win32 API docs for listen() introduce the C macro SOMAXCONN_HINT() to specify very large socket backlog sizes. The C macro SOMAXCONN_HINT() defined in WinSock2.h. Looking at the macro definition, you can see that it simply negates the value. I wrote sample code to demonstrate very large backlog sizes can be created from Java using a negative backlog size during socket listen.

      The "override to 50" is probably ancient behaviour and cannot be changed without breaking some existing code in the wild. (See: https://xkcd.com/1172/) Ideas: (1) Can you either add notes to the Java doc about this issue, or (2) add a new bind() method that allows negative backlog sizes? As a workaround, I wrote a custom impl of bind() that calls the JNI listen method directly with a negative backlog size. I confirm that a very large backlog (>200) is possible on Windows from Java.

      More details here: https://stackoverflow.com/questions/78365010/how-to-set-java-server-socket-backlog-to-a-negative-value-on-windows


            michaelm Michael McMahon
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: