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

SocketInputStream.socketRead0 can hang even with soTimeout set

    Details

    • Subcomponent:
    • Resolved In Build:
      b137
    • CPU:
      x86_64
    • OS:
      linux

      Backports

        Description

        FULL PRODUCT VERSION :


        A DESCRIPTION OF THE PROBLEM :
        As noted at JDK-8049846, the implementation of Java_java_net_SocketInputStream_socketRead0 assumes that read() won't block after poll() reports that a read is possible. This assumption does not hold, as noted on the man page for select (referenced by the man page for poll): Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it may be safer to use O_NONBLOCK on sockets that should not block.

        In production, we hit this about once a day. For testing and reproduction purposes, we can use fault injection to get spurious poll() results on demand.

        [This report is probably more appropriate as a comment on JDK-8049846, but commenting requires an account, and obtaining an account does not appear to be an easy task]

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Create file called poll.c with contents:
        -----
        #define _GNU_SOURCE
        #include <poll.h>
        #include <dlfcn.h>
        #include <stdio.h>

        int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
          static int n;
          if ((++n & 0x3) == 0) {
            // Fault injection: perhaps we should report a spurious readiness notification
            int i;
            for (i = 0; i < nfds; ++i) {
              if (fds[i].events & POLLIN) {
                fds[i].revents |= POLLIN;
        return 1;
              }
            }
          }
          return ((int(*)(struct pollfd*,nfds_t,int))dlsym(RTLD_NEXT, "poll"))(fds, nfds, timeout);
        }
        -----

        2. Create file called OneReaderThread.java with contents:
        -----
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.OutputStream;
        import java.net.ServerSocket;
        import java.net.Socket;

        public class OneReaderThread {
        public static void main(String[] args) throws IOException {
        @SuppressWarnings("resource")
        ServerSocket serverSocket = new ServerSocket(17291);
        new ReadingThreadA().start();
        Socket client = serverSocket.accept();
        OutputStream os = client.getOutputStream();
        byte[] writeData = new byte[2];
        for (;;) {
        waitingForA = true;
        long start = System.currentTimeMillis();
        while (waitingForA) {
        long now = System.currentTimeMillis();
        if (now > start + 500) {
        // 500ms have passed, which is 10x the read timeout
        System.out.println("Should never happen: A is unresponsive");
        os.write(writeData);
        break;
        }
        }
        }
        }

        private static volatile boolean waitingForA;

        private static final class ReadingThreadA extends Thread {
        @Override
        public void run() {
        try {
        @SuppressWarnings("resource")
        Socket s = new Socket("localhost", 17291);
        s.setSoTimeout(50); // SO_TIMEOUT is set, meaning that reads should not block for more than 50ms
        final InputStream is = s.getInputStream();
        byte[] readDataA = new byte[2];
        for (;;) {
        int n = 0;
        try {
        n = is.read(readDataA);
        } catch (IOException e) {
        // Ignore
        }
        System.out.println("A tick (" + n + ")");
        waitingForA = false; // This assignment should happen at least once every 50ms
        }
        } catch (Exception e) {
        e.printStackTrace();
        }
        }
        }
        }
        -----

        3. Compile the C code: gcc -o poll.so -shared poll.c -ldl -fPIC
        4. Compile the Java code: javac OneReaderThread.java
        5. Run the Java code with the C library preloaded: LD_PRELOAD=./poll.so java -cp . OneReaderThread

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Expect an output stream consisting solely of:
        A tick (0)
        A tick (0)
        A tick (0)
        A tick (0)
        A tick (0)
        A tick (0)
        A tick (0)
        A tick (0)
        ACTUAL -
        Actual output stream is repetitions of:
        Should never happen: A is unresponsive
        A tick (2)
        A tick (0)
        A tick (0)
        A tick (0)
        Should never happen: A is unresponsive
        A tick (2)
        A tick (0)
        A tick (0)
        A tick (0)

        REPRODUCIBILITY :
        This bug can be reproduced occasionally.

        1. OneReaderThread.java
          1 kB
          Pardeep Sharma
        2. poll.c
          0.5 kB
          Pardeep Sharma

          Issue Links

            Activity

            webbuggrp Webbug Group created issue -
            pardesha Pardeep Sharma made changes -
            Field Original Value New Value
            Assignee Pardeep Sharma [ pardesha ]
            pardesha Pardeep Sharma made changes -
            Attachment OneReaderThread.java [ 26416 ]
            Attachment poll.c [ 26417 ]
            pardesha Pardeep Sharma made changes -
            Assignee Pardeep Sharma [ pardesha ]
            pardesha Pardeep Sharma made changes -
            Project Java Incidents [ 10301 ] JDK [ 10100 ]
            Key JI-9019858 JDK-8075484
            pardesha Pardeep Sharma made changes -
            Affects Version/s 8u40 [ 16510 ]
            Affects Version/s 8u40 [ 16902 ]
            Subcomponent java.net [ 498 ] java.net [ 193 ]
            Component/s core-libs [ 10300 ]
            Component/s core-libs [ 10701 ]
            pardesha Pardeep Sharma made changes -
            Link This issue relates to JDK-8049846 [ JDK-8049846 ]
            jeff Jeff Dinkins made changes -
            Assignee Pavel Rappo [ prappo ]
            prappo Pavel Rappo made changes -
            Fix Version/s tbd_minor [ 11999 ]
            bpb Brian Burkhalter made changes -
            Status New [ 10000 ] Open [ 1 ]
            psonal Pallavi Sonal made changes -
            Link This issue duplicates JI-9026741 [ JI-9026741 ]
            bvaidya Balchandra Vaidya made changes -
            Priority P4 [ 4 ] P3 [ 3 ]
            vtewari Vyom Tewari made changes -
            Assignee Pavel Rappo [ prappo ] Vyom Tewari [ vtewari ]
            hgupdate HG Updates made changes -
            Status Open [ 1 ] Resolved [ 5 ]
            Resolved In Build team [ 17324 ]
            Fix Version/s 9 [ 14949 ]
            Fix Version/s tbd_minor [ 11999 ]
            Resolution Fixed [ 1 ]
            hgupdate HG Updates made changes -
            Resolved In Build team [ 17324 ] master [ 18256 ]
            hgupdate HG Updates made changes -
            Resolved In Build master [ 18256 ] b137 [ 17499 ]
            coffeys Sean Coffey made changes -
            Link This issue duplicates JDK-8049846 [ JDK-8049846 ]
            rcalnan Roger Calnan made changes -
            Link This issue relates to JDK-8049846 [ JDK-8049846 ]
            tidu Tim Du (Inactive) made changes -
            Link This issue duplicates JDK-8143305 [ JDK-8143305 ]
            rcalnan Roger Calnan made changes -
            Link This issue relates to JDK-8134601 [ JDK-8134601 ]
            clanger Christoph Langer made changes -
            Labels webbug noreg-hard webbug
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8172578 [ JDK-8172578 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8180513 [ JDK-8180513 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8181697 [ JDK-8181697 ]
            robm Robert Mckenna made changes -
            Link This issue duplicates JDK-8180756 [ JDK-8180756 ]
            ostuart Owen Stuart made changes -
            Labels noreg-hard webbug 8bpr-critical-request noreg-hard webbug
            ostuart Owen Stuart made changes -
            Labels 8bpr-critical-request noreg-hard webbug noreg-hard webbug
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8182435 [ JDK-8182435 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8182665 [ JDK-8182665 ]
            ostuart Owen Stuart made changes -
            Labels noreg-hard webbug 7bpr-critical-request noreg-hard webbug
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8183082 [ JDK-8183082 ]
            robm Robert Mckenna made changes -
            Labels 7bpr-critical-request noreg-hard webbug 7bpr-critical-request 8bpr-critical-request noreg-hard webbug
            robm Robert Mckenna made changes -
            Labels 7bpr-critical-request 8bpr-critical-request noreg-hard webbug 7bpr-critical-request noreg-hard webbug
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8183241 [ JDK-8183241 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8183485 [ JDK-8183485 ]
            ostuart Owen Stuart made changes -
            Labels 7bpr-critical-request noreg-hard webbug 7bpr-critical-approved noreg-hard webbug
            robm Robert Mckenna made changes -
            Labels 7bpr-critical-approved noreg-hard webbug 7bpr-critical-approved CPU17_04-critical-request noreg-hard webbug
            pmohan Praveen Mohan made changes -
            Labels 7bpr-critical-approved CPU17_04-critical-request noreg-hard webbug 7bpr-critical-approved CPU17_04-critical-SQE-OK CPU17_04-critical-request noreg-hard webbug
            ydagra Yashi Dagra made changes -
            Labels 7bpr-critical-approved CPU17_04-critical-SQE-OK CPU17_04-critical-request noreg-hard webbug 7bpr-critical-approved CPU17_04-critical-SQE-OK CPU17_04-critical-approved noreg-hard webbug
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8184442 [ JDK-8184442 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8184443 [ JDK-8184443 ]
            robm Robert Mckenna made changes -
            Link This issue duplicates JDK-8182311 [ JDK-8182311 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8185234 [ JDK-8185234 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8185284 [ JDK-8185284 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8189503 [ JDK-8189503 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8189556 [ JDK-8189556 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8192432 [ JDK-8192432 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8193986 [ JDK-8193986 ]
            hgupdate HG Updates made changes -
            Link This issue backported by JDK-8197341 [ JDK-8197341 ]

              People

              • Assignee:
                vtewari Vyom Tewari
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                12 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: