8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
authorbpb
Wed, 21 Dec 2016 07:49:36 -0800
changeset 42923 691c320b44f7
parent 42922 9111fb672357
child 42924 0d68b06248fb
8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks Summary: Change System.currentTimeMillis() to System.nanoTime() Reviewed-by: martin, rriggs
jdk/src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java
--- a/jdk/src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java	Wed Dec 21 20:16:29 2016 +0530
+++ b/jdk/src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java	Wed Dec 21 07:49:36 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,7 @@
 import java.nio.channels.*;
 import java.security.AccessController;
 import java.security.PrivilegedExceptionAction;
-
+import java.util.concurrent.TimeUnit;
 
 // Make a socket channel look like a socket.
 //
@@ -99,17 +99,19 @@
                 try {
                     if (sc.connect(remote))
                         return;
-                    long to = timeout;
+                    long timeoutNanos =
+                        TimeUnit.NANOSECONDS.convert(timeout,
+                            TimeUnit.MILLISECONDS);
                     for (;;) {
                         if (!sc.isOpen())
                             throw new ClosedChannelException();
-                        long st = System.currentTimeMillis();
+                        long startTime = System.nanoTime();
 
-                        int result = sc.poll(Net.POLLCONN, to);
+                        int result = sc.poll(Net.POLLCONN, timeout);
                         if (result > 0 && sc.finishConnect())
                             break;
-                        to -= System.currentTimeMillis() - st;
-                        if (to <= 0) {
+                        timeoutNanos -= System.nanoTime() - startTime;
+                        if (timeoutNanos <= 0) {
                             try {
                                 sc.close();
                             } catch (IOException x) { }
@@ -194,18 +196,20 @@
                     int n;
                     if ((n = sc.read(bb)) != 0)
                         return n;
-                    long to = timeout;
+                    long timeoutNanos =
+                        TimeUnit.NANOSECONDS.convert(timeout,
+                            TimeUnit.MILLISECONDS);
                     for (;;) {
                         if (!sc.isOpen())
                             throw new ClosedChannelException();
-                        long st = System.currentTimeMillis();
-                        int result = sc.poll(Net.POLLIN, to);
+                        long startTime = System.nanoTime();
+                        int result = sc.poll(Net.POLLIN, timeout);
                         if (result > 0) {
                             if ((n = sc.read(bb)) != 0)
                                 return n;
                         }
-                        to -= System.currentTimeMillis() - st;
-                        if (to <= 0)
+                        timeoutNanos -= System.nanoTime() - startTime;
+                        if (timeoutNanos <= 0)
                             throw new SocketTimeoutException();
                     }
                 } finally {