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

(se) Selector.select(Long.MAX_VALUE) fires repeatedly

XMLWordPrintable

    • b154
    • x86_64
    • windows_7

        FULL PRODUCT VERSION :
        java version "1.8.0_111"
        Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
        Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]

        EXTRA RELEVANT SYSTEM CONFIGURATION :
        N/A (I think)

        A DESCRIPTION OF THE PROBLEM :
        When Selector.select(Long.MAX_VALUE) is used, select() returns immediately. I found out that two values not that far apart, namely 850_000_000_000_000L and 825_000_000_000_000L have different behavior - first one causes "select" to instantly return and second one works as expected (probably, it would block for a very, very long time).


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run code provided below.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        select(to) should timeout only after 'to' milliseconds, it shouldn't return immediately.
        ACTUAL -
        select(Long.MAX_VALUE) returns immediately.

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------

        package playground;

        import java.io.IOException;
        import java.net.InetSocketAddress;
        import java.net.ServerSocket;
        import java.nio.channels.SelectionKey;
        import java.nio.channels.Selector;
        import java.nio.channels.ServerSocketChannel;
        import java.util.Set;

        public class SelTest {

            public static void main(String[] args) throws IOException {
                Selector selector = Selector.open();
                ServerSocketChannel dc = ServerSocketChannel.open();
                ServerSocket sock = dc.socket();
                sock.bind(new InetSocketAddress("localhost", 12000));
                dc.configureBlocking(false);
                dc.register(selector, SelectionKey.OP_ACCEPT);
                int count = 0;
                while (true) {
                    System.out.println(count++);
                    selector.select(850_000_000_000_000L); // 825_000_000_000_000L or 850_000_000_000_000L
                    Set<SelectionKey> selectedKeysSet = selector.selectedKeys();
                    selectedKeysSet.clear();
                }
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Set lower values of 'timeout' parameter. In reality anything higher than one minut, so probably 60_000 is fine in most cases.

              bpb Brian Burkhalter
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: