From f986518c51fb9282371c600b60b3dd511ceff730 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Wed, 21 Dec 2016 07:49:36 -0800 Subject: [PATCH] 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 --- .../classes/sun/nio/ch/SocketAdaptor.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/jdk/src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java b/jdk/src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java index bf43c8b2317..68d795b3441 100644 --- a/jdk/src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java +++ b/jdk/src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java @@ -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 {