changeset 16372:70f9673ebcd6

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
author bpb
date Wed, 21 Dec 2016 07:49:36 -0800
parents 212c89e7dbe5
children bd9dd28d7b4f
files src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java
diffstat 1 files changed, 16 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java	Wed Dec 21 20:16:29 2016 +0530
+++ b/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 {