8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks

Change System.currentTimeMillis() to System.nanoTime()

Reviewed-by: martin, rriggs
This commit is contained in:
Brian Burkhalter 2016-12-21 07:49:36 -08:00
parent 6802af0822
commit f986518c51

View File

@ -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.*;
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 @@ public class SocketAdaptor
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 @@ public class SocketAdaptor
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 {